Class: Cask::Cmd::Upgrade Private

Inherits:
AbstractCommand show all
Defined in:
Library/Homebrew/cask/cmd/upgrade.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Implementation of the brew cask upgrade command.

Constant Summary collapse

OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  [:switch, "--skip-cask-deps", {
    description: "Skip installing cask dependencies.",
  }],
  [:switch, "--greedy", {
    description: "Also include casks with `auto_updates true` or `version :latest`.",
  }],
].freeze

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCommand

abstract?, banner_args, banner_headline, command_name, help, #initialize, max_named, min_named, run, short_description, visible?

Methods included from Homebrew::Search

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

Constructor Details

This class inherits a constructor from Cask::Cmd::AbstractCommand

Class Method Details

.descriptionObject

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.


13
14
15
# File 'Library/Homebrew/cask/cmd/upgrade.rb', line 13

def self.description
  "Upgrades all outdated casks or the specified casks."
end

.parserObject

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.


26
27
28
29
30
31
32
33
34
35
36
37
# File 'Library/Homebrew/cask/cmd/upgrade.rb', line 26

def self.parser
  super do
    switch "--force",
           description: "Force overwriting existing files."
    switch "--dry-run",
           description: "Show what would be upgraded, but do not actually upgrade anything."

    OPTIONS.each do |option|
      send(*option)
    end
  end
end

.upgrade_cask(old_cask, new_cask, binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:) ⇒ Object

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.


114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'Library/Homebrew/cask/cmd/upgrade.rb', line 114

def self.upgrade_cask(
  old_cask, new_cask,
  binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:
)
  require "cask/installer"

  odebug "Started upgrade process for Cask #{old_cask}"
  old_config = old_cask.config

  old_options = {
    binaries: binaries,
    verbose:  verbose,
    force:    force,
    upgrade:  true,
  }.compact

  old_cask_installer =
    Installer.new(old_cask, **old_options)

  new_cask.config = new_cask.default_config.merge(old_config)

  new_options = {
    binaries:       binaries,
    verbose:        verbose,
    force:          force,
    skip_cask_deps: skip_cask_deps,
    require_sha:    require_sha,
    upgrade:        true,
    quarantine:     quarantine,
  }.compact

  new_cask_installer =
    Installer.new(new_cask, **new_options)

  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 => 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

.upgrade_casks(*casks, args:, force: false, greedy: false, dry_run: false, skip_cask_deps: false, verbose: false, binaries: nil, quarantine: nil, require_sha: nil) ⇒ Object

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.

Raises:


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

def self.upgrade_casks(
  *casks,
  args:,
  force: false,
  greedy: false,
  dry_run: false,
  skip_cask_deps: false,
  verbose: false,
  binaries: nil,
  quarantine: nil,
  require_sha: nil
)

  quarantine = true if quarantine.nil?

  outdated_casks = if casks.empty?
    Caskroom.casks(config: Config.from_args(args)).select do |cask|
      cask.outdated?(greedy: greedy)
    end
  else
    casks.select do |cask|
      raise CaskNotInstalledError, cask unless cask.installed? || force

      cask.outdated?(greedy: true)
    end
  end

  return if outdated_casks.empty?

  ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if casks.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("\n")
  return if dry_run

  upgradable_casks.each do |(old_cask, new_cask)|
    upgrade_cask(
      old_cask, new_cask,
      binaries: binaries, force: force, skip_cask_deps: skip_cask_deps, verbose: verbose,
      quarantine: quarantine, require_sha: require_sha
    )
  rescue => e
    caught_exceptions << e.exception("#{new_cask.full_name}: #{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

Instance Method Details

#runObject

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.


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'Library/Homebrew/cask/cmd/upgrade.rb', line 39

def run
  verbose = ($stdout.tty? || args.verbose?) && !args.quiet?
  self.class.upgrade_casks(
    *casks,
    force:          args.force?,
    greedy:         args.greedy?,
    dry_run:        args.dry_run?,
    binaries:       args.binaries?,
    quarantine:     args.quarantine?,
    require_sha:    args.require_sha?,
    skip_cask_deps: args.skip_cask_deps?,
    verbose:        verbose,
    args:           args,
  )
end