From 868c15fe4f5020182cd46e915fed631e3d534d9e Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Mon, 22 Apr 2024 11:58:07 +0800 Subject: [PATCH] cmd/list: support listing formulae installed on request or automatically Sample usage: $ brew ls --manual gcc llvm [...] $ brew ls --auto grpc protobuf [...] $ brew ls --manual --auto gcc: manual grpc: auto llvm: manual protobuf: auto [...] Resolves #17117. --- Library/Homebrew/cmd/list.rb | 31 ++++++++++++++++++++- Library/Homebrew/test/cmd/list_spec.rb | 37 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb index 619bc49feb..e1e2e8b01c 100644 --- a/Library/Homebrew/cmd/list.rb +++ b/Library/Homebrew/cmd/list.rb @@ -7,6 +7,7 @@ require "formula" require "cli/parser" require "cask/list" require "system_command" +require "tab" module Homebrew module Cmd @@ -36,6 +37,11 @@ module Homebrew switch "--pinned", description: "List only pinned formulae, or only the specified (pinned) " \ "formulae if are provided. See also `pin`, `unpin`." + switch "--manual", "--installed-on-request", + description: "List the formulae installed on request." + switch "--auto", "--installed-as-dependency", + description: "List the formulae installed automatically." + # passed through to ls switch "-1", description: "Force output to be one entry per line. " \ @@ -54,11 +60,17 @@ module Homebrew conflicts "--pinned", "--cask" conflicts "--multiple", "--cask" conflicts "--pinned", "--multiple" + ["--manual", "--auto"].each do |flag| + conflicts "--cask", flag + conflicts "--versions", flag + conflicts "--pinned", flag + end ["-1", "-l", "-r", "-t"].each do |flag| conflicts "--versions", flag conflicts "--pinned", flag end - ["--versions", "--pinned", "-l", "-r", "-t"].each do |flag| + ["--versions", "--pinned", "--manual", "--auto", + "-l", "-r", "-t"].each do |flag| conflicts "--full-name", flag end @@ -91,6 +103,23 @@ module Homebrew elsif args.versions? filtered_list unless args.cask? list_casks if args.cask? || (!args.formula? && !args.multiple? && args.no_named?) + elsif args.manual? || args.auto? + unless args.no_named? + raise UsageError, + "Cannot use `--manual` or `--auto` with formula arguments." + end + + Formula.installed.sort.each do |formula| + tab = Tab.for_formula(formula) + + if args.manual? && args.auto? + status = tab.installed_on_request ? "manual" : "auto" + puts "#{formula.name}: #{status}" + elsif (args.manual? && tab.installed_on_request) || + (args.auto? && !tab.installed_on_request) + puts formula.name + end + end elsif args.no_named? ENV["CLICOLOR"] = nil diff --git a/Library/Homebrew/test/cmd/list_spec.rb b/Library/Homebrew/test/cmd/list_spec.rb index b5b4a52902..3ca3e9a15a 100644 --- a/Library/Homebrew/test/cmd/list_spec.rb +++ b/Library/Homebrew/test/cmd/list_spec.rb @@ -2,8 +2,24 @@ require "cmd/list" require "cmd/shared_examples/args_parse" +require "tab" RSpec.describe Homebrew::Cmd::List do + def setup_installation(formula_name, installed_on_request:) + setup_test_formula(formula_name) + + keg_dir = HOMEBREW_CELLAR/formula_name/"1.0" + keg_dir.mkpath + + tab = Tab.new( + "installed_on_request" => installed_on_request, + "tabfile" => keg_dir/Tab::FILENAME, + ) + tab.write + + keg_dir + end + let(:formulae) { %w[bar foo qux] } it_behaves_like "parseable arguments" @@ -18,4 +34,25 @@ RSpec.describe Homebrew::Cmd::List do .and not_to_output.to_stderr .and be_a_success end + + it "lists the formulae installed on request or automatically", + :integration_test do + setup_installation "foo", installed_on_request: true + setup_installation "bar", installed_on_request: false + + expect { brew "list", "--manual" } + .to be_a_success + .and output("foo\n").to_stdout + .and not_to_output.to_stderr + + expect { brew "list", "--auto" } + .to be_a_success + .and output("bar\n").to_stdout + .and not_to_output.to_stderr + + expect { brew "list", "--manual", "--auto" } + .to be_a_success + .and output("bar: auto\nfoo: manual\n").to_stdout + .and not_to_output.to_stderr + end end