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.
This commit is contained in:
parent
f8ae5f3bc4
commit
74f1eca14c
@ -20,37 +20,9 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
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
|
def autoremove
|
||||||
args = autoremove_args.parse
|
args = autoremove_args.parse
|
||||||
|
|
||||||
removable_formulae = get_removable_formulae(Formula.installed)
|
Uninstall.autoremove_kegs(Formula.removable_formulae, dry_run: args.dry_run?)
|
||||||
|
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -81,6 +81,6 @@ module Homebrew
|
|||||||
|
|
||||||
return unless Homebrew::EnvConfig.uninstall_autoremove?
|
return unless Homebrew::EnvConfig.uninstall_autoremove?
|
||||||
|
|
||||||
system HOMEBREW_BREW_FILE, "autoremove"
|
Uninstall.autoremove_kegs(Formula.removable_formulae)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1706,7 +1706,7 @@ class Formula
|
|||||||
end.uniq(&:name)
|
end.uniq(&:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
# An array of all installed {Formula} without dependents
|
# An array of all installed {Formula} without {Formula} dependents
|
||||||
# @private
|
# @private
|
||||||
def self.installed_formulae_with_no_dependents(formulae = installed)
|
def self.installed_formulae_with_no_dependents(formulae = installed)
|
||||||
return [] if formulae.blank?
|
return [] if formulae.blank?
|
||||||
@ -1720,6 +1720,37 @@ class Formula
|
|||||||
installed.select { |f| f.installed_alias_path == alias_path }
|
installed.select { |f| f.installed_alias_path == alias_path }
|
||||||
end
|
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}
|
# an array of all alias files of core {Formula}
|
||||||
# @private
|
# @private
|
||||||
def self.core_alias_files
|
def self.core_alias_files
|
||||||
|
|||||||
@ -97,6 +97,20 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
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: [])
|
def handle_unsatisfied_dependents(kegs_by_rack, casks: [], ignore_dependencies: false, named_args: [])
|
||||||
return if ignore_dependencies
|
return if ignore_dependencies
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user