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
2021-01-25 09:18:10 +00:00
# Cask implementation of the `brew upgrade` command.
2020-08-19 10:34:07 +02:00
#
# @api private
2017-10-29 17:31:07 -03:00
class Upgrade < AbstractCommand
2020-10-20 12:03:48 +02:00
extend T :: Sig
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`. " ,
} ] ,
2021-07-01 23:01:22 +10:00
[ :switch , " --greedy-latest " , {
description : " Also include casks with `version :latest`. " ,
} ] ,
[ :switch , " --greedy-auto-updates " , {
description : " Also include casks with `auto_updates true`. " ,
2021-06-30 11:21:24 +10:00
} ] ,
2020-09-28 02:34:14 +02:00
] . freeze
2020-11-16 01:52:57 +01:00
sig { returns ( Homebrew :: CLI :: Parser ) }
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
2020-10-20 12:03:48 +02:00
sig { void }
2017-10-29 17:31:07 -03:00
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 ,
2021-07-01 23:02:15 +10:00
force : args . force? ,
greedy : args . greedy? ,
2021-07-03 11:57:32 +10:00
greedy_latest : args . greedy_latest? ,
greedy_auto_updates : args . greedy_auto_updates? ,
2021-07-01 23:02:15 +10:00
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
2021-01-17 22:45:55 -08:00
sig {
2020-11-16 01:52:57 +01:00
params (
2021-07-01 23:02:15 +10:00
casks : Cask ,
args : Homebrew :: CLI :: Args ,
force : T . nilable ( T :: Boolean ) ,
greedy : T . nilable ( T :: Boolean ) ,
greedy_latest : T . nilable ( T :: Boolean ) ,
2021-07-03 11:57:32 +10:00
greedy_auto_updates : T . nilable ( T :: Boolean ) ,
2021-07-01 23:02:15 +10:00
dry_run : T . nilable ( T :: Boolean ) ,
skip_cask_deps : T . nilable ( T :: Boolean ) ,
verbose : T . nilable ( T :: Boolean ) ,
binaries : T . nilable ( T :: Boolean ) ,
quarantine : T . nilable ( T :: Boolean ) ,
require_sha : T . nilable ( T :: Boolean ) ,
2021-01-11 11:43:11 -08:00
) . returns ( T :: Boolean )
2021-01-17 22:45:55 -08:00
}
2020-08-05 11:50:01 +02:00
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 ,
2021-07-01 23:01:22 +10:00
greedy_latest : false ,
greedy_auto_updates : false ,
2020-08-05 12:12:50 -04:00
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 |
2021-07-01 23:02:15 +10:00
cask . outdated? ( greedy : greedy , greedy_latest : args . greedy_latest? ,
greedy_auto_updates : args . greedy_auto_updates? )
2017-11-24 01:21:30 +00:00
end
2020-08-05 11:50:01 +02:00
else
casks . select do | cask |
2021-01-07 13:49:05 -08:00
raise CaskNotInstalledError , cask if ! 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
2021-05-12 23:46:50 +09:00
manual_installer_casks = outdated_casks . select do | cask |
cask . artifacts . any? ( Artifact :: Installer :: ManualInstaller )
end
2021-05-14 12:56:57 +09:00
if manual_installer_casks . present?
2021-05-15 21:24:24 +09:00
count = manual_installer_casks . count
ofail " Not upgrading #{ count } `installer manual` #{ " cask " . pluralize ( count ) } . "
2021-05-14 12:56:57 +09:00
puts manual_installer_casks . map ( & :to_s )
2021-05-15 21:24:24 +09:00
outdated_casks -= manual_installer_casks
2021-05-14 12:56:57 +09:00
end
2021-01-11 11:43:11 -08:00
return false if outdated_casks . empty?
2018-09-03 22:08:14 -06:00
2020-11-16 01:48:51 +01:00
if casks . empty? && ! greedy
2021-07-03 11:57:32 +10:00
ohai " Casks with 'auto_updates true' or 'version :latest' will not be upgraded; pass `--greedy` to upgrade them. " if ! args . greedy_auto_updates? && ! args . greedy_latest?
ohai " Casks with 'version :latest' will not be upgraded; pass `--greedy-latest` to upgrade them. " if args . greedy_auto_updates? && ! args . greedy_latest?
ohai " Casks with 'auto_updates true' will not be upgraded; pass `--greedy-auto-updates` to upgrade them. " if ! args . greedy_auto_updates? && args . greedy_latest?
2020-11-16 01:48:51 +01:00
end
2020-08-05 11:50:01 +02:00
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 " )
2021-01-11 11:43:11 -08:00
return true 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
2021-01-11 11:43:11 -08:00
return true if caught_exceptions . empty?
2019-04-03 19:17:12 -04:00
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