2020-11-04 18:53:03 -05:00
|
|
|
# typed: false
|
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
|
|
|
|
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)
|
2020-11-05 12:27:22 -05:00
|
|
|
removable_formulae = Formula.installed_formulae_with_no_dependents(formulae).reject do |f|
|
2020-11-04 18:53:03 -05:00
|
|
|
Tab.for_keg(f.any_installed_keg).installed_on_request
|
2020-09-10 19:45:02 +02:00
|
|
|
end
|
|
|
|
|
2020-11-04 18:53:03 -05:00
|
|
|
removable_formulae += get_removable_formulae(formulae - removable_formulae) if removable_formulae.any?
|
|
|
|
|
2020-09-10 19:45:02 +02:00
|
|
|
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
|
|
|
|
2020-11-05 12:27:22 -05:00
|
|
|
intent = args.dry_run? ? "could" : "will"
|
|
|
|
oh1 "Formulae that #{intent} be removed"
|
2020-09-10 19:45:02 +02:00
|
|
|
puts formulae_names
|
2020-11-04 18:53:03 -05:00
|
|
|
|
2020-09-10 19:45:02 +02:00
|
|
|
return if args.dry_run?
|
|
|
|
|
2020-11-05 12:27:22 -05:00
|
|
|
puts
|
2020-11-04 15:59:50 -05:00
|
|
|
kegs_by_rack = removable_formulae.map(&:any_installed_keg).group_by(&:rack)
|
2020-11-05 12:27:22 -05:00
|
|
|
Uninstall.uninstall_kegs(kegs_by_rack)
|
2020-09-10 19:45:02 +02:00
|
|
|
end
|
|
|
|
end
|