From e40eece17bdf83b6d93fc8fff36f94938db22cd3 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 28 Sep 2020 02:10:17 +0200 Subject: [PATCH] Add cask options to `brew reinstall`. --- Library/Homebrew/cli/named_args.rb | 65 +++++++++------------ Library/Homebrew/cmd/reinstall.rb | 91 +++++++++++++++++++++--------- 2 files changed, 91 insertions(+), 65 deletions(-) diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index e82c911175..e5a2342b8b 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -23,21 +23,33 @@ module Homebrew super(@args) end + def to_casks + @to_casks ||= to_formulae_and_casks(only: :cask).freeze + end + def to_formulae @to_formulae ||= to_formulae_and_casks(only: :formula).freeze end - def to_formulae_and_casks(only: nil) + def to_formulae_and_casks(only: nil, method: nil) @to_formulae_and_casks ||= {} @to_formulae_and_casks[only] ||= begin - to_objects(only: only).reject { |o| o.is_a?(Tap) }.freeze + to_objects(only: only, method: method).reject { |o| o.is_a?(Tap) }.freeze end end - def load_formula_or_cask(name, only: nil) + def load_formula_or_cask(name, only: nil, method: nil) if only != :cask begin - formula = Formulary.factory(name, spec, force_bottle: @force_bottle, flags: @flags) + formula = case method + when nil, :factory + Formulary.factory(name, *spec, force_bottle: @force_bottle, flags: @flags) + when :resolve + Formulary.resolve(name, spec: spec, force_bottle: @force_bottle, flags: @flags) + else + raise + end + warn_if_cask_conflicts(name, "formula") unless only == :formula return formula rescue FormulaUnavailableError => e @@ -47,7 +59,7 @@ module Homebrew if only != :formula begin - return Cask::CaskLoader.load(name) + return Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent)) rescue Cask::CaskUnavailableError raise e if only == :cask end @@ -58,46 +70,30 @@ module Homebrew private :load_formula_or_cask def resolve_formula(name) - Formulary.resolve(name, spec: spec(nil), force_bottle: @force_bottle, flags: @flags) + Formulary.resolve(name, spec: spec, force_bottle: @force_bottle, flags: @flags) end private :resolve_formula def to_resolved_formulae - @to_resolved_formulae ||= (downcased_unique_named - homebrew_tap_cask_names).map do |name| - resolve_formula(name) - end.uniq(&:name).freeze + @to_resolved_formulae ||= to_formulae_and_casks(only: :formula, method: :resolve) + .freeze end def to_resolved_formulae_to_casks - @to_resolved_formulae_to_casks ||= begin - resolved_formulae = [] - casks = [] - - downcased_unique_named.each do |name| - resolved_formulae << resolve_formula(name) - - warn_if_cask_conflicts(name, "formula") - rescue FormulaUnavailableError - begin - casks << Cask::CaskLoader.load(name) - rescue Cask::CaskUnavailableError - raise "No available formula or cask with the name \"#{name}\"" - end - end - - [resolved_formulae.freeze, casks.freeze].freeze - end + @to_resolved_formulae_to_casks ||= to_formulae_and_casks(method: :resolve) + .partition { |o| o.is_a?(Formula) } + .map(&:freeze).freeze end # Convert named arguments to `Tap`, `Formula` or `Cask` objects. # If both a formula and cask exist with the same name, returns the # formula and prints a warning unless `only` is specified. - def to_objects(only: nil) + def to_objects(only: nil, method: nil) @to_objects ||= {} @to_objects[only] ||= downcased_unique_named.flat_map do |name| next Tap.fetch(name) if only == :tap || (only.nil? && name.count("/") == 1 && !name.start_with?("./", "/")) - load_formula_or_cask(name, only: only) + load_formula_or_cask(name, only: only, method: method) end.uniq.freeze end @@ -132,12 +128,6 @@ module Homebrew end.uniq.freeze end - def to_casks - @to_casks ||= downcased_unique_named - .map { |name| Cask::CaskLoader.load(name, config: Cask::Config.from_args(@parent)) } - .freeze - end - def to_kegs @to_kegs ||= downcased_unique_named.map do |name| resolve_keg name @@ -187,9 +177,10 @@ module Homebrew end.uniq end - def spec(default = :stable) - @override_spec || default + def spec + @override_spec end + private :spec def resolve_keg(name) raise UsageError if name.blank? diff --git a/Library/Homebrew/cmd/reinstall.rb b/Library/Homebrew/cmd/reinstall.rb index fbafbccf57..4aa8b0be3f 100644 --- a/Library/Homebrew/cmd/reinstall.rb +++ b/Library/Homebrew/cmd/reinstall.rb @@ -16,6 +16,39 @@ module Homebrew module_function def reinstall_args + cask_only_options = [ + [:switch, "--cask", "--casks", { + description: "Treat all named arguments as casks.", + }], + *Cask::Cmd::OPTIONS, + *Cask::Cmd::AbstractCommand::OPTIONS, + ] + + formula_only_options = [ + [:switch, "--formula", "--formulae", { + description: "Treat all named arguments as formulae.", + }], + [:switch, "-s", "--build-from-source", { + description: "Compile from source even if a bottle is available.", + }], + [:switch, "-i", "--interactive", { + description: "Download and patch , then open a shell. This allows the user to "\ + "run `./configure --help` and otherwise determine how to turn the software "\ + "package into a Homebrew package.", + }], + [:switch, "--force-bottle", { + description: "Install from a bottle if it exists for the current or newest version of "\ + "macOS, even if it would not normally be used for installation.", + }], + [:switch, "--keep-tmp", { + description: "Retain the temporary files created during installation.", + }], + [:switch, "--display-times", { + env: :display_install_times, + description: "Print install times for each formula at the end of the run.", + }], + ] + Homebrew::CLI::Parser.new do usage_banner <<~EOS `reinstall` [] | @@ -29,26 +62,23 @@ module Homebrew switch "-d", "--debug", description: "If brewing fails, open an interactive debugging session with access to IRB "\ "or a shell inside the temporary build directory." - switch "-s", "--build-from-source", - description: "Compile from source even if a bottle is available." - switch "-i", "--interactive", - description: "Download and patch , then open a shell. This allows the user to "\ - "run `./configure --help` and otherwise determine how to turn the software "\ - "package into a Homebrew package." - switch "--force-bottle", - description: "Install from a bottle if it exists for the current or newest version of "\ - "macOS, even if it would not normally be used for installation." - switch "--keep-tmp", - description: "Retain the temporary files created during installation." switch "-f", "--force", description: "Install without checking for previously installed keg-only or "\ "non-migrated versions." switch "-v", "--verbose", description: "Print the verification and postinstall steps." - switch "--display-times", - env: :display_install_times, - description: "Print install times for each formula at the end of the run." conflicts "--build-from-source", "--force-bottle" + + formula_only_options.each do |options| + send(*options) + conflicts "--cask", options[-2] + end + + cask_only_options.each do |options| + send(*options) + conflicts "--formula", options[-2] + end + formula_options min_named 1 end @@ -61,8 +91,13 @@ module Homebrew Install.perform_preinstall_checks - resolved_formulae, casks = args.named.to_resolved_formulae_to_casks - resolved_formulae.each do |f| + only = :cask if args.cask? && !args.formula? + only = :formula if !args.cask? && args.formula? + + formulae, casks = args.named.to_formulae_and_casks(only: only, method: :resolve) + .partition { |o| o.is_a?(Formula) } + + formulae.each do |f| if f.pinned? onoe "#{f.full_name} is pinned. You must unpin it to reinstall." next @@ -74,18 +109,18 @@ module Homebrew Upgrade.check_installed_dependents(args: args) + if casks.any? + Cask::Cmd::Reinstall.reinstall_casks( + *casks, + binaries: EnvConfig.cask_opts_binaries?, + verbose: args.verbose?, + force: args.force?, + require_sha: EnvConfig.cask_opts_require_sha?, + skip_cask_deps: args.skip_cask_deps?, + quarantine: EnvConfig.cask_opts_quarantine?, + ) + end + Homebrew.messages.display_messages(display_times: args.display_times?) - - return if casks.blank? - - Cask::Cmd::Reinstall.reinstall_casks( - *casks, - binaries: EnvConfig.cask_opts_binaries?, - verbose: args.verbose?, - force: args.force?, - require_sha: EnvConfig.cask_opts_require_sha?, - skip_cask_deps: args.skip_cask_deps?, - quarantine: EnvConfig.cask_opts_quarantine?, - ) end end