Class: Cask::Upgrade Private

Inherits:
Object show all
Extended by:
Utils::Output::Mixin
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

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_outdated, pretty_uninstalled, pretty_upgradable

Class Method Details

.greedy_casksArray<String>

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.

Returns:



15
16
17
18
19
20
21
# File 'cask/upgrade.rb', line 15

def self.greedy_casks
  if (upgrade_greedy_casks = Homebrew::EnvConfig.upgrade_greedy_casks.presence)
    upgrade_greedy_casks.split
  else
    []
  end
end

.outdated_casks(casks, args:, force:, quiet:, greedy: false, greedy_latest: false, greedy_auto_updates: false, summary_disabled: nil) ⇒ Array<Cask>

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 (Array<Cask>)
  • args (Homebrew::CLI::Args)
  • force (Boolean, nil)
  • quiet (Boolean, nil)
  • greedy (Boolean, nil) (defaults to: false)
  • greedy_latest (Boolean, nil) (defaults to: false)
  • greedy_auto_updates (Boolean, nil) (defaults to: false)
  • summary_disabled (Array<String>, nil) (defaults to: nil)

Returns:



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
# File 'cask/upgrade.rb', line 35

def self.outdated_casks(casks, args:, force:, quiet:,
                        greedy: false, greedy_latest: false, greedy_auto_updates: false,
                        summary_disabled: nil)
  # Validate mutually exclusive opt-in/opt-out env vars before we start
  # selecting casks so `brew upgrade` errors consistently.
  Homebrew::EnvConfig.upgrade_auto_updates_casks?
  greedy = true if Homebrew::EnvConfig.upgrade_greedy?

  if casks.empty?
    Caskroom.casks(config: Config.from_args(args)).select do |cask|
      if cask.disabled?
        summary_disabled&.push(cask.full_name)
        opoo "Not upgrading #{cask.token}, it is #{DeprecateDisable.message(cask)}" unless quiet
        next false
      end

      cask_greedy = greedy || greedy_casks.include?(cask.token)
      cask.outdated?(greedy: cask_greedy, greedy_latest:,
                     greedy_auto_updates:)
    end
  else
    casks.select do |cask|
      raise CaskNotInstalledError, cask if !cask.installed? && !force

      if cask.disabled?
        summary_disabled&.push(cask.full_name)
        opoo "Not upgrading #{cask.token}, it is #{DeprecateDisable.message(cask)}" unless quiet
        next false
      end

      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
end

.release_app_upgrade_quarantine?(old_cask, new_cask, old_signing_identities, old_user_approved) ⇒ 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:

Returns:

  • (Boolean)


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'cask/upgrade.rb', line 273

def self.release_app_upgrade_quarantine?(old_cask, new_cask, old_signing_identities, old_user_approved)
  old_app_artifacts = old_cask.artifacts.grep(Artifact::App)
  new_app_artifacts = new_cask.artifacts.grep(Artifact::App)
  return false if old_app_artifacts.empty? || old_app_artifacts.length != new_app_artifacts.length

  old_app_artifacts.each_with_index.all? do |artifact, index|
    next false unless old_user_approved.fetch(artifact.target.to_s, false)

    old_identity = old_signing_identities[artifact.target.to_s]
    new_identity = Quarantine.signing_identity(new_app_artifacts.fetch(index).target)

    [
      [old_identity&.identifier, new_identity&.identifier],
      [old_identity&.team_identifier, new_identity&.team_identifier],
    ].none? do |old_value, new_value|
      !old_value.nil? && !new_value.nil? && old_value != new_value
    end
  end
rescue
  false
end

.show_upgrade_summary(cask_upgrades, dry_run: false) ⇒ 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:

  • cask_upgrades (Array<String>)
  • dry_run (Boolean, nil) (defaults to: false)


79
80
81
82
83
84
85
# File 'cask/upgrade.rb', line 79

def self.show_upgrade_summary(cask_upgrades, dry_run: false)
  return if cask_upgrades.empty?

  verb = dry_run ? "Would upgrade" : "Upgrading"
  oh1 "#{verb} #{cask_upgrades.count} outdated #{::Utils.pluralize("package", cask_upgrades.count)}:"
  puts cask_upgrades.join("\n")
end

.upgrade_cask(old_cask, new_cask, binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:, download_queue:) ⇒ 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)
  • download_queue (Homebrew::DownloadQueue)


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'cask/upgrade.rb', line 308

def self.upgrade_cask(
  old_cask, new_cask,
  binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:, download_queue:
)
  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,
    download_queue:,
  }.compact

  new_cask_installer =
    Installer.new(new_cask, **new_options, quarantine: quarantine != false, defer_fetch: true)

  started_upgrade = false
  new_artifacts_installed = false
  old_signing_identities = T.let({}, T::Hash[String, T.nilable(Quarantine::SigningIdentity)])
  old_user_approved = T.let({}, T::Hash[String, T::Boolean])

  begin
    oh1 "Upgrading #{Formatter.identifier(old_cask)}"
    puts "  #{old_cask.version} -> #{new_cask.version}"

    # Start new cask's installation steps
    new_cask_installer.check_conflicts

    if (caveats = new_cask_installer.caveats)
      puts caveats
    end

    new_cask_installer.fetch

    if quarantine.nil?
      old_cask.artifacts.grep(Artifact::App).each do |artifact|
        old_user_approved[artifact.target.to_s] =
          if artifact.target.exist?
            Quarantine.user_approved?(artifact.target)
          else
            false
          end
        old_signing_identities[artifact.target.to_s] = Quarantine.signing_identity(artifact.target)
      end
    end

    # 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 quarantine.nil? &&
       Quarantine.available? &&
       release_app_upgrade_quarantine?(old_cask, new_cask, old_signing_identities, old_user_approved)
      new_cask.artifacts.grep(Artifact::App).each do |artifact|
        Quarantine.release!(download_path: artifact.target)
      end
    end

    # 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, skip_prefetch: false, show_upgrade_summary: true, download_queue: nil, summary_upgrades: nil, summary_deprecated: nil, summary_disabled: 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)
  • skip_prefetch (Boolean) (defaults to: false)
  • show_upgrade_summary (Boolean) (defaults to: true)
  • download_queue (Homebrew::DownloadQueue, nil) (defaults to: nil)
  • summary_upgrades (Array<String>, nil) (defaults to: nil)
  • summary_deprecated (Array<String>, nil) (defaults to: nil)
  • summary_disabled (Array<String>, nil) (defaults to: nil)

Returns:

  • (Boolean)

Raises:



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
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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'cask/upgrade.rb', line 110

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,
  skip_prefetch: false,
  show_upgrade_summary: true,
  download_queue: nil,
  summary_upgrades: nil,
  summary_deprecated: nil,
  summary_disabled: nil
)
  quarantine = true if quarantine.nil?

  outdated_casks =
    self.outdated_casks(casks, args:, greedy:, greedy_latest:, greedy_auto_updates:, force:, quiet:,
                               summary_disabled:)

  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 !Homebrew::EnvConfig.no_env_hints? && casks.empty? && !greedy && greedy_casks.empty?
    output_hint = false
    if !greedy_auto_updates && outdated_casks.any?(&:auto_updates)
      puts "Homebrew will now attempt to upgrade casks with `auto_updates true`."
      puts "Disable this behaviour with `HOMEBREW_NO_UPGRADE_AUTO_UPDATES_CASKS=1`."
      output_hint ||= true
    end
    if !greedy_auto_updates && !greedy_latest
      puts "Some casks with `auto_updates true` or `version :latest` may still require `--greedy`,"
      puts "`HOMEBREW_UPGRADE_GREEDY` or `HOMEBREW_UPGRADE_GREEDY_CASKS` to be upgraded."
      output_hint ||= true
    end
    if greedy_auto_updates && !greedy_latest
      puts "Casks with `version :latest` will not be upgraded; pass `--greedy-latest` to upgrade them."
      output_hint ||= true
    end
    if !greedy_auto_updates && greedy_latest
      puts "Some casks with `auto_updates true` may still require `--greedy-auto-updates` to be upgraded."
      output_hint ||= true
    end
    puts "Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`)." if output_hint
  end

  upgradable_casks = outdated_casks.filter_map do |c|
    invalid_cask = !c.installed?

    invalid_cask ||= begin
      loaded_cask = CaskLoader.load(T.must(c.installed_caskfile))
      false
    rescue CaskInvalidError, CaskUnavailableError
      true
    end

    if invalid_cask
      opoo <<~EOS
        The cask '#{c.token}' cannot be upgraded as-is. To fix this, run:
        brew reinstall --cask --force #{c.token}
      EOS
      next
    end

    [loaded_cask, c]
  end

  return false if upgradable_casks.empty?

  cask_upgrades = upgradable_casks.map do |(old_cask, new_cask)|
    "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}"
  end
  summary_upgrades&.concat(cask_upgrades)
  summary_deprecated&.concat(upgradable_casks.filter_map do |(_, new_cask)|
    new_cask.full_name if new_cask.deprecated?
  end)

  created_download_queue = T.let(false, T::Boolean)
  download_queue ||= if !dry_run && !skip_prefetch
    created_download_queue = true
    Homebrew::DownloadQueue.new(pour: true)
  end

  if !dry_run && !skip_prefetch
    prefetch_download_queue = download_queue || Homebrew.default_download_queue
    begin
      fetchable_casks = upgradable_casks.map(&:last)
      fetchable_cask_installers = fetchable_casks.map do |cask|
        # This is significantly easier given the weird difference in Sorbet signatures here.
        # rubocop:disable Style/DoubleNegation
        Installer.new(cask, binaries: !!binaries, verbose: !!verbose, force: !!force,
                                skip_cask_deps: !!skip_cask_deps, require_sha: !!require_sha,
                                upgrade: true, quarantine: quarantine != false,
                                download_queue: prefetch_download_queue, defer_fetch: true)
        # rubocop:enable Style/DoubleNegation
      end

      fetchable_cask_installers.each(&:prelude)

      fetchable_casks_sentence = fetchable_casks.map { |cask| Formatter.identifier(cask.full_name) }.to_sentence
      oh1 "Fetching downloads for: #{fetchable_casks_sentence}", truncate: false

      fetchable_cask_installers.each(&:enqueue_downloads)
      prefetch_download_queue.fetch
    ensure
      prefetch_download_queue.shutdown if created_download_queue
    end
  end

  show_upgrade_summary(cask_upgrades, dry_run:) if show_upgrade_summary
  return true if dry_run

  caught_exceptions = []

  download_queue ||= Homebrew.default_download_queue

  upgradable_casks.each do |(old_cask, new_cask)|
    upgrade_cask(
      old_cask, new_cask,
      binaries:, force:, skip_cask_deps:, verbose:,
      quarantine:, require_sha:, download_queue:
    )
  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.one?

  false
end