Class: Lich::Util::Update::SnapshotManager

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

Overview

Manages snapshot and rollback functionality for Lich core files.

This class creates timestamped backups of lib/, lich.rbw, and core scripts before updates and supports rollback to the most recent snapshot.

See Also:

Constant Summary collapse

CORE_SCRIPTS =
%w[
  alias.lic autostart.lic dependency.lic ewaggle.lic foreach.lic
  go2.lic infomon.lic jinx.lic lnet.lic log.lic logxml.lic
  map.lic repository.lic vars.lic version.lic
].freeze

Instance Method Summary collapse

Instance Method Details

#revertvoid

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.

Reverts Lich5 to the previously installed version.

This method restores the Lich5 environment from the most recent snapshot, including core files and scripts. If no snapshot is found, an error message is displayed.



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
# File 'documented/common/update/snapshot_manager.rb', line 67

def revert
  respond
  respond 'Reverting Lich5 to previously installed version.'

  revert_array = Dir.glob(File.join(BACKUP_DIR, "L5-snapshot-*")).sort.reverse
  restore_snapshot = revert_array.first
  if restore_snapshot.nil?
    respond "No prior Lich5 version found. Seek assistance."
  else
    FileUtils.rm_rf(Dir.glob(File.join(LIB_DIR, "*")))
    FileUtils.cp_r(File.join(restore_snapshot, "lib", "."), LIB_DIR)

    CORE_SCRIPTS.each do |file|
      File.delete(File.join(SCRIPT_DIR, file)) if File.exist?(File.join(SCRIPT_DIR, file))
    end
    FileUtils.cp_r(File.join(restore_snapshot, "scripts", "."), SCRIPT_DIR)

    lich_to_update = File.join(LICH_DIR, File.basename($PROGRAM_NAME))
    update_to_lich = File.join(restore_snapshot, File.basename($PROGRAM_NAME))
    File.open(update_to_lich, 'rb') { |r| File.open(lich_to_update, 'wb') { |w| w.write(r.read) } }

    targetversion = ''
    targetfile = File.read(File.join(LIB_DIR, "version.rb"))
    targetfile.each_line do |line|
      if line =~ /LICH_VERSION\s*=\s*['"]([^'"]+)['"]/
        targetversion = $1
      end
    end

    Lich::Util::Update.clear_branch_tracking

    respond
    respond "Lich5 has been reverted to Lich5 version #{targetversion}"
    respond "You should exit the game, then log back in.  This will start the game"
    respond "with your previous version of Lich.  Enjoy!"
  end
end

#snapshotvoid

This method returns an undefined value.

Creates a snapshot of the current Lich core files.

This method creates a backup of the current Lich core files in a timestamped directory. It is recommended to also copy the entire Lich5 folder for additional safety after updates.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'documented/common/update/snapshot_manager.rb', line 32

def snapshot
  respond
  respond 'Creating a snapshot of current Lich core files ONLY.'
  respond
  respond 'You may also wish to copy your entire Lich5 folder to'
  respond 'another location for additional safety, after any'
  respond 'additional requested updates are completed.'

  snapshot_subdir = File.join(BACKUP_DIR, "L5-snapshot-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}")
  FileUtils.mkdir_p(snapshot_subdir)

  FileUtils.cp(File.join(LICH_DIR, File.basename($PROGRAM_NAME)),
               File.join(snapshot_subdir, File.basename($PROGRAM_NAME)))

  FileUtils.mkdir_p(File.join(snapshot_subdir, "lib"))
  FileUtils.cp_r(LIB_DIR, snapshot_subdir)

  FileUtils.mkdir_p(File.join(snapshot_subdir, "scripts"))
  CORE_SCRIPTS.each do |file|
    source = File.join(SCRIPT_DIR, file)
    FileUtils.cp(source, File.join(snapshot_subdir, "scripts", file)) if File.exist?(source)
  end

  respond
  respond 'Current Lich ecosystem files (only) backed up to:'
  respond "    #{snapshot_subdir}"
end