diff --git a/Library/Homebrew/cmd/autoremove.rb b/Library/Homebrew/cmd/autoremove.rb new file mode 100644 index 0000000000..f02712e94b --- /dev/null +++ b/Library/Homebrew/cmd/autoremove.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require "formula" +require "cli/parser" + +module Homebrew + module_function + + def autoremove_args + Homebrew::CLI::Parser.new do + usage_banner <<~EOS + `autoremove` [] + + 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 + + def get_removable_formulae(installed_formulae) + removable_formulae = [] + + installed_formulae.each do |formula| + # Reject formulae installed on request. + next if Tab.for_keg(formula.any_installed_keg).installed_on_request + # Reject formulae which are needed at runtime by other formulae. + next if installed_formulae.flat_map(&:runtime_formula_dependencies).include?(formula) + + removable_formulae << installed_formulae.delete(formula) + removable_formulae += get_removable_formulae(installed_formulae) + end + + removable_formulae + end + + def autoremove + args = autoremove_args.parse + + removable_formulae = get_removable_formulae(Formula.installed.sort) + + return if removable_formulae.blank? + + formulae_names = removable_formulae.map(&:full_name) + + oh1 "Formulae that could be removed" + puts formulae_names + + return if args.dry_run? + + system HOMEBREW_BREW_FILE, "rm", *formulae_names + end +end diff --git a/Library/Homebrew/test/.rubocop_todo.yml b/Library/Homebrew/test/.rubocop_todo.yml index 3474b71ed0..e3f0383a8a 100644 --- a/Library/Homebrew/test/.rubocop_todo.yml +++ b/Library/Homebrew/test/.rubocop_todo.yml @@ -33,6 +33,7 @@ RSpec/MultipleDescribes: - 'cmd/--repository_spec.rb' - 'cmd/--version_spec.rb' - 'cmd/analytics_spec.rb' + - 'cmd/autoremove_spec.rb' - 'cmd/cleanup_spec.rb' - 'cmd/commands_spec.rb' - 'cmd/config_spec.rb' diff --git a/Library/Homebrew/test/cmd/autoremove_spec.rb b/Library/Homebrew/test/cmd/autoremove_spec.rb new file mode 100644 index 0000000000..efbe93ecfb --- /dev/null +++ b/Library/Homebrew/test/cmd/autoremove_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require "cmd/shared_examples/args_parse" + +describe "Homebrew.autoremove_args" do + it_behaves_like "parseable arguments" +end diff --git a/completions/internal_commands_list.txt b/completions/internal_commands_list.txt index cd9c60d4b0..0d277a1200 100644 --- a/completions/internal_commands_list.txt +++ b/completions/internal_commands_list.txt @@ -12,6 +12,7 @@ abv analytics audit +autoremove bottle bump bump-cask-pr diff --git a/docs/Manpage.md b/docs/Manpage.md index 318806b29d..9fff49ae0d 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -56,6 +56,13 @@ Turn Homebrew's analytics on or off respectively. `brew analytics regenerate-uuid`: Regenerate the UUID used for Homebrew's analytics. +### `autoremove` [*`options`*] + +Remove packages that weren't installed on request and are no longer needed. + +* `-n`, `--dry-run`: + Just print what would be removed. + ### `cask` *`command`* [*`options`*] [*`cask`*] Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries. diff --git a/manpages/brew.1 b/manpages/brew.1 index 3ea3804113..537ebab945 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -53,6 +53,13 @@ Control Homebrew\'s anonymous aggregate user behaviour analytics\. Read more at \fBbrew analytics regenerate\-uuid\fR Regenerate the UUID used for Homebrew\'s analytics\. . +.SS "\fBautoremove\fR [\fIoptions\fR]" +Remove packages that weren\'t installed on request and are no longer needed\. +. +.TP +\fB\-n\fR, \fB\-\-dry\-run\fR +Just print what would be removed\. +. .SS "\fBcask\fR \fIcommand\fR [\fIoptions\fR] [\fIcask\fR]" Homebrew Cask provides a friendly CLI workflow for the administration of macOS applications distributed as binaries\. .