From 74f1eca14c9b2e353eb0c32e35a005dfbb333a9b Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Tue, 12 Jul 2022 00:28:25 -0700 Subject: [PATCH] autoremove.rb: moved logic out into formula.rb, and uninstall.rb This allows us to autoremove formulae in the autoremove and uninstall commands without having to shell out to brew. --- Library/Homebrew/cmd/autoremove.rb | 30 +-------------------------- Library/Homebrew/cmd/uninstall.rb | 2 +- Library/Homebrew/formula.rb | 33 +++++++++++++++++++++++++++++- Library/Homebrew/uninstall.rb | 14 +++++++++++++ 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Library/Homebrew/cmd/autoremove.rb b/Library/Homebrew/cmd/autoremove.rb index a80cb4ae49..c1a9b65cec 100644 --- a/Library/Homebrew/cmd/autoremove.rb +++ b/Library/Homebrew/cmd/autoremove.rb @@ -20,37 +20,9 @@ module Homebrew end end - def get_removable_formulae(formulae) - removable_formulae = Formula.installed_formulae_with_no_dependents(formulae).reject do |f| - Tab.for_keg(f.any_installed_keg).installed_on_request - end - - removable_formulae += get_removable_formulae(formulae - removable_formulae) if removable_formulae.present? - - removable_formulae - end - def autoremove args = autoremove_args.parse - removable_formulae = get_removable_formulae(Formula.installed) - - if (casks = Cask::Caskroom.casks.presence) - removable_formulae -= casks.flat_map { |cask| cask.depends_on[:formula] } - .compact - .map { |f| Formula[f] } - .flat_map { |f| [f, *f.runtime_formula_dependencies].compact } - end - return if removable_formulae.blank? - - formulae_names = removable_formulae.map(&:full_name).sort - - verb = args.dry_run? ? "Would uninstall" : "Uninstalling" - oh1 "#{verb} #{formulae_names.count} unneeded #{"formula".pluralize(formulae_names.count)}:" - puts formulae_names.join("\n") - return if args.dry_run? - - kegs_by_rack = removable_formulae.map(&:any_installed_keg).group_by(&:rack) - Uninstall.uninstall_kegs(kegs_by_rack) + Uninstall.autoremove_kegs(Formula.removable_formulae, dry_run: args.dry_run?) end end diff --git a/Library/Homebrew/cmd/uninstall.rb b/Library/Homebrew/cmd/uninstall.rb index e8e1bb386c..fffa54a69f 100644 --- a/Library/Homebrew/cmd/uninstall.rb +++ b/Library/Homebrew/cmd/uninstall.rb @@ -81,6 +81,6 @@ module Homebrew return unless Homebrew::EnvConfig.uninstall_autoremove? - system HOMEBREW_BREW_FILE, "autoremove" + Uninstall.autoremove_kegs(Formula.removable_formulae) end end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 17488e0ed1..d367e157dd 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1706,7 +1706,7 @@ class Formula end.uniq(&:name) end - # An array of all installed {Formula} without dependents + # An array of all installed {Formula} without {Formula} dependents # @private def self.installed_formulae_with_no_dependents(formulae = installed) return [] if formulae.blank? @@ -1720,6 +1720,37 @@ class Formula installed.select { |f| f.installed_alias_path == alias_path } end + # An array of all installed {Formula} with {Cask} dependents. + # @private + def self.installed_formulae_with_cask_dependents + Cask::Caskroom.casks + .flat_map { |cask| cask.depends_on[:formula] } + .compact + .map { |f| Formula[f] } + .flat_map { |f| [f, *f.runtime_formula_dependencies].compact } + end + + # An array of all installed {Formula} without {Formula} or {Cask} dependents + # that weren't installed on request. + # @private + def self.removable_formulae + formulae = installed + all_removable_formulae = T.let([], T::Array[Formula]) + + loop do + removable_formulae = installed_formulae_with_no_dependents(formulae).reject do |f| + Tab.for_keg(f.any_installed_keg).installed_on_request + end + + break if removable_formulae.blank? + + all_removable_formulae += removable_formulae + formulae -= removable_formulae + end + + all_removable_formulae - installed_formulae_with_cask_dependents + end + # an array of all alias files of core {Formula} # @private def self.core_alias_files diff --git a/Library/Homebrew/uninstall.rb b/Library/Homebrew/uninstall.rb index 1d9a1585e4..ee00fb849c 100644 --- a/Library/Homebrew/uninstall.rb +++ b/Library/Homebrew/uninstall.rb @@ -97,6 +97,20 @@ module Homebrew end end + def autoremove_kegs(removable_formulae, dry_run: false) + return if removable_formulae.blank? + + formulae_names = removable_formulae.map(&:full_name).sort + + verb = dry_run ? "Would uninstall" : "Uninstalling" + oh1 "#{verb} #{formulae_names.count} unneeded #{"formula".pluralize(formulae_names.count)}:" + puts formulae_names.join("\n") + return if dry_run + + kegs_by_rack = removable_formulae.map(&:any_installed_keg).group_by(&:rack) + Uninstall.uninstall_kegs(kegs_by_rack) + end + def handle_unsatisfied_dependents(kegs_by_rack, casks: [], ignore_dependencies: false, named_args: []) return if ignore_dependencies