Class: Cask::Upgrade Private

Inherits:
Object show all
Defined in:
cask/upgrade.rb

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Class Method Summary collapse

Class Method Details

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

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • old_cask (Cask)
  • new_cask (Cask)
  • binaries (Boolean, nil)
  • force (Boolean, nil)
  • quarantine (Boolean, nil)
  • require_sha (Boolean, nil)
  • skip_cask_deps (Boolean, nil)
  • verbose (Boolean, nil)


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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'cask/upgrade.rb', line 148

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

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

  old_options = {
    binaries:,
    verbose:,
    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:,
    verbose:,
    force:,
    skip_cask_deps:,
    require_sha:,
    upgrade:        true,
    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

    if (caveats = new_cask_installer.caveats)
      puts caveats
    end

    new_cask_installer.fetch

    # Move the old cask's artifacts back to staging
    old_cask_installer.start_upgrade(successor: new_cask)
    # And flag it so in case of error
    started_upgrade = true

    # Install the new cask
    new_cask_installer.stage

    new_cask_installer.install_artifacts(predecessor: old_cask)
    new_artifacts_installed = true

    # If successful, wipe the old cask from staging.
    old_cask_installer.finalize_upgrade
  rescue => e
    new_cask_installer.uninstall_artifacts(successor: old_cask) if new_artifacts_installed
    new_cask_installer.purge_versioned_files
    old_cask_installer.revert_upgrade(predecessor: new_cask) if started_upgrade
    raise e
  end

  end_time = Time.now
  Homebrew.messages.package_installed(new_cask.token, end_time - start_time)
end

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

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • casks (Cask)
  • args (Homebrew::CLI::Args)
  • force (Boolean, nil) (defaults to: false)
  • greedy (Boolean, nil) (defaults to: false)
  • greedy_latest (Boolean, nil) (defaults to: false)
  • greedy_auto_updates (Boolean, nil) (defaults to: false)
  • dry_run (Boolean, nil) (defaults to: false)
  • skip_cask_deps (Boolean, nil) (defaults to: false)
  • verbose (Boolean, nil) (defaults to: false)
  • quiet (Boolean, nil) (defaults to: false)
  • binaries (Boolean, nil) (defaults to: nil)
  • quarantine (Boolean, nil) (defaults to: nil)
  • require_sha (Boolean, nil) (defaults to: nil)

Returns:

  • (Boolean)

Raises:



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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'cask/upgrade.rb', line 26

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

  quarantine = true if quarantine.nil?

  greedy = true if Homebrew::EnvConfig.upgrade_greedy?

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

      if cask.outdated?(greedy: true)
        true
      elsif cask.version.latest?
        opoo "Not upgrading #{cask.token}, the downloaded artifact has not changed" unless quiet
        false
      else
        opoo "Not upgrading #{cask.token}, the latest version is already installed" unless quiet
        false
      end
    end
  end

  manual_installer_casks = outdated_casks.select do |cask|
    cask.artifacts.any? do |artifact|
      artifact.is_a?(Artifact::Installer) && artifact.manual_install
    end
  end

  if manual_installer_casks.present?
    count = manual_installer_casks.count
    ofail "Not upgrading #{count} `installer manual` #{::Utils.pluralize("cask", count)}."
    puts manual_installer_casks.map(&:to_s)
    outdated_casks -= manual_installer_casks
  end

  return false if outdated_casks.empty?

  if casks.empty? && !greedy
    if !greedy_auto_updates && !greedy_latest
      ohai "Casks with 'auto_updates true' or 'version :latest' " \
           "will not be upgraded; pass `--greedy` to upgrade them."
    end
    if greedy_auto_updates && !greedy_latest
      ohai "Casks with 'version :latest' will not be upgraded; pass `--greedy-latest` to upgrade them."
    end
    if !greedy_auto_updates && greedy_latest
      ohai "Casks with 'auto_updates true' will not be upgraded; pass `--greedy-auto-updates` to upgrade them."
    end
  end

  verb = dry_run ? "Would upgrade" : "Upgrading"
  oh1 "#{verb} #{outdated_casks.count} outdated #{::Utils.pluralize("package", outdated_casks.count)}:"

  caught_exceptions = []

  upgradable_casks = outdated_casks.map do |c|
    unless c.installed?
      odie <<~EOS
        The cask '#{c.token}' was affected by a bug and cannot be upgraded as-is. To fix this, run:
          brew reinstall --cask --force #{c.token}
      EOS
    end

    [CaskLoader.load(c.installed_caskfile), c]
  end

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

  upgradable_casks.each do |(old_cask, new_cask)|
    upgrade_cask(
      old_cask, new_cask,
      binaries:, force:, skip_cask_deps:, verbose:,
      quarantine:, require_sha:
    )
  rescue => e
    new_exception = e.exception("#{new_cask.full_name}: #{e}")
    new_exception.set_backtrace(e.backtrace)
    caught_exceptions << new_exception
    next
  end

  return true if caught_exceptions.empty?
  raise MultipleCaskErrors, caught_exceptions if caught_exceptions.count > 1
  raise caught_exceptions.fetch(0) if caught_exceptions.count == 1

  false
end