cmd/--prefix: add --installed flag

This changes the output for uninstalled formulae from Cellar prefix to empty string, so:
```sh
$ brew --prefix abcde python@3.9 tcl-tk
/usr/local/Cellar/abcde/2.9.3_1
/usr/local/opt/python@3.9
/usr/local/Cellar/tcl-tk/8.6.10
$ brew --prefix --installed abcde python@3.9 tcl-tk

/usr/local/opt/python@3.9

$
```
This commit is contained in:
Adrian Ho 2021-01-30 08:58:04 +08:00
parent 5181405d5c
commit 28a8409b6e
7 changed files with 52 additions and 6 deletions

View File

@ -23,6 +23,9 @@ module Homebrew
EOS EOS
switch "--unbrewed", switch "--unbrewed",
description: "List files in Homebrew's prefix not installed by Homebrew." description: "List files in Homebrew's prefix not installed by Homebrew."
switch "--installed",
description: "Outputs nothing and returns a failing status code if <formula> is not installed."
conflicts "--unbrewed", "--installed"
named_args :formula named_args :formula
end end
@ -31,6 +34,8 @@ module Homebrew
def __prefix def __prefix
args = __prefix_args.parse args = __prefix_args.parse
raise UsageError, "`--installed` requires a formula argument." if args.installed? && args.no_named?
if args.unbrewed? if args.unbrewed?
raise UsageError, "`--unbrewed` does not take a formula argument." unless args.no_named? raise UsageError, "`--unbrewed` does not take a formula argument." unless args.no_named?
@ -38,9 +43,27 @@ module Homebrew
elsif args.no_named? elsif args.no_named?
puts HOMEBREW_PREFIX puts HOMEBREW_PREFIX
else else
puts args.named.to_resolved_formulae.map { |f| formulae = args.named.to_resolved_formulae
f.opt_prefix.exist? ? f.opt_prefix : f.latest_installed_prefix prefixes = formulae.map do |f|
} if f.opt_prefix.exist?
f.opt_prefix
elsif args.installed?
nil
else
f.latest_installed_prefix
end
end.compact
puts prefixes
if args.installed?
missing_formulae = formulae.reject(&:optlinked?)
.map(&:name)
return if missing_formulae.blank?
raise NotAKegError, <<~EOS
The following formulae are not installed:
#{missing_formulae.join(" ")}
EOS
end
end end
end end

View File

@ -14,4 +14,18 @@ describe "brew --prefix", :integration_test do
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
end end
it "errors if the given Formula doesn't exist" do
expect { brew "--prefix", "--installed", "nonexistent" }
.to output(/No available formula/).to_stderr
.and not_to_output.to_stdout
.and be_a_failure
end
it "prints a warning with `--installed` if the given Formula is not installed" do
expect { brew "--prefix", "--installed", testball }
.to not_to_output.to_stdout
.and output(/testball/).to_stderr
.and be_a_failure
end
end end

View File

@ -202,6 +202,7 @@ _brew___prefix() {
__brewcomp " __brewcomp "
--debug --debug
--help --help
--installed
--quiet --quiet
--unbrewed --unbrewed
--verbose --verbose

View File

@ -248,6 +248,7 @@ __fish_brew_complete_arg '--env' -a '(__fish_brew_suggest_formulae_all)'
__fish_brew_complete_cmd '--prefix' 'Display Homebrew\'s install path' __fish_brew_complete_cmd '--prefix' 'Display Homebrew\'s install path'
__fish_brew_complete_arg '--prefix' -l debug -d 'Display any debugging information' __fish_brew_complete_arg '--prefix' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg '--prefix' -l help -d 'Show this message' __fish_brew_complete_arg '--prefix' -l help -d 'Show this message'
__fish_brew_complete_arg '--prefix' -l installed -d 'Outputs nothing and returns a failing status code if formula is not installed'
__fish_brew_complete_arg '--prefix' -l quiet -d 'Make some output more quiet' __fish_brew_complete_arg '--prefix' -l quiet -d 'Make some output more quiet'
__fish_brew_complete_arg '--prefix' -l unbrewed -d 'List files in Homebrew\'s prefix not installed by Homebrew' __fish_brew_complete_arg '--prefix' -l unbrewed -d 'List files in Homebrew\'s prefix not installed by Homebrew'
__fish_brew_complete_arg '--prefix' -l verbose -d 'Make some output more verbose' __fish_brew_complete_arg '--prefix' -l verbose -d 'Make some output more verbose'

View File

@ -312,8 +312,9 @@ _brew___prefix() {
_arguments \ _arguments \
'--debug[Display any debugging information]' \ '--debug[Display any debugging information]' \
'--help[Show this message]' \ '--help[Show this message]' \
'(--unbrewed)--installed[Outputs nothing and returns a failing status code if formula is not installed]' \
'--quiet[Make some output more quiet]' \ '--quiet[Make some output more quiet]' \
'--unbrewed[List files in Homebrew'\''s prefix not installed by Homebrew]' \ '(--installed)--unbrewed[List files in Homebrew'\''s prefix not installed by Homebrew]' \
'--verbose[Make some output more verbose]' \ '--verbose[Make some output more verbose]' \
'::formula:__brew_formulae' '::formula:__brew_formulae'
} }

View File

@ -723,7 +723,7 @@ the list is formatted for export to `bash`(1) unless `--plain` is passed.
* `--plain`: * `--plain`:
Generate plain output even when piped. Generate plain output even when piped.
### `--prefix` [*`--unbrewed`*] [*`formula`* ...] ### `--prefix` [*`--unbrewed`*] [*`--installed`*] [*`formula`* ...]
Display Homebrew's install path. *Default:* Display Homebrew's install path. *Default:*
@ -736,6 +736,8 @@ is or would be installed.
* `--unbrewed`: * `--unbrewed`:
List files in Homebrew's prefix not installed by Homebrew. List files in Homebrew's prefix not installed by Homebrew.
* `--installed`:
Outputs nothing and returns a failing status code if *`formula`* is not installed.
### `--repository`, `--repo` [*`tap`* ...] ### `--repository`, `--repo` [*`tap`* ...]

View File

@ -978,7 +978,7 @@ Generate a list of environment variables for the specified shell, or \fB\-\-shel
\fB\-\-plain\fR \fB\-\-plain\fR
Generate plain output even when piped\. Generate plain output even when piped\.
. .
.SS "\fB\-\-prefix\fR [\fI\-\-unbrewed\fR] [\fIformula\fR \.\.\.]" .SS "\fB\-\-prefix\fR [\fI\-\-unbrewed\fR] [\fI\-\-installed\fR] [\fIformula\fR \.\.\.]"
Display Homebrew\'s install path\. \fIDefault:\fR Display Homebrew\'s install path\. \fIDefault:\fR
. .
.IP "\(bu" 4 .IP "\(bu" 4
@ -999,6 +999,10 @@ If \fIformula\fR is provided, display the location in the Cellar where \fIformul
\fB\-\-unbrewed\fR \fB\-\-unbrewed\fR
List files in Homebrew\'s prefix not installed by Homebrew\. List files in Homebrew\'s prefix not installed by Homebrew\.
. .
.TP
\fB\-\-installed\fR
Outputs nothing and returns a failing status code if \fIformula\fR is not installed\.
.
.SS "\fB\-\-repository\fR, \fB\-\-repo\fR [\fItap\fR \.\.\.]" .SS "\fB\-\-repository\fR, \fB\-\-repo\fR [\fItap\fR \.\.\.]"
Display where Homebrew\'s git repository is located\. Display where Homebrew\'s git repository is located\.
. .