Class: Hbc::CLI::Upgrade

Inherits:
AbstractCommand show all
Defined in:
Library/Homebrew/cask/lib/hbc/cli/upgrade.rb

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCommand

abstract?, command_name, needs_init?, run, visible

Methods included from Options

included, #process_arguments

Constructor Details

#initializeUpgrade

Returns a new instance of Upgrade



9
10
11
12
# File 'Library/Homebrew/cask/lib/hbc/cli/upgrade.rb', line 9

def initialize(*)
  super
  self.verbose = ($stdout.tty? || verbose?) && !quiet?
end

Class Method Details

.helpObject



82
83
84
# File 'Library/Homebrew/cask/lib/hbc/cli/upgrade.rb', line 82

def self.help
  "upgrades all outdated casks"
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'Library/Homebrew/cask/lib/hbc/cli/upgrade.rb', line 14

def run
  outdated_casks = casks(alternative: lambda {
    Hbc.installed.select do |cask|
      cask.outdated?(greedy?)
    end
  }).select { |cask| cask.outdated?(true) }

  if outdated_casks.empty?
    oh1 "No Casks to upgrade"
    return
  end

  oh1 "Upgrading #{Formatter.pluralize(outdated_casks.length, "outdated package")}, with result:"
  puts outdated_casks.map { |f| "#{f.full_name} #{f.version}" } * ", "

  outdated_casks.each do |old_cask|
    odebug "Started upgrade process for Cask #{old_cask}"
    raise CaskNotInstalledError, old_cask unless old_cask.installed? || force?

    raise CaskUnavailableError.new(old_cask, "The Caskfile is missing!") if old_cask.installed_caskfile.nil?

    old_cask = CaskLoader.load(old_cask.installed_caskfile)

    old_cask_installer = Installer.new(old_cask, binaries: binaries?, verbose: verbose?, force: force?, upgrade: true)

    new_cask = CaskLoader.load(old_cask.to_s)

    new_cask_installer =
      Installer.new(new_cask, binaries:       binaries?,
                              verbose:        verbose?,
                              force:          force?,
                              skip_cask_deps: skip_cask_deps?,
                              require_sha:    require_sha?,
                              upgrade: true)

    started_upgrade = false
    new_artifacts_installed = false

    begin
      # Start new Cask's installation steps
      new_cask_installer.check_conflicts

      new_cask_installer.fetch

      # Move the old Cask's artifacts back to staging
      old_cask_installer.start_upgrade
      # And flag it so in case of error
      started_upgrade = true

      # Install the new Cask
      new_cask_installer.stage

      new_cask_installer.install_artifacts
      new_artifacts_installed = true

      new_cask_installer.enable_accessibility_access

      # If successful, wipe the old Cask from staging
      old_cask_installer.finalize_upgrade
    rescue CaskError => e
      new_cask_installer.uninstall_artifacts if new_artifacts_installed
      new_cask_installer.purge_versioned_files
      old_cask_installer.revert_upgrade if started_upgrade
      raise e
    end
  end
end