184 lines
5.3 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
2020-08-05 11:50:01 +02:00
require "env_config"
require "cask/config"
2018-09-06 08:29:14 +02:00
module Cask
2018-09-04 08:45:48 +01:00
class Cmd
2020-08-19 10:34:07 +02:00
# Implementation of the `brew cask upgrade` command.
#
# @api private
class Upgrade < AbstractCommand
2020-08-01 02:30:46 +02:00
def self.description
"Upgrades all outdated casks or the specified casks."
end
OPTIONS = [
[:switch, "--skip-cask-deps", {
description: "Skip installing cask dependencies.",
}],
[:switch, "--greedy", {
description: "Also include casks with `auto_updates true` or `version :latest`.",
}],
].freeze
2020-08-01 02:30:46 +02:00
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
2020-08-01 02:30:46 +02:00
end
end
def run
2020-08-01 02:30:46 +02:00
verbose = ($stdout.tty? || args.verbose?) && !args.quiet?
2020-08-05 11:50:01 +02:00
self.class.upgrade_casks(
*casks,
2020-08-01 02:30:46 +02:00
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,
2020-08-05 11:50:01 +02:00
)
end
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
2020-08-05 11:50:01 +02:00
)
2020-08-01 02:30:46 +02:00
quarantine = true if quarantine.nil?
2020-08-05 11:50:01 +02:00
outdated_casks = if casks.empty?
Caskroom.casks(config: Config.from_args(args)).select do |cask|
2020-08-19 17:12:32 +01:00
cask.outdated?(greedy: greedy)
end
2020-08-05 11:50:01 +02:00
else
casks.select do |cask|
raise CaskNotInstalledError, cask unless cask.installed? || force
2018-09-17 02:45:00 +02:00
2020-08-19 17:12:32 +01:00
cask.outdated?(greedy: true)
2020-08-05 11:50:01 +02:00
end
end
2017-11-06 18:33:29 -03:00
return if outdated_casks.empty?
2018-09-03 22:08:14 -06:00
2020-08-05 11:50:01 +02:00
ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if casks.empty? && !greedy
verb = dry_run ? "Would upgrade" : "Upgrading"
2019-07-28 14:50:59 +01:00
oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:"
2020-08-05 11:50:01 +02:00
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")
2020-08-05 11:50:01 +02:00
return if dry_run
upgradable_casks.each do |(old_cask, new_cask)|
2020-08-05 11:50:01 +02:00
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
2020-08-05 11:50:01 +02:00
def self.upgrade_cask(
old_cask, new_cask,
binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:
)
2020-08-18 00:23:23 +01:00
require "cask/installer"
odebug "Started upgrade process for Cask #{old_cask}"
old_config = old_cask.config
2020-08-01 02:30:46 +02:00
old_options = {
binaries: binaries,
verbose: verbose,
force: force,
upgrade: true,
}.compact
old_cask_installer =
2020-08-01 02:30:46 +02:00
Installer.new(old_cask, **old_options)
2020-09-29 23:46:30 +02:00
new_cask.config = new_cask.default_config.merge(old_config)
2020-08-01 02:30:46 +02:00
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 =
2020-08-01 02:30:46 +02:00
Installer.new(new_cask, **new_options)
started_upgrade = false
new_artifacts_installed = false
2017-11-12 09:25:15 -03:00
begin
oh1 "Upgrading #{Formatter.identifier(old_cask)}"
# Start new cask's installation steps
new_cask_installer.check_conflicts
2017-11-11 17:21:13 -03:00
puts new_cask_installer.caveats if new_cask_installer.caveats
2018-06-02 11:31:33 +10:00
new_cask_installer.fetch
2017-11-11 17:21:13 -03:00
# 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
end
end
end