Merge pull request #8214 from MLH-Fellowship/fix-cask-issue-87045

Fix issue where the cask_opts env variable was not respected when running upgrade, reinstall, uninstall
This commit is contained in:
Markus Reiter 2020-08-05 21:09:38 +02:00 committed by GitHub
commit fb9b8f4c96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 170 additions and 43 deletions

View File

@ -4,13 +4,39 @@ module Cask
class Cmd class Cmd
class Reinstall < Install class Reinstall < Install
def run def run
self.class.reinstall_casks(
*casks,
binaries: binaries?,
verbose: verbose?,
force: force?,
skip_cask_deps: skip_cask_deps?,
require_sha: require_sha?,
quarantine: quarantine?,
)
end
def self.reinstall_casks(
*casks,
verbose: false,
force: false,
skip_cask_deps: false,
binaries: nil,
require_sha: nil,
quarantine: nil
)
# TODO: Handle this in `CLI::Parser`.
binaries = Homebrew::EnvConfig.cask_opts_binaries? if binaries.nil?
quarantine = Homebrew::EnvConfig.cask_opts_quarantine? if quarantine.nil?
require_sha = Homebrew::EnvConfig.cask_opts_require_sha? if require_sha.nil?
casks.each do |cask| casks.each do |cask|
Installer.new(cask, binaries: binaries?, Installer.new(cask,
verbose: verbose?, binaries: binaries,
force: force?, verbose: verbose,
skip_cask_deps: skip_cask_deps?, force: force,
require_sha: require_sha?, skip_cask_deps: skip_cask_deps,
quarantine: quarantine?).reinstall require_sha: require_sha,
quarantine: quarantine).reinstall
end end
end end

View File

@ -11,17 +11,28 @@ module Cask
end end
def run def run
self.class.uninstall_casks(
*casks,
binaries: binaries?,
verbose: verbose?,
force: force?,
)
end
def self.uninstall_casks(*casks, verbose: false, force: false, binaries: nil)
binaries = Homebrew::EnvConfig.cask_opts_binaries? if binaries.nil?
casks.each do |cask| casks.each do |cask|
odebug "Uninstalling Cask #{cask}" odebug "Uninstalling Cask #{cask}"
raise CaskNotInstalledError, cask unless cask.installed? || force? raise CaskNotInstalledError, cask unless cask.installed? || force
if cask.installed? && !cask.installed_caskfile.nil? if cask.installed? && !cask.installed_caskfile.nil?
# use the same cask file that was used for installation, if possible # use the same cask file that was used for installation, if possible
cask = CaskLoader.load(cask.installed_caskfile) if cask.installed_caskfile.exist? cask = CaskLoader.load(cask.installed_caskfile) if cask.installed_caskfile.exist?
end end
Installer.new(cask, binaries: binaries?, verbose: verbose?, force: force?).uninstall Installer.new(cask, binaries: binaries, verbose: verbose, force: force).uninstall
next if (versions = cask.versions).empty? next if (versions = cask.versions).empty?

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require "env_config"
require "cask/config" require "cask/config"
module Cask module Cask
@ -17,14 +18,45 @@ module Cask
end end
def run def run
outdated_casks = casks(alternative: lambda { self.class.upgrade_casks(
Caskroom.casks.select do |cask| *casks,
cask.outdated?(greedy?) force: force?,
end greedy: greedy?,
}).select do |cask| dry_run: dry_run?,
raise CaskNotInstalledError, cask unless cask.installed? || force? binaries: binaries?,
quarantine: quarantine?,
require_sha: require_sha?,
skip_cask_deps: skip_cask_deps?,
verbose: verbose?,
)
end
cask.outdated?(true) def self.upgrade_casks(
*casks,
force: false,
greedy: false,
dry_run: false,
skip_cask_deps: false,
verbose: false,
binaries: nil,
quarantine: nil,
require_sha: nil
)
# TODO: Handle this in `CLI::Parser`.
binaries = Homebrew::EnvConfig.cask_opts_binaries? if binaries.nil?
quarantine = Homebrew::EnvConfig.cask_opts_quarantine? if quarantine.nil?
require_sha = Homebrew::EnvConfig.cask_opts_require_sha? if require_sha.nil?
outdated_casks = if casks.empty?
Caskroom.casks.select do |cask|
cask.outdated?(greedy)
end
else
casks.select do |cask|
raise CaskNotInstalledError, cask unless cask.installed? || force
cask.outdated?(true)
end
end end
if outdated_casks.empty? if outdated_casks.empty?
@ -32,9 +64,11 @@ module Cask
return return
end end
ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if args.empty? && !greedy? ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if casks.empty? && !greedy
verb = dry_run? ? "Would upgrade" : "Upgrading"
verb = dry_run ? "Would upgrade" : "Upgrading"
oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:" oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:"
caught_exceptions = [] caught_exceptions = []
upgradable_casks = outdated_casks.map { |c| [CaskLoader.load(c.installed_caskfile), c] } upgradable_casks = outdated_casks.map { |c| [CaskLoader.load(c.installed_caskfile), c] }
@ -42,10 +76,14 @@ module Cask
puts upgradable_casks puts upgradable_casks
.map { |(old_cask, new_cask)| "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}" } .map { |(old_cask, new_cask)| "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}" }
.join(", ") .join(", ")
return if dry_run? return if dry_run
upgradable_casks.each do |(old_cask, new_cask)| upgradable_casks.each do |(old_cask, new_cask)|
upgrade_cask(old_cask, new_cask) 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 rescue => e
caught_exceptions << e.exception("#{new_cask.full_name}: #{e}") caught_exceptions << e.exception("#{new_cask.full_name}: #{e}")
next next
@ -56,26 +94,29 @@ module Cask
raise caught_exceptions.first if caught_exceptions.count == 1 raise caught_exceptions.first if caught_exceptions.count == 1
end end
def upgrade_cask(old_cask, new_cask) def self.upgrade_cask(
old_cask, new_cask,
binaries:, force:, quarantine:, require_sha:, skip_cask_deps:, verbose:
)
odebug "Started upgrade process for Cask #{old_cask}" odebug "Started upgrade process for Cask #{old_cask}"
old_config = old_cask.config old_config = old_cask.config
old_cask_installer = old_cask_installer =
Installer.new(old_cask, binaries: binaries?, Installer.new(old_cask, binaries: binaries,
verbose: verbose?, verbose: verbose,
force: force?, force: force,
upgrade: true) upgrade: true)
new_cask.config = Config.global.merge(old_config) new_cask.config = Config.global.merge(old_config)
new_cask_installer = new_cask_installer =
Installer.new(new_cask, binaries: binaries?, Installer.new(new_cask, binaries: binaries,
verbose: verbose?, verbose: verbose,
force: force?, force: force,
skip_cask_deps: skip_cask_deps?, skip_cask_deps: skip_cask_deps,
require_sha: require_sha?, require_sha: require_sha,
upgrade: true, upgrade: true,
quarantine: quarantine?) quarantine: quarantine)
started_upgrade = false started_upgrade = false
new_artifacts_installed = false new_artifacts_installed = false

View File

@ -78,9 +78,14 @@ module Homebrew
return if casks.blank? return if casks.blank?
reinstall_cmd = Cask::Cmd::Reinstall.new(casks) Cask::Cmd::Reinstall.reinstall_casks(
reinstall_cmd.verbose = args.verbose? *casks,
reinstall_cmd.force = args.force? binaries: args.binaries?,
reinstall_cmd.run verbose: args.verbose?,
force: args.force?,
require_sha: args.require_sha?,
skip_cask_deps: args.skip_cask_deps?,
quarantine: args.quarantine?,
)
end end
end end

View File

@ -127,10 +127,12 @@ module Homebrew
return if casks.blank? return if casks.blank?
cask_uninstall = Cask::Cmd::Uninstall.new(casks) Cask::Cmd::Uninstall.uninstall_casks(
cask_uninstall.force = args.force? *casks,
cask_uninstall.verbose = args.verbose? binaries: args.binaries?,
cask_uninstall.run verbose: args.verbose?,
force: args.force?,
)
rescue MultipleVersionsInstalledError => e rescue MultipleVersionsInstalledError => e
ofail e ofail e
puts "Run `brew uninstall --force #{e.name}` to remove all versions." puts "Run `brew uninstall --force #{e.name}` to remove all versions."

View File

@ -133,10 +133,16 @@ module Homebrew
end end
def upgrade_outdated_casks(casks, args:) def upgrade_outdated_casks(casks, args:)
cask_upgrade = Cask::Cmd::Upgrade.new(casks) Cask::Cmd::Upgrade.upgrade_casks(
cask_upgrade.force = args.force? *casks,
cask_upgrade.dry_run = args.dry_run? force: args.force?,
cask_upgrade.greedy = args.greedy? greedy: args.greedy?,
cask_upgrade.run dry_run: args.dry_run?,
binaries: args.binaries?,
quarantine: args.quarantine?,
require_sha: args.require_sha?,
skip_cask_deps: args.skip_cask_deps?,
verbose: args.verbose?,
)
end end
end end

View File

@ -54,6 +54,9 @@ module Homebrew
"Linux: `$XDG_CACHE_HOME/Homebrew` or `$HOME/.cache/Homebrew`.", "Linux: `$XDG_CACHE_HOME/Homebrew` or `$HOME/.cache/Homebrew`.",
default: HOMEBREW_DEFAULT_CACHE, default: HOMEBREW_DEFAULT_CACHE,
}, },
HOMEBREW_CASK_OPTS: {
description: "Options which should be used for all `cask` commands.",
},
HOMEBREW_CLEANUP_MAX_AGE_DAYS: { HOMEBREW_CLEANUP_MAX_AGE_DAYS: {
description: "Cleanup all cached files older than this many days.", description: "Cleanup all cached files older than this many days.",
default: 120, default: 120,
@ -293,7 +296,7 @@ module Homebrew
end end
elsif hash[:default].present? elsif hash[:default].present?
# Needs a custom implementation. # Needs a custom implementation.
next if env == "HOMEBREW_MAKE_JOBS" next if ["HOMEBREW_MAKE_JOBS", "HOMEBREW_CASK_OPTS"].include?(env)
define_method(method_name) do define_method(method_name) do
ENV[env].presence || hash.fetch(:default).to_s ENV[env].presence || hash.fetch(:default).to_s
@ -315,5 +318,31 @@ module Homebrew
.call .call
.to_s .to_s
end end
def cask_opts
Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
end
def cask_opts_binaries?
cask_opts.reverse_each do |opt|
return true if opt == "--binaries"
return false if opt == "--no-binaries"
end
true
end
def cask_opts_quarantine?
cask_opts.reverse_each do |opt|
return true if opt == "--quarantine"
return false if opt == "--no-quarantine"
end
true
end
def cask_opts_require_sha?
cask_opts.include?("--require-sha")
end
end end
end end

View File

@ -1398,6 +1398,9 @@ Note that environment variables must have a value set to be detected. For exampl
*Default:* macOS: `$HOME/Library/Caches/Homebrew`, Linux: `$XDG_CACHE_HOME/Homebrew` or `$HOME/.cache/Homebrew`. *Default:* macOS: `$HOME/Library/Caches/Homebrew`, Linux: `$XDG_CACHE_HOME/Homebrew` or `$HOME/.cache/Homebrew`.
* `HOMEBREW_CASK_OPTS`:
Options which should be used for all `cask` commands.
* `HOMEBREW_CLEANUP_MAX_AGE_DAYS`: * `HOMEBREW_CLEANUP_MAX_AGE_DAYS`:
Cleanup all cached files older than this many days. Cleanup all cached files older than this many days.

View File

@ -1818,6 +1818,10 @@ Use the specified directory as the download cache\.
\fIDefault:\fR macOS: \fB$HOME/Library/Caches/Homebrew\fR, Linux: \fB$XDG_CACHE_HOME/Homebrew\fR or \fB$HOME/\.cache/Homebrew\fR\. \fIDefault:\fR macOS: \fB$HOME/Library/Caches/Homebrew\fR, Linux: \fB$XDG_CACHE_HOME/Homebrew\fR or \fB$HOME/\.cache/Homebrew\fR\.
. .
.TP .TP
\fBHOMEBREW_CASK_OPTS\fR
Options which should be used for all \fBcask\fR commands\.
.
.TP
\fBHOMEBREW_CLEANUP_MAX_AGE_DAYS\fR \fBHOMEBREW_CLEANUP_MAX_AGE_DAYS\fR
Cleanup all cached files older than this many days\. Cleanup all cached files older than this many days\.
. .