cmd: add autoremove command

This commit is contained in:
Dawid Dziurla 2020-09-10 19:45:02 +02:00 committed by Tie
parent 4127a7b624
commit 0fa417706a
6 changed files with 77 additions and 0 deletions

View File

@ -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` [<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
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

View File

@ -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'

View File

@ -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

View File

@ -12,6 +12,7 @@
abv
analytics
audit
autoremove
bottle
bump
bump-cask-pr

View File

@ -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.

View File

@ -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\.
.