Class: Cask::Cmd::Upgrade

Inherits:
AbstractCommand show all
Defined in:
brew/Library/Homebrew/cask/cmd/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, run, visible

Methods included from Homebrew::Search

#query_regexp, #search_casks, #search_descriptions, #search_formulae, #search_taps

Methods included from Options

#process_arguments

Constructor Details

#initializeUpgrade

Returns a new instance of Upgrade



14
15
16
17
# File 'brew/Library/Homebrew/cask/cmd/upgrade.rb', line 14

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

Class Method Details

.helpObject



114
115
116
# File 'brew/Library/Homebrew/cask/cmd/upgrade.rb', line 114

def self.help
  "upgrades all outdated casks"
end

Instance Method Details

#runObject

Raises:



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
# File 'brew/Library/Homebrew/cask/cmd/upgrade.rb', line 19

def run
  outdated_casks = casks(alternative: lambda {
    Caskroom.casks.select do |cask|
      cask.outdated?(greedy?)
    end
  }).select do |cask|
    raise CaskNotInstalledError, cask unless cask.installed? || force?

    cask.outdated?(true)
  end

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

  ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if args.empty? && !greedy?
  verb = dry_run? ? "Would upgrade" : "Upgrading"
  oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:"
  caught_exceptions = []

  upgradable_casks = outdated_casks.map { |c| [CaskLoader.load(c.installed_caskfile), c] }

  puts upgradable_casks
    .map { |(old_cask, new_cask)| "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}" }
    .join(", ")
  return if dry_run?

  upgradable_casks.each do |(old_cask, new_cask)|
    upgrade_cask(old_cask, new_cask)
  rescue CaskError => e
    caught_exceptions << e
    next
  end

  return if caught_exceptions.empty?
  raise MultipleCaskErrors, caught_exceptions if caught_exceptions.count > 1
  raise caught_exceptions.first if caught_exceptions.count == 1
end

#upgrade_cask(old_cask, new_cask) ⇒ Object



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
# File 'brew/Library/Homebrew/cask/cmd/upgrade.rb', line 59

def upgrade_cask(old_cask, new_cask)
  odebug "Started upgrade process for Cask #{old_cask}"
  old_config = old_cask.config

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

  new_cask.config = Config.global.merge(old_config)

  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,
                            quarantine:     quarantine?)

  started_upgrade = false
  new_artifacts_installed = false

  begin
    oh1 "Upgrading #{Formatter.identifier(old_cask)}"

    # Start new Cask's installation steps
    new_cask_installer.check_conflicts

    puts new_cask_installer.caveats if new_cask_installer.caveats

    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

    # 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