2020-11-25 17:03:23 +01:00
|
|
|
# typed: true
|
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>]
|
|
|
|
|
2020-11-06 10:36:21 -05:00
|
|
|
Uninstall formulae that were only installed as a dependency of another formula and are now no longer needed.
|
2020-09-10 19:45:02 +02:00
|
|
|
EOS
|
|
|
|
switch "-n", "--dry-run",
|
2020-11-06 10:36:21 -05:00
|
|
|
description: "List what would be uninstalled, but do not actually uninstall anything."
|
2020-11-12 10:40:41 -05:00
|
|
|
|
2021-01-10 14:26:40 -05:00
|
|
|
named_args :none
|
2020-09-10 19:45:02 +02:00
|
|
|
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-06 10:36:21 -05:00
|
|
|
removable_formulae += get_removable_formulae(formulae - removable_formulae) if removable_formulae.present?
|
2020-11-04 18:53:03 -05:00
|
|
|
|
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-06 10:36:21 -05:00
|
|
|
verb = args.dry_run? ? "Would uninstall" : "Uninstalling"
|
|
|
|
oh1 "#{verb} #{formulae_names.count} unneeded #{"formula".pluralize(formulae_names.count)}:"
|
|
|
|
puts formulae_names.join("\n")
|
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)
|
2020-11-05 12:27:22 -05:00
|
|
|
Uninstall.uninstall_kegs(kegs_by_rack)
|
2020-09-10 19:45:02 +02:00
|
|
|
end
|
|
|
|
end
|