Class: Cask::Upgrade Private
- 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
- .greedy_casks ⇒ Array<String> private
- .outdated_casks(casks, args:, force:, quiet:, greedy: false, greedy_latest: false, greedy_auto_updates: false, summary_pinned: nil, summary_disabled: nil, refresh_metadata: false) ⇒ Array<Cask> private
- .quarantine_release_decision(old_cask, new_cask, old_signing_identities, old_user_approved, old_unquarantined) ⇒ Object private
- .show_upgrade_summary(cask_upgrades, dry_run: false, verb: "Upgrading") ⇒ void private
- .upgrade_cask(old_cask, new_cask, binaries:, force:, require_sha:, quit:, skip_cask_deps:, verbose:, download_queue:, new_cask_installer: nil) ⇒ void private
- .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, require_sha: nil, quit: true, skip_prefetch: false, show_upgrade_summary: true, download_queue: nil, summary_upgrades: nil, summary_pinned: nil, summary_deprecated: nil, summary_disabled: nil, prefetched_errors: nil, upgraded_casks: nil, prefetched_cask_installers: nil) ⇒ Object private
Methods included from Utils::Output::Mixin
issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_incompatible, pretty_install_status, pretty_installed, pretty_removed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning
Class Method Details
.greedy_casks ⇒ Array<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.
19 20 21 22 23 24 25 |
# File 'cask/upgrade.rb', line 19 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_pinned: nil, summary_disabled: nil, refresh_metadata: false) ⇒ 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.
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 |
# File 'cask/upgrade.rb', line 41 def self.outdated_casks(casks, args:, force:, quiet:, greedy: false, greedy_latest: false, greedy_auto_updates: false, summary_pinned: nil, summary_disabled: nil, refresh_metadata: false) greedy = true if Homebrew::EnvConfig.upgrade_greedy? on_self_update = ->(cask, installed) { Installer.new(cask).refresh_self_update(installed) } if outdated_casks = 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.(cask)}" unless quiet next false end cask_greedy = greedy || greedy_casks.include?(cask.token) cask.outdated_version(greedy: cask_greedy, greedy_latest:, greedy_auto_updates:, &on_self_update) 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.(cask)}" unless quiet next false end version = cask.version if version.nil? opoo "Not upgrading #{cask.token}, no version is available for the current platform" unless quiet false elsif cask.outdated_version(greedy: true, &on_self_update) true elsif cask.auto_updates_app? opoo "Not upgrading #{cask.token}, no newer app version was detected" unless quiet false elsif 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 pinned_casks = outdated_casks.select(&:pinned?) outdated_casks -= pinned_casks pinned_versions = pinned_casks.map do |cask| VersionChange.new(name: cask.full_name, old_version: cask.installed_version.to_s, new_version: cask.version.to_s) end summary_pinned&.concat(pinned_versions) if pinned_casks.any? && ((!quiet && summary_pinned.nil?) || casks.any?) = "Not upgrading #{pinned_casks.count} pinned #{::Utils.pluralize("package", pinned_casks.count)}:" casks.any? ? ofail() : opoo() $stderr.puts Formatter.version_changes(pinned_versions).join("\n") if !quiet && summary_pinned.nil? end outdated_casks end |
.quarantine_release_decision(old_cask, new_cask, old_signing_identities, old_user_approved, old_unquarantined) ⇒ Object
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.
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 |
# File 'cask/upgrade.rb', line 339 def self.quarantine_release_decision(old_cask, new_cask, old_signing_identities, old_user_approved, old_unquarantined) old_app_artifacts = old_cask.artifacts.grep(Artifact::App) new_app_artifacts = new_cask.artifacts.grep(Artifact::App) return :skip if old_app_artifacts.empty? || old_app_artifacts.length != new_app_artifacts.length return :unapproved unless old_app_artifacts.all? do |artifact| # An app with no quarantine attribute already launches without a Gatekeeper prompt, so carry # that state forward as well: an app whose own updater replaced the bundle should not cost the # user a prompt at its next upgrade. The signing identity is still checked below. old_user_approved.fetch(artifact.target.to_s, false) || old_unquarantined.fetch(artifact.target.to_s, false) end old_app_artifacts.each_with_index do |artifact, index| old_identity = old_signing_identities[artifact.target.to_s] return :signer_unverified if old_identity.nil? identity_matches = Quarantine.signing_identity_match(new_app_artifacts.fetch(index).target, old_identity) return :signer_unverified if identity_matches.nil? return :signer_changed unless identity_matches end :release rescue :skip end |
.show_upgrade_summary(cask_upgrades, dry_run: false, verb: "Upgrading") ⇒ 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.
106 107 108 109 110 111 112 113 |
# File 'cask/upgrade.rb', line 106 def self.show_upgrade_summary(cask_upgrades, dry_run: false, verb: "Upgrading") cask_upgrades = cask_upgrades.uniq return if cask_upgrades.empty? verb = "Would upgrade" if dry_run oh1 "#{verb} #{cask_upgrades.count} outdated #{::Utils.pluralize("package", cask_upgrades.count)}:" puts Formatter.version_changes(cask_upgrades).join("\n") end |
.upgrade_cask(old_cask, new_cask, binaries:, force:, require_sha:, quit:, skip_cask_deps:, verbose:, download_queue:, new_cask_installer: nil) ⇒ 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.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'cask/upgrade.rb', line 419 def self.upgrade_cask( old_cask, new_cask, binaries:, force:, require_sha:, quit:, skip_cask_deps:, verbose:, download_queue:, new_cask_installer: nil ) completed = false start_time = Time.now odebug "Started upgrade process for Cask #{old_cask}" old_config = old_cask.config = { binaries:, verbose:, force:, upgrade: true, }.compact old_cask_installer = Installer.new(old_cask, **) old_tab = old_cask.tab new_cask.config = new_cask.default_config.merge(old_config) report_after_staging = new_cask_installer.nil? new_cask_installer ||= fallback_installer(new_cask, binaries:, force:, require_sha:, skip_cask_deps:, verbose:, download_queue:) raise CaskError, "Download failed for #{new_cask}." if new_cask_installer.download_failed? 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]) old_unquarantined = T.let({}, T::Hash[String, T::Boolean]) old_cask.temp = new_cask.temp begin oh1 "Upgrading #{Formatter.identifier(old_cask)}" puts " #{old_cask.version} -> #{new_cask.version}" # Start new cask's installation steps new_cask_installer.prelude new_cask_installer.record_caveats new_cask_installer.fetch # This snapshot reads quarantine metadata, so it needs the same guard as the code below that uses it. if Quarantine.available? 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_unquarantined[artifact.target.to_s] = if artifact.target.exist? Quarantine.detect(artifact.target).blank? 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, quit:) # And flag it so in case of error started_upgrade = true # Install the new cask new_cask_installer.stage ::Utils::Analytics.report_installs(casks: [new_cask_installer]) if report_after_staging new_cask_installer.install_artifacts(predecessor: old_cask) new_artifacts_installed = true if Quarantine.available? case quarantine_release_decision(old_cask, new_cask, old_signing_identities, old_user_approved, old_unquarantined) when :release if old_unquarantined.value?(true) odebug "#{new_cask.token} wasn't quarantined so approving the new version to match." end new_cask.artifacts.grep(Artifact::App).each do |artifact| Quarantine.inherit_user_approval!(download_path: artifact.target) rescue CaskQuarantineReleaseError => e odebug e opoo "Homebrew couldn't inherit #{new_cask.token}'s quarantine approval so macOS may prompt at " \ "next launch." end when :signer_changed opoo "#{new_cask.token}'s signer changed so macOS may prompt at next launch." when :signer_unverified opoo "Homebrew couldn't verify #{new_cask.token}'s signer so macOS may prompt at next launch." when :unapproved = "#{new_cask.token} wasn't quarantine approved so not approving now. " \ "macOS may prompt at next launch." if verbose ohai else odebug end end end # If successful, wipe the old cask from staging. old_cask_installer.finalize_upgrade reopen_apps_after_upgrade(old_cask, new_cask) if quit rescue => e begin new_cask_installer.uninstall_artifacts(successor: old_cask, quit:) if new_artifacts_installed # A :latest cask still owns the shared staging path until its backup succeeds. if started_upgrade || old_cask.staged_path != new_cask.staged_path new_cask_installer.purge_versioned_files end old_cask_installer.revert_upgrade(predecessor: new_cask) if started_upgrade rescue => rollback_error opoo "Rolling back the failed upgrade of #{old_cask.token} also failed: " \ "#{rollback_error.class}: #{rollback_error.}" if (rollback_backtrace = rollback_error.backtrace) odebug "Rollback backtrace:", rollback_backtrace end end new_cask_installer.purge_staged_download raise e end # Wait until rollback is no longer possible so failures keep the old # receipt, while successful upgrades can load artifacts next time. tab = Tab.create(new_cask) tab.installed_on_request = old_tab.tabfile.nil? || old_tab.installed_on_request tab.write end_time = Time.now Homebrew..package_installed(new_cask.token, end_time - start_time) completed = true ensure new_cask.cleanup_temp(retain: !completed) old_cask.temp = nil 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, require_sha: nil, quit: true, skip_prefetch: false, show_upgrade_summary: true, download_queue: nil, summary_upgrades: nil, summary_pinned: nil, summary_deprecated: nil, summary_disabled: nil, prefetched_errors: nil, upgraded_casks: nil, prefetched_cask_installers: nil) ⇒ Object
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.
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'cask/upgrade.rb', line 144 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, require_sha: nil, quit: true, skip_prefetch: false, show_upgrade_summary: true, download_queue: nil, summary_upgrades: nil, summary_pinned: nil, summary_deprecated: nil, summary_disabled: nil, prefetched_errors: nil, upgraded_casks: nil, prefetched_cask_installers: nil ) outdated_casks = self.outdated_casks(casks, args:, greedy:, greedy_latest:, greedy_auto_updates:, force:, quiet:, summary_pinned:, summary_disabled:, refresh_metadata: !dry_run) 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| loaded_cask = if c.installed? && (installed_caskfile = c.installed_caskfile) begin CaskLoader.load_from_installed_caskfile(installed_caskfile) rescue CaskInvalidError, CaskUnavailableError, MethodDeprecatedError, JSON::ParserError CaskLoader.recover_from_installed_caskfile(installed_caskfile, fallback_cask: c) end end if loaded_cask.nil? = <<~EOS The cask '#{c.token}' cannot be upgraded as-is. To fix this, run: brew reinstall --cask --force #{c.token} EOS args.named.present? ? ofail() : opoo() next end [loaded_cask, c] end return false if upgradable_casks.empty? # Report each failure as it happens and carry on with the other casks, # rather than aborting the run; `ofail` still exits nonzero at the end. prefetched_errors&.each { |error| ofail error } failed = T.let(prefetched_errors.present?, T::Boolean) 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 prefetched_cask_installers ||= [] if !dry_run && !skip_prefetch prefetch_download_queue = download_queue || Homebrew::DownloadQueue.default begin prefetched_cask_installers.clear upgradable_casks.select! do |(_, cask)| # This is significantly easier given the weird difference in Sorbet signatures here. # rubocop:disable Style/DoubleNegation installer = Installer.new(cask, binaries: !!binaries, verbose: !!verbose, force: !!force, skip_cask_deps: !!skip_cask_deps, require_sha: !!require_sha, upgrade: true, download_queue: prefetch_download_queue, defer_fetch: true) # rubocop:enable Style/DoubleNegation begin installer.check_requirements rescue CaskError => e ofail e failed = true next false end prefetched_cask_installers << installer true end prefetched_cask_installers.replace(Homebrew::Install.enqueue_cask_installers(prefetched_cask_installers)) prefetch_download_queue.fetch(heading: Homebrew::Install.combined_fetch_downloads_heading( cask_names: prefetched_cask_installers.map { |installer| installer.cask.full_name }, )) Homebrew::Install.fetch_cask_dependencies(prefetched_cask_installers, download_queue: prefetch_download_queue) ensure prefetch_download_queue.shutdown if created_download_queue end end return false if upgradable_casks.empty? && !failed cask_upgrades = upgradable_casks.map do |(old_cask, new_cask)| VersionChange.new(name: new_cask.full_name, old_version: old_cask.version.to_s, new_version: new_cask.version.to_s) end summary_upgrades&.concat(cask_upgrades) if dry_run summary_deprecated&.concat(upgradable_casks.filter_map do |(_, new_cask)| new_cask.full_name if new_cask.deprecated? end) show_upgrade_summary(cask_upgrades, dry_run:) if show_upgrade_summary return true if dry_run download_queue ||= Homebrew::DownloadQueue.default fallbacks = upgradable_casks.filter_map do |old_cask, new_cask| next if prefetched_cask_installers.any? { |installer| installer.cask.equal?(new_cask) } new_cask.config = new_cask.default_config.merge(old_cask.config) fallback_installer(new_cask, binaries:, force:, require_sha:, skip_cask_deps:, verbose:, download_queue:).tap(&:download_with_checks) rescue # upgrade_cask retries a failed fallback and reports a successful retry. nil end installers = prefetched_cask_installers + fallbacks ::Utils::Analytics.report_installs(casks: skip_prefetch ? fallbacks : installers) upgradable_casks.each_with_index do |(old_cask, new_cask), index| new_cask_installer = installers.find { |installer| installer.cask.equal?(new_cask) } new_cask_installer&.check_conflicts if fallbacks.include?(new_cask_installer) upgrade_cask( old_cask, new_cask, binaries:, force:, skip_cask_deps:, verbose:, require_sha:, quit:, download_queue:, new_cask_installer: ) summary_upgrades&.push(cask_upgrades.fetch(index)) upgraded_casks&.push(new_cask) rescue => e ofail "#{new_cask.full_name}: #{e}" failed = true next end !failed end |