2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-08-05 11:50:01 +02:00
|
|
|
require "env_config"
|
2019-02-03 13:03:16 +01:00
|
|
|
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
|
2017-10-29 17:31:07 -03:00
|
|
|
class Upgrade < AbstractCommand
|
2020-08-01 02:30:46 +02:00
|
|
|
def self.description
|
|
|
|
"Upgrades all outdated casks or the specified casks."
|
|
|
|
end
|
|
|
|
|
2020-09-28 02:34:14 +02:00
|
|
|
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."
|
2020-09-28 02:34:14 +02:00
|
|
|
|
|
|
|
OPTIONS.each do |option|
|
|
|
|
send(*option)
|
|
|
|
end
|
2020-08-01 02:30:46 +02:00
|
|
|
end
|
2017-10-29 17:31:07 -03:00
|
|
|
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,
|
2020-10-01 00:57:02 +02:00
|
|
|
args: args,
|
2020-08-05 11:50:01 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.upgrade_casks(
|
|
|
|
*casks,
|
2020-10-01 00:57:02 +02:00
|
|
|
args:,
|
2020-08-05 12:12:50 -04:00
|
|
|
force: false,
|
|
|
|
greedy: false,
|
|
|
|
dry_run: false,
|
|
|
|
skip_cask_deps: false,
|
|
|
|
verbose: false,
|
2020-08-05 10:28:58 -04:00
|
|
|
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?
|
2020-10-01 00:57:02 +02:00
|
|
|
Caskroom.casks(config: Config.from_args(args)).select do |cask|
|
2020-08-19 17:12:32 +01:00
|
|
|
cask.outdated?(greedy: greedy)
|
2017-11-24 01:21:30 +00:00
|
|
|
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
|
2018-06-09 11:19:04 +02:00
|
|
|
end
|
2017-11-06 18:33:29 -03:00
|
|
|
|
2020-08-06 16:16:19 +08: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
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
caught_exceptions = []
|
2019-05-31 19:07:53 +02:00
|
|
|
|
|
|
|
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}" }
|
2020-08-06 17:25:46 +08:00
|
|
|
.join("\n")
|
2020-08-05 11:50:01 +02:00
|
|
|
return if dry_run
|
2019-07-27 07:21:36 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2019-11-22 02:50:44 +01:00
|
|
|
rescue => e
|
2020-07-31 11:47:10 +08:00
|
|
|
caught_exceptions << e.exception("#{new_cask.full_name}: #{e}")
|
2019-10-13 10:03:26 +01:00
|
|
|
next
|
2018-09-09 00:34:33 -04:00
|
|
|
end
|
2019-05-31 19:07:53 +02:00
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
return if caught_exceptions.empty?
|
|
|
|
raise MultipleCaskErrors, caught_exceptions if caught_exceptions.count > 1
|
|
|
|
raise caught_exceptions.first if caught_exceptions.count == 1
|
|
|
|
end
|
2017-10-29 17:31:07 -03:00
|
|
|
|
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"
|
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
odebug "Started upgrade process for Cask #{old_cask}"
|
|
|
|
old_config = old_cask.config
|
2019-02-03 13:03:16 +01:00
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
old_options = {
|
|
|
|
binaries: binaries,
|
|
|
|
verbose: verbose,
|
|
|
|
force: force,
|
|
|
|
upgrade: true,
|
|
|
|
}.compact
|
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
old_cask_installer =
|
2020-08-01 02:30:46 +02:00
|
|
|
Installer.new(old_cask, **old_options)
|
2017-10-29 17:31:07 -03:00
|
|
|
|
2020-09-29 23:46:30 +02:00
|
|
|
new_cask.config = new_cask.default_config.merge(old_config)
|
2017-10-29 17:31:07 -03:00
|
|
|
|
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
|
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
new_cask_installer =
|
2020-08-01 02:30:46 +02:00
|
|
|
Installer.new(new_cask, **new_options)
|
2017-10-30 23:29:00 -03:00
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
started_upgrade = false
|
|
|
|
new_artifacts_installed = false
|
2017-11-12 09:25:15 -03:00
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
begin
|
2019-05-31 19:07:53 +02:00
|
|
|
oh1 "Upgrading #{Formatter.identifier(old_cask)}"
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Start new cask's installation steps
|
2019-04-03 19:17:12 -04:00
|
|
|
new_cask_installer.check_conflicts
|
2017-11-11 17:21:13 -03:00
|
|
|
|
2019-05-31 19:07:53 +02:00
|
|
|
puts new_cask_installer.caveats if new_cask_installer.caveats
|
2018-06-02 11:31:33 +10:00
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
new_cask_installer.fetch
|
2017-11-11 17:21:13 -03:00
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Move the old cask's artifacts back to staging
|
2019-04-03 19:17:12 -04:00
|
|
|
old_cask_installer.start_upgrade
|
|
|
|
# And flag it so in case of error
|
|
|
|
started_upgrade = true
|
2017-10-30 23:29:00 -03:00
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Install the new cask
|
2019-04-03 19:17:12 -04:00
|
|
|
new_cask_installer.stage
|
2017-11-28 13:00:08 +00:00
|
|
|
|
2019-04-03 19:17:12 -04:00
|
|
|
new_cask_installer.install_artifacts
|
|
|
|
new_artifacts_installed = true
|
2017-10-29 17:31:07 -03:00
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# If successful, wipe the old cask from staging
|
2019-04-03 19:17:12 -04:00
|
|
|
old_cask_installer.finalize_upgrade
|
2019-11-22 02:50:44 +01:00
|
|
|
rescue => e
|
2019-04-03 19:17:12 -04:00
|
|
|
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
|
2017-10-29 17:31:07 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|