brew/Library/Homebrew/cmd/autoremove.rb

55 lines
1.2 KiB
Ruby
Raw Normal View History

2020-09-10 19:45:02 +02:00
# frozen_string_literal: true
require "formula"
require "cli/parser"
2020-11-04 15:59:50 -05:00
require "uninstall"
2020-09-10 19:45:02 +02:00
module Homebrew
2020-11-04 15:59:50 -05:00
extend Uninstall
2020-09-10 19:45:02 +02:00
module_function
def autoremove_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`autoremove` [<options>]
Remove packages that weren't installed on request and are no longer needed.
EOS
switch "-n", "--dry-run",
description: "Just print what would be removed."
named 0
end
end
2020-11-04 15:59:50 -05:00
def get_removable_formulae(formulae)
removable_formulae = Formula.installed_non_deps(formulae).reject {
|f| Tab.for_keg(f.any_installed_keg).installed_on_request
}
2020-09-10 19:45:02 +02:00
2020-11-04 15:59:50 -05:00
if removable_formulae.any?
removable_formulae += get_removable_formulae(formulae - removable_formulae)
2020-09-10 19:45:02 +02:00
end
removable_formulae
end
def autoremove
args = autoremove_args.parse
2020-11-04 15:59:50 -05:00
removable_formulae = get_removable_formulae(Formula.installed)
2020-09-10 19:45:02 +02:00
return if removable_formulae.blank?
2020-11-04 15:59:50 -05:00
formulae_names = removable_formulae.map(&:full_name).sort
2020-09-10 19:45:02 +02:00
oh1 "Formulae that could be removed"
puts formulae_names
2020-11-04 15:59:50 -05:00
2020-09-10 19:45:02 +02:00
return if args.dry_run?
2020-11-04 15:59:50 -05:00
kegs_by_rack = removable_formulae.map(&:any_installed_keg).group_by(&:rack)
uninstall_kegs(kegs_by_rack)
2020-09-10 19:45:02 +02:00
end
end