Skip to content

Commit

Permalink
cmd/list: support listing formulae installed on request or automatically
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ZhongRuoyu committed Apr 22, 2024
1 parent 8810b7f commit 1731ac7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Library/Homebrew/cmd/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "cli/parser"
require "cask/list"
require "system_command"
require "tab"

module Homebrew
module Cmd
Expand Down Expand Up @@ -36,6 +37,11 @@ class List < AbstractCommand
switch "--pinned",
description: "List only pinned formulae, or only the specified (pinned) " \
"formulae if <formula> 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. " \
Expand All @@ -54,11 +60,17 @@ class List < AbstractCommand
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

Expand Down Expand Up @@ -91,6 +103,23 @@ def run
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

Expand Down
38 changes: 38 additions & 0 deletions Library/Homebrew/test/cmd/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

require "cmd/list"
require "cmd/shared_examples/args_parse"
require "formula"
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"
Expand All @@ -18,4 +35,25 @@
.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

0 comments on commit 1731ac7

Please sign in to comment.