brew/Library/Homebrew/cmd/autoremove.rb

52 lines
1.4 KiB
Ruby
Raw Normal View History

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>]
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
max_named 0
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