Class: Lich::Util::Update::TrackedScripts

Inherits:
Object
  • Object
show all
Defined in:
documented/common/update/tracked_scripts.rb

Overview

Manages user-tracked script lists for repositories in :explicit tracking mode.

Combines default tracked scripts from repository config with user-added scripts stored in UserVars. Provides methods to query, add, and remove tracked scripts, and to display tracked scripts in tabular format. Supports both built-in SCRIPT_REPOS and user-registered custom repositories.

Instance Method Summary collapse

Instance Method Details

#check_collision(script_name, exclude_repo) ⇒ String?

Checks for filename collisions across all repos.

Parameters:

  • script_name (String)

    script filename to check

  • exclude_repo (String)

    repo key to exclude from check

Returns:

  • (String, nil)

    warning message if collision found, nil otherwise



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'documented/common/update/tracked_scripts.rb', line 55

def check_collision(script_name, exclude_repo)
  # Check built-in repos
  SCRIPT_REPOS.each do |key, config|
    next if key == exclude_repo

    if config[:tracking_mode] == :all
      if File.exist?(File.join(SCRIPT_DIR, script_name))
        return "Error: '#{script_name}' is already installed from #{config[:display_name]}. The custom version would shadow it at runtime."
      end
      next
    end

    tracked = tracked_scripts(config)
    if tracked.include?(script_name)
      return "Error: '#{script_name}' is already tracked in #{config[:display_name]}."
    end
  end

  # Check custom repos
  CustomRepos.all.each do |key, _reg|
    next if key == exclude_repo

    tracked = UserVars.tracked_scripts&.dig(key) || []
    if tracked.include?(script_name)
      return "Error: '#{script_name}' is already tracked in Custom: #{key}."
    end
  end

  nil
end

#resolve_config(repo_key) ⇒ Hash?

Resolves a repo_key to its config, checking both SCRIPT_REPOS and custom repos.

Parameters:

  • repo_key (String)

    repository key

Returns:

  • (Hash, nil)

    config hash or nil



40
41
42
43
44
45
46
47
48
# File 'documented/common/update/tracked_scripts.rb', line 40

def resolve_config(repo_key)
  config = SCRIPT_REPOS[repo_key]
  return config if config

  reg = CustomRepos.all[repo_key]
  return CustomRepos.build_config(repo_key, reg) if reg

  nil
end

#show_tracked(repo_key = nil) ⇒ void

This method returns an undefined value.

Displays Terminal::Table of tracked scripts for one or all repos.

Parameters:

  • repo_key (String, nil) (defaults to: nil)

    repository key or nil for all repos



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'documented/common/update/tracked_scripts.rb', line 152

def show_tracked(repo_key = nil)
  table_rows = []

  # Built-in repos
  builtin_repos = repo_key ? {} : SCRIPT_REPOS
  if repo_key && SCRIPT_REPOS[repo_key]
    builtin_repos = { repo_key => SCRIPT_REPOS[repo_key] }
  end

  builtin_repos.each do |key, config|
    name = config[:display_name] || key
    table_rows << :separator unless table_rows.empty?
    table_rows << [{ value: "#{name} (#{key})", colspan: 3, alignment: :center }]
    table_rows << :separator

    if config[:tracking_mode] == :all
      table_rows << [{ value: "All .lic files synced automatically", colspan: 3 }]
    else
      table_rows << ['Script', 'Type', 'Status']
      table_rows << :separator
      scripts = tracked_scripts(config)
      defaults = config[:default_tracked] || []
      scripts.sort.each do |s|
        type = defaults.include?(s) ? 'default' : 'user-added'
        exists = File.exist?(File.join(SCRIPT_DIR, s))
        status = exists ? 'installed' : 'not installed'
        table_rows << [s, type, status]
      end
    end
  end

  # Custom repos
  custom_repos = CustomRepos.all
  if repo_key
    if custom_repos[repo_key]
      custom_repos = { repo_key => custom_repos[repo_key] }
    elsif builtin_repos.empty?
      respond "[lich5-update: Unknown repository '#{repo_key}'.]"
      return
    else
      custom_repos = {}
    end
  end

  custom_repos.each do |key, reg|
    config = CustomRepos.build_config(key, reg)
    name = config[:display_name]
    table_rows << :separator unless table_rows.empty?
    table_rows << [{ value: name, colspan: 3, alignment: :center }]
    table_rows << :separator
    table_rows << ['Script', 'Type', 'Status']
    table_rows << :separator

    scripts = UserVars.tracked_scripts&.dig(key) || []
    dest = config[:dest_dir]
    scripts.sort.each do |s|
      exists = File.exist?(File.join(dest, s))
      status = exists ? 'installed' : 'not installed'
      table_rows << [s, 'user-added', status]
    end

    if scripts.empty?
      table_rows << [{ value: "No scripts tracked. Use --track=#{key}:script.lic to add.", colspan: 3 }]
    end
  end

  if table_rows.empty?
    respond "[lich5-update: No repositories to display.]"
    return
  end

  table = Terminal::Table.new(rows: table_rows, title: 'Tracked Scripts')
  StatusReporter.respond_mono(table.to_s)
end

#track_script(repo_key, script_name) ⇒ void

This method returns an undefined value.

Adds a script to user's tracked list for a repository.

Parameters:

  • repo_key (String)

    repository key (built-in or custom)

  • script_name (String)

    script filename



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'documented/common/update/tracked_scripts.rb', line 91

def track_script(repo_key, script_name)
  config = resolve_config(repo_key)
  unless config
    respond "[lich5-update: Unknown repository '#{repo_key}'.]"
    return
  end
  UserVars.tracked_scripts ||= {}
  UserVars.tracked_scripts[repo_key] ||= []
  name = config[:display_name] || repo_key
  if UserVars.tracked_scripts[repo_key].include?(script_name)
    StatusReporter.respond_mono("[lich5-update: '#{script_name}' is already tracked in #{name}.]")
  else
    collision = check_collision(script_name, repo_key)
    if collision
      StatusReporter.respond_mono("[lich5-update: #{collision}]")
      return
    end
    UserVars.tracked_scripts[repo_key].push(script_name)
    Vars.save
    StatusReporter.respond_mono("[lich5-update: Added '#{script_name}' to #{name} tracked list.]")
  end
end

#tracked_scripts(config, repo_key: nil) ⇒ Array<String>

Returns all tracked scripts for a repository config.

Parameters:

  • config (Hash)

    repository config from SCRIPT_REPOS or custom repo

  • repo_key (String, nil) (defaults to: nil)

    explicit repo key (avoids config-equality lookup)

Returns:

  • (Array<String>)

    list of tracked script filenames



29
30
31
32
33
34
# File 'documented/common/update/tracked_scripts.rb', line 29

def tracked_scripts(config, repo_key: nil)
  defaults = config[:default_tracked] || []
  repo_key ||= SCRIPT_REPOS.key(config)
  user_additions = UserVars.tracked_scripts&.dig(repo_key) || [] rescue []
  (defaults + user_additions).uniq
end

#untrack_script(repo_key, script_name) ⇒ void

This method returns an undefined value.

Removes a script from user's tracked list for a repository.

Parameters:

  • repo_key (String)

    repository key (built-in or custom)

  • script_name (String)

    script filename



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'documented/common/update/tracked_scripts.rb', line 119

def untrack_script(repo_key, script_name)
  config = resolve_config(repo_key)
  unless config
    respond "[lich5-update: Unknown repository '#{repo_key}'.]"
    return
  end
  name = config[:display_name] || repo_key
  if (config[:default_tracked] || []).include?(script_name)
    StatusReporter.respond_mono("[lich5-update: '#{script_name}' is a default script and cannot be removed.]")
    return
  end
  if UserVars.tracked_scripts&.dig(repo_key)&.delete(script_name)
    Vars.save
    StatusReporter.respond_mono("[lich5-update: Removed '#{script_name}' from #{name} tracked list.]")

    # Check the appropriate directory for the installed file
    if config[:custom]
      install_path = File.join(config[:dest_dir], script_name)
    else
      install_path = File.join(SCRIPT_DIR, script_name)
    end
    if File.exist?(install_path)
      StatusReporter.respond_mono("[lich5-update: Note: #{script_name} is still installed. Delete manually if no longer needed.]")
    end
  else
    StatusReporter.respond_mono("[lich5-update: '#{script_name}' was not in your #{name} tracked list.]")
  end
end