Class: Lich::Util::Update::ScriptSync Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Bulk SHA-based repository sync manager for script repositories.

Downloads scripts and data files from configured SCRIPT_REPOS and user-registered custom repos, skipping files whose local SHA1 matches the remote. Supports both :all (auto-sync all .lic) and :explicit (tracked list) tracking modes.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ScriptSync

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ScriptSync.

Parameters:



23
24
25
# File 'documented/common/update/script_sync.rb', line 23

def initialize(client)
  @client = client
end

Instance Method Details

#filter_syncable_scripts(tree, config, repo_key = nil) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Filters tree entries to syncable scripts based on tracking mode.

Parameters:

  • tree (Array<Hash>) —

    GitHub tree API response

  • config (Hash) —

    repository config from SCRIPT_REPOS or custom repo

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

    repository key for tracked script lookup

Returns:

  • (Array<Hash>) —

    filtered tree entries



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'documented/common/update/script_sync.rb', line 178

def filter_syncable_scripts(tree, config, repo_key = nil)
  candidates = tree.select { |e| e['path'] =~ config[:script_pattern] && e['type'] == 'blob' }

  case config[:tracking_mode]
  when :all
    candidates.reject { |e| File.basename(e['path']).include?('-setup') }
  when :explicit
    tracked = TrackedScripts.new.tracked_scripts(config, repo_key: repo_key)
    candidates.select { |e| tracked.include?(File.basename(e['path'])) }
  else
    candidates
  end
end

#sync_all_repos ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Syncs all registered repositories (built-in + custom) for current game. Stops at the first GitHub-wide failure: the remaining repos would fail the same way, and one message is enough.



32
33
34
35
36
37
38
39
40
# File 'documented/common/update/script_sync.rb', line 32

def sync_all_repos
  synced = false
  (SCRIPT_REPOS.keys + CustomRepos.all.keys).each do |repo_key|
    result = sync_repo(repo_key, after_synced_repo: synced)
    break if result == :github_unavailable

    synced ||= result == :synced
  end
end

#sync_repo(repo_key, force: false, after_synced_repo: false) ⇒ Symbol?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Syncs a single repository by key (built-in or custom).

Parameters:

  • repo_key (String) —

    repository key from SCRIPT_REPOS or custom repo

  • force (Boolean) (defaults to: false) —

    skip SHA check and download all (default: false)

  • after_synced_repo (Boolean) (defaults to: false) —

    an earlier repo already synced this run, so a GitHub failure must not claim nothing was updated (default: false)

Returns:

  • (Symbol, nil) —

    :synced, :github_unavailable when GitHub itself failed, or nil when the repo was skipped



50
51
52
53
54
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'documented/common/update/script_sync.rb', line 50

def sync_repo(repo_key, force: false, after_synced_repo: false)
  config = SCRIPT_REPOS[repo_key]
  unless config
    # Check custom repos
    reg = CustomRepos.all[repo_key]
    if reg
      config = CustomRepos.build_config(repo_key, reg)
    else
      known = (SCRIPT_REPOS.keys + CustomRepos.all.keys).join(', ')
      respond "[lich5-update: Unknown repository '#{repo_key}'. Known: #{known}]"
      return
    end
  end

  if config[:game_filter] && XMLData.game !~ config[:game_filter]
    return
  end

  name = config[:display_name] || repo_key
  tree_data = @client.fetch_github_json(config[:api_url])
  unless tree_data && tree_data['tree']
    error = @client.last_error
    if error && !error.global?
      StatusReporter.respond_github_failure(error, "No scripts were updated from #{name}.", subject: name)
      return nil
    end
    consequence = after_synced_repo ? 'No further scripts have been updated this run.' : 'No scripts have been updated this run.'
    StatusReporter.respond_github_failure(error, consequence)
    return :github_unavailable
  end
  tree = tree_data['tree']

  syncable = filter_syncable_scripts(tree, config, repo_key)
  StatusReporter.respond_mono("[lich5-update: Syncing #{name} (#{syncable.length} scripts)...]")

  # Custom repos write to their per-repo subdir; built-in repos to SCRIPT_DIR
  dest = config[:dest_dir] || SCRIPT_DIR
  FileUtils.mkdir_p(dest) if config[:custom]

  local_shas = FileWriter.build_local_sha_map(dest)
  downloaded_scripts = []
  failed_scripts = []
  held_scripts = []
  syncable.each do |entry|
    filename = File.basename(entry['path'])
    next if !force && local_shas[filename] == entry['sha']

    content = @client.http_get("#{config[:raw_base_url]}/#{entry['path']}", auth: false)
    unless content
      failed_scripts << filename
      next
    end

    if config[:check_lich_requirement]
      required = Lich::Common::Script.required_lich_version_in(content)
      unless Lich::Common::Script.lich_version_satisfied?(required)
        held_scripts << [filename, required]
        next
      end
    end

    begin
      FileWriter.safe_write(File.join(dest, filename), content)
      downloaded_scripts << filename
    rescue StandardError => e
      respond "[lich5-update: write failed for #{filename}: #{e.message}]"
      failed_scripts << filename
    end
  end

  downloaded_other = {}
  failed_other = {}
  (config[:subdirs] || {}).each do |subdir_name, subconfig|
    files, failures = sync_subdir(tree, config, subdir_name, subconfig)
    downloaded_other[subdir_name] = files unless files.empty?
    failed_other[subdir_name] = failures unless failures.empty?
  end

  StatusReporter.render_sync_summary(name, syncable.length, downloaded_scripts, downloaded_other, config[:subdirs]&.keys || [], failed_scripts, failed_other, held_scripts)
  :synced
end

#sync_subdir(tree, config, _subdir_name, subconfig) ⇒ Array<Array<String>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Syncs a subdirectory (profiles, data) from repository.

Parameters:

  • tree (Array<Hash>) —

    GitHub tree API response

  • config (Hash) —

    repository config from SCRIPT_REPOS

  • _subdir_name (String) —

    subdirectory name (unused)

  • subconfig (Hash) —

    subdirectory config (pattern, dest, glob)

Returns:

  • (Array<Array<String>>) —

    [downloaded_files, failed_files]



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'documented/common/update/script_sync.rb', line 139

def sync_subdir(tree, config, _subdir_name, subconfig)
  pattern = subconfig[:pattern]
  dest = subconfig[:dest]
  return [[], []] unless pattern && dest

  FileUtils.mkdir_p(dest)
  entries = tree.select { |e| e['path'] =~ pattern && e['type'] == 'blob' }
  return [[], []] if entries.empty?

  local_shas = FileWriter.build_local_sha_map(dest, subconfig[:glob] || '*.yaml')
  downloaded = []
  failed = []
  entries.each do |entry|
    filename = File.basename(entry['path'])
    next if local_shas[filename] == entry['sha']

    content = @client.http_get("#{config[:raw_base_url]}/#{entry['path']}", auth: false)
    unless content
      failed << filename
      next
    end

    begin
      FileWriter.safe_write(File.join(dest, filename), content)
      downloaded << filename
    rescue StandardError => e
      respond "[lich5-update: write failed for #{filename}: #{e.message}]"
      failed << filename
    end
  end
  [downloaded, failed]
end