Class: Lich::Util::Update::FileUpdater
- Inherits:
-
Object
- Object
- Lich::Util::Update::FileUpdater
- Defined in:
- documented/common/update/file_updater.rb
Overview
Updates individual scripts, libraries, and data files from script repositories.
Handles both repository-specific file updates (e.g., dr-scripts:foo.lic) and
legacy auto-detected updates. Supports SHA-based skip-if-current checks for
script repository files to avoid redundant downloads.
Instance Method Summary collapse
-
#initialize(client, resolver, snapshot_manager) ⇒ FileUpdater
constructor
A new instance of FileUpdater.
-
#update_core_data_and_scripts(version = LICH_VERSION, snapshot_dir = nil) ⇒ void
Updates core data files (effect-list.xml) after version upgrade.
-
#update_file(type, rf, version = 'production') ⇒ void
Updates a file using legacy auto-detection logic.
-
#update_file_from_repo(type, repo_key, filename) ⇒ void
Updates a file from a specific repository.
Constructor Details
#initialize(client, resolver, snapshot_manager) ⇒ FileUpdater
Returns a new instance of FileUpdater.
28 29 30 31 32 |
# File 'documented/common/update/file_updater.rb', line 28 def initialize(client, resolver, snapshot_manager) @client = client @resolver = resolver @snapshot_manager = snapshot_manager end |
Instance Method Details
#update_core_data_and_scripts(version = LICH_VERSION, snapshot_dir = nil) ⇒ void
This method returns an undefined value.
Updates core data files (effect-list.xml) after version upgrade.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'documented/common/update/file_updater.rb', line 208 def update_core_data_and_scripts(version = LICH_VERSION, snapshot_dir = nil) if XMLData.game !~ /^GS|^DR/ respond "invalid game type, unsure what scripts to update via Update.update_core_scripts" return end if XMLData.game =~ /^GS/ ["effect-list.xml"].each do |file| if File.exist?(File.join(DATA_DIR, file)) data_backup_dir = File.join(snapshot_dir || @snapshot_manager.new_data_backup_dir, "data") FileUtils.mkdir_p(data_backup_dir) newfilename = File.join(data_backup_dir, file) File.open(File.join(DATA_DIR, file), 'rb') { |r| File.open(newfilename, 'wb') { |w| w.write(r.read) } } respond "The prior version of #{file} was backed up to #{newfilename}." end update_file('data', file) end end Lich.core_updated_with_lich_version = version end |
#update_file(type, rf, version = 'production') ⇒ void
This method returns an undefined value.
Updates a file using legacy auto-detection logic.
127 128 129 130 131 132 133 134 135 136 137 138 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 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 |
# File 'documented/common/update/file_updater.rb', line 127 def update_file(type, rf, version = 'production') if version =~ /^(?:staging|master)$/i respond 'Requested channel %s mapped to main (stable).' % [version] version = 'production' end requested_file = rf case type when "script" location = SCRIPT_DIR if requested_file.downcase == 'dependency.lic' remote_repo = "https://raw.githubusercontent.com/elanthia-online/dr-scripts/main" else remote_repo = "https://raw.githubusercontent.com/elanthia-online/scripts/master/scripts" end requested_file_ext = requested_file =~ /\.lic$/ ? ".lic" : "bad extension" when "library" location = LIB_DIR case version when "production" remote_repo = "https://raw.githubusercontent.com/#{GITHUB_REPO}/#{@resolver.resolve_channel_ref(:stable)}/lib" when "beta" ref = @resolver.resolve_channel_ref(:beta) if ref.nil? if @resolver.last_error StatusReporter.respond_github_failure(@resolver.last_error, 'No beta update was installed.') else respond 'No viable beta found. Aborting beta update.' end return end remote_repo = "https://raw.githubusercontent.com/#{GITHUB_REPO}/#{ref}/lib" end requested_file_ext = requested_file =~ /\.rb$/ ? ".rb" : "bad extension" when "data" location = DATA_DIR remote_repo = "https://raw.githubusercontent.com/elanthia-online/scripts/master/scripts" requested_file_ext = requested_file =~ /(\.(?:xml|ui))$/ ? $1&.dup : "bad extension" end unless requested_file_ext == "bad extension" file_path = File.join(location, requested_file) begin # Download fully before touching the file, then replace it # atomically: the live file stays in place for the whole # network read and is untouched if anything fails. content = URI.parse(File.join(remote_repo, requested_file)).open.read FileWriter.safe_write(file_path, content) respond respond "#{requested_file} has been updated." rescue StandardError => e respond respond "Error updating #{requested_file}: #{e.class} - #{e.}" respond "Backtrace: #{e.backtrace.first(3).join(' | ')}" if $debug respond "The existing #{requested_file} was left unchanged." if File.exist?(file_path) respond respond "The filename #{requested_file} is not available via lich5-update." respond "Check the spelling of your requested file, or use '#{$clean_lich_char}jinx' to" respond "download #{requested_file} from another repository." end else respond respond "The requested file #{requested_file} has an incorrect extension." respond "Valid extensions are '.lic' for scripts, '.rb' for library files," respond "and '.xml' or '.ui' for data files. Please correct and try again." end end |
#update_file_from_repo(type, repo_key, filename) ⇒ void
This method returns an undefined value.
Updates a file from a specific repository.
40 41 42 43 44 45 46 47 48 49 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 |
# File 'documented/common/update/file_updater.rb', line 40 def update_file_from_repo(type, repo_key, filename) config = SCRIPT_REPOS[repo_key] unless config custom_reg = CustomRepos.all[repo_key] config = CustomRepos.build_config(repo_key, custom_reg) if custom_reg end unless config all_keys = (SCRIPT_REPOS.keys + CustomRepos.all.keys).join(', ') respond "[lich5-update: Unknown repository '#{repo_key}'. Known: #{all_keys}]" return end if config[:custom] && type == "data" respond "[lich5-update: Data file downloads are not supported for custom repos. Only --script= is available.]" return end case type when "script" location = config[:dest_dir] || SCRIPT_DIR FileUtils.mkdir_p(location) if config[:dest_dir] when "data" data_subdir = (config[:subdirs] || {})['data'] location = data_subdir ? data_subdir[:dest] : File.join(SCRIPT_DIR, 'data') FileUtils.mkdir_p(location) else respond "[lich5-update: repo:filename syntax is only supported for --script= and --data=.]" return end prefix = config[:script_prefix] tree_data = @client.fetch_github_json(config[:api_url]) unless tree_data && tree_data['tree'] name = config[:display_name] || repo_key StatusReporter.respond_github_failure(@client.last_error, "#{filename} was not updated.", subject: name) return end raw_path = if type == "data" data_subdir = (config[:subdirs] || {})['data'] if data_subdir && data_subdir[:pattern] match = tree_data['tree'].find { |e| e['path'] =~ data_subdir[:pattern] && File.basename(e['path']) == filename } match ? match['path'] : nil else prefix ? "#{prefix}/#{filename}" : filename end elsif prefix "#{prefix}/#{filename}" else filename end remote_entry = raw_path ? tree_data['tree'].find { |e| e['path'] == raw_path } : nil if remote_entry local_path = File.join(location, filename) if File.exist?(local_path) local_sha = Digest::SHA1.hexdigest("blob #{File.binread(local_path).bytesize}\0#{File.binread(local_path)}") if local_sha == remote_entry['sha'] StatusReporter.respond_mono("[lich5-update: #{filename} is already up to date.]") return end end else name = config[:display_name] || repo_key StatusReporter.respond_mono("[lich5-update: #{filename} not found in #{name} repository.]") return end name = config[:display_name] || repo_key url = "#{config[:raw_base_url]}/#{raw_path}" content = @client.http_get(url, auth: false) if content FileWriter.safe_write(File.join(location, filename), content) StatusReporter.respond_mono("[lich5-update: #{filename} has been updated from #{name}.]") else # The tree just listed this file, so a repo-specific error here is # transient (e.g. raw CDN lag), not a missing repository. error = @client.last_error StatusReporter.respond_github_failure(error&.global? ? error : nil, "#{filename} was not updated.") end end |