Class: Lich::Util::Update::ReleaseInstaller
- Inherits:
-
Object
- Object
- Lich::Util::Update::ReleaseInstaller
- Defined in:
- documented/common/update/release_installer.rb
Overview
Manages Lich5 installation from GitHub releases (stable and beta).
Fetches release metadata, checks Ruby compatibility, downloads tarballs, extracts archives, validates structure, and coordinates library/script updates via FileUpdater and backup management via SnapshotManager. Synchronizes multiple concurrent commands (announcement, update, beta install) through class-level mutexes to prevent race conditions.
Constant Summary collapse
- TOP_LEVEL_FILES =
Top-level files (besides lib/ and lich.rbw) copied verbatim from the release/branch archive into LICH_DIR during a self-update. lich.rbw is handled separately due to its dynamic target name.
Gemfile.lock travels alongside Gemfile so the resolved dependency set on disk stays consistent with the Gemfile after an update. Shipping a new Gemfile without its lock would leave a stale lock that a later
bundle install(or any Bundler invocation) has to re-resolve. Entries absent from the archive are skipped (see #copy_top_level_files). %w[Gemfile Gemfile.lock LICENSE].freeze
- REQUIRED_ARCHIVE_ITEMS =
Archive entries that must exist for an extracted download to count as a structurally valid Lich installation. Deliberately distinct from TOP_LEVEL_FILES so that optional payload (e.g. Gemfile.lock, which older release tarballs may omit) never gates an update.
%w[lib lich.rbw Gemfile LICENSE].freeze
- REQUIRED_RUBY_PATTERN =
Regex that extracts the REQUIRED_RUBY version string from version.rb.
Matches lines like
REQUIRED_RUBY = "3.1.0"orREQUIRED_RUBY = '3.1.0'and captures the version number (group 1). /REQUIRED_RUBY\s*=\s*["']([^"']+)["']/.freeze
- GEMSTONE_INSTALL_URL =
URL to GemStone IV Lich installation and upgrade documentation.
Linked in error messages when the current Ruby version is too old for the target Lich release.
'https://gswiki.play.net/Lich:Software/Installation'.freeze
- BETA_CONFIRM_WINDOW =
Seconds a beta notice stays open for
lich5-update --beta --confirm. 60- BETA_LOCK =
Guards the check-and-clear of the beta notice. Class-level so that an installer built before this file was reloaded still has a lock; the guard keeps a reload from swapping the mutex while it is held.
Mutex.new
- RELEASE_LOCK =
Serialises every command that sets, reads or clears the release target (announce, update, beta install). One installer serves the whole session, and the login autostart runs --announce on a script thread while lich5-update runs on the client thread, so without it one command could clear the target another is installing. Reentrant, because the beta install calls #download_release_update under the lock, and #announce and #download_release_update call #prep_update. Class-level for the same reload reason as BETA_LOCK.
Monitor.new
Instance Method Summary collapse
-
#announce ⇒ void
Displays announcement if new version available.
-
#check_ruby_compatibility(source_dir, version) ⇒ Boolean
Checks if current Ruby meets minimum version requirement.
-
#confirm_betatest ⇒ void
Installs the latest beta if the notice from #prep_betatest was shown within the last BETA_CONFIRM_WINDOW seconds.
-
#copy_top_level_files(source_dir) ⇒ void
Copies each top-level file present in the extracted archive into LICH_DIR.
-
#download_release_update ⇒ void
Downloads and installs latest stable release.
-
#extract_version_from_file(version_file_path) ⇒ String?
Extracts LICH_VERSION constant from version.rb file.
-
#initialize(client, resolver, snapshot_manager) ⇒ ReleaseInstaller
constructor
A new instance of ReleaseInstaller.
-
#perform_update(source_dir, version, snapshot_dir = nil) ⇒ Boolean
Copies lib files and lich.rbw from extracted source to install dirs.
-
#prep_betatest(type = nil, requested_file = nil) ⇒ void
Handles beta update requests (full install or individual file).
-
#prep_update ⇒ void
Fetches latest stable release metadata from GitHub.
-
#ruby_upgrade_required? ⇒ Boolean
Reads the target release's Ruby floor without downloading its archive.
-
#validate_lich_structure(dir) ⇒ Boolean
Validates the extracted directory contains every archive entry Lich requires to install safely.
Constructor Details
#initialize(client, resolver, snapshot_manager) ⇒ ReleaseInstaller
Returns a new instance of ReleaseInstaller.
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'documented/common/update/release_installer.rb', line 78 def initialize(client, resolver, snapshot_manager) @client = client @resolver = resolver @snapshot_manager = snapshot_manager @current = LICH_VERSION @update_to = nil @holder = nil @new_features = nil @zipfile = nil @release_tag = nil @required_ruby = nil @beta_prompted_at = nil end |
Instance Method Details
#announce ⇒ void
This method returns an undefined value.
Displays announcement if new version available. Waits for any install in progress to finish first.
96 97 98 |
# File 'documented/common/update/release_installer.rb', line 96 def announce RELEASE_LOCK.synchronize { announce_release } end |
#check_ruby_compatibility(source_dir, version) ⇒ Boolean
Checks if current Ruby meets minimum version requirement.
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'documented/common/update/release_installer.rb', line 410 def check_ruby_compatibility(source_dir, version) version_file_path = File.join(source_dir, "lib", "version.rb") if File.exist?(version_file_path) version_file_content = File.read(version_file_path) if (match = version_file_content.match(REQUIRED_RUBY_PATTERN)) required_ruby_version = match[1] current_ruby_version = RUBY_VERSION if Gem::Version.new(current_ruby_version) < Gem::Version.new(required_ruby_version) respond respond "*** UPDATE ABORTED ***" respond respond "Lich version #{version} requires Ruby #{required_ruby_version} or higher." respond "Your current Ruby version is #{current_ruby_version}." respond respond "Please update your Ruby installation before updating Lich." respond respond "DragonRealms - https://github.com/elanthia-online/lich-5/wiki/Documentation-for-Installing-and-Upgrading-Lich" respond "Gemstone IV - #{GEMSTONE_INSTALL_URL}" respond return false end end end true end |
#confirm_betatest ⇒ void
This method returns an undefined value.
Installs the latest beta if the notice from #prep_betatest was shown within the last BETA_CONFIRM_WINDOW seconds. A notice can be confirmed only once; a second confirmation needs a fresh notice.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'documented/common/update/release_installer.rb', line 237 def confirm_betatest prompted_at = BETA_LOCK.synchronize do value = @beta_prompted_at @beta_prompted_at = nil value end if prompted_at.nil? respond "There is no beta test request to confirm. Run #{$clean_lich_char}lich5-update --beta first and read the notice." return end if monotonic_now - prompted_at > BETA_CONFIRM_WINDOW respond "Your beta test request expired after #{BETA_CONFIRM_WINDOW} seconds. Run #{$clean_lich_char}lich5-update --beta again to start over." return end respond 'Beta test installation accepted. Thank you for considering!' # Held from setting the beta target until the install clears it. The # ensure also clears a target set before the install began, such as # when reading the release record raises. RELEASE_LOCK.synchronize do install_beta ensure clear_release_target end end |
#copy_top_level_files(source_dir) ⇒ void
This method returns an undefined value.
Copies each top-level file present in the extracted archive into LICH_DIR. Files missing from the archive are skipped rather than treated as errors, so a partial archive (e.g. one without Gemfile.lock) updates cleanly instead of aborting.
386 387 388 389 390 391 392 393 394 |
# File 'documented/common/update/release_installer.rb', line 386 def copy_top_level_files(source_dir) TOP_LEVEL_FILES.each do |filename| src = File.join(source_dir, filename) next unless File.exist?(src) FileUtils.cp(src, File.join(LICH_DIR, filename)) respond "Updated #{filename}." end end |
#download_release_update ⇒ void
This method returns an undefined value.
Downloads and installs latest stable release.
Also installs the beta target that the beta install set up just before calling this. The target is cleared when this returns or raises, so it never carries over into the next command. Holds RELEASE_LOCK for the whole install, so a concurrent announcement waits rather than clearing the target mid-download.
274 275 276 |
# File 'documented/common/update/release_installer.rb', line 274 def download_release_update RELEASE_LOCK.synchronize { install_release } end |
#extract_version_from_file(version_file_path) ⇒ String?
Extracts LICH_VERSION constant from version.rb file.
440 441 442 443 444 445 446 447 448 |
# File 'documented/common/update/release_installer.rb', line 440 def extract_version_from_file(version_file_path) return nil unless File.exist?(version_file_path) version_file_content = File.read(version_file_path) if version_file_content =~ /LICH_VERSION\s*=\s*['"]([^'"]+)['"]/ return $1 end nil end |
#perform_update(source_dir, version, snapshot_dir = nil) ⇒ Boolean
Copies lib files and lich.rbw from extracted source to install dirs.
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'documented/common/update/release_installer.rb', line 352 def perform_update(source_dir, version, snapshot_dir = nil) unless validate_lich_structure(source_dir) respond "Error: extracted source is missing required files. Aborting update to protect installation." return false end FileUtils.rm_rf(Dir.glob(File.join(LIB_DIR, "*"))) respond respond 'Copying updated lich files to their locations.' FileUtils.copy_entry(File.join(source_dir, "lib"), File.join(LIB_DIR)) respond respond "All Lich lib files have been updated." respond copy_top_level_files(source_dir) file_updater = FileUpdater.new(@client, @resolver, @snapshot_manager) file_updater.update_core_data_and_scripts(version, snapshot_dir) lich_to_update = File.join(LICH_DIR, File.basename($PROGRAM_NAME)) update_to_lich = File.join(source_dir, "lich.rbw") File.open(update_to_lich, 'rb') { |r| File.open(lich_to_update, 'wb') { |w| w.write(r.read) } } true end |
#prep_betatest(type = nil, requested_file = nil) ⇒ void
This method returns an undefined value.
Handles beta update requests (full install or individual file).
A full beta install is a two-step command so that it never reads a client socket: this step only prints the notice and opens a confirmation window; #confirm_betatest performs the install.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'documented/common/update/release_installer.rb', line 217 def prep_betatest(type = nil, requested_file = nil) if type.nil? BETA_LOCK.synchronize { @beta_prompted_at = monotonic_now } respond 'You are electing to participate in the beta testing of the next Lich release.' respond 'This beta test will include only Lich code, and does not include Ruby updates.' respond 'While we will do everything we can to ensure you have a smooth experience, ' respond 'it is a test, and untoward things can result.' respond "To confirm your participation, run: #{$clean_lich_char}lich5-update --beta --confirm" respond "You have #{BETA_CONFIRM_WINDOW} seconds to confirm. To decline, do nothing; the request expires on its own." else file_updater = FileUpdater.new(@client, @resolver, @snapshot_manager) file_updater.update_file(type, requested_file, 'beta') end end |
#prep_update ⇒ void
This method returns an undefined value.
Fetches latest stable release metadata from GitHub. Any target left from an earlier command is dropped first, so a failed check leaves no target rather than the previous one. Holds RELEASE_LOCK so that the target it sets is the one the calling command acts on.
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 |
# File 'documented/common/update/release_installer.rb', line 146 def prep_update RELEASE_LOCK.synchronize do clear_release_target latest = @client.fetch_github_json("https://api.github.com/repos/#{GITHUB_REPO}/releases/latest") if latest.is_a?(Hash) && latest['prerelease'] all = @client.fetch_github_json("https://api.github.com/repos/#{GITHUB_REPO}/releases") if all.is_a?(Array) stable = all.select { |r| !r['prerelease'] && r['tag_name'] }.max_by { |r| @resolver.version_key(r['tag_name']) } latest = stable if stable end end unless latest.is_a?(Hash) StatusReporter.respond_github_failure(@client.last_error, 'Could not check for a new Lich version; your current installation is unaffected.') return end @holder = latest['assets'] release_asset = @holder && @holder.find { |x| x['name'] =~ /\b#{ASSET_TARBALL_NAME}\b/ } unless release_asset respond "Update notice: no release tarball found in assets (prep_update)." return end @release_tag = latest['tag_name'].to_s @update_to = @release_tag.sub(/^v/, '') @new_features = latest['body'].to_s.gsub(/\#\# What's Changed.+$/m, '').gsub(/<!--[\s\S]*?-->/, '') @zipfile = release_asset.fetch('browser_download_url') end end |
#ruby_upgrade_required? ⇒ Boolean
Reads the target release's Ruby floor without downloading its archive. A failed metadata read must not suppress an otherwise valid update notice.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'documented/common/update/release_installer.rb', line 194 def ruby_upgrade_required? return false if @release_tag.nil? || @release_tag.empty? version_url = "https://raw.githubusercontent.com/#{GITHUB_REPO}/#{@release_tag}/lib/version.rb" version_content = @client.http_get(version_url, auth: false) match = version_content&.match(REQUIRED_RUBY_PATTERN) return false unless match @required_ruby = match[1] Gem::Version.new(RUBY_VERSION) < Gem::Version.new(@required_ruby) rescue ArgumentError false end |
#validate_lich_structure(dir) ⇒ Boolean
Validates the extracted directory contains every archive entry Lich requires to install safely.
401 402 403 |
# File 'documented/common/update/release_installer.rb', line 401 def validate_lich_structure(dir) REQUIRED_ARCHIVE_ITEMS.all? { |item| File.exist?(File.join(dir, item)) } end |