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
switch "--unbrewed",
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
end
@ -31,6 +34,8 @@ module Homebrew
def __prefix
args = __prefix_args.parse
raise UsageError, "`--installed` requires a formula argument." if args.installed? && args.no_named?
if args.unbrewed?
raise UsageError, "`--unbrewed` does not take a formula argument." unless args.no_named?
@ -38,9 +43,27 @@ module Homebrew
elsif args.no_named?
puts HOMEBREW_PREFIX
else
puts args.named.to_resolved_formulae.map { |f|
f.opt_prefix.exist? ? f.opt_prefix : f.latest_installed_prefix
}
formulae = args.named.to_resolved_formulae
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

View File

@ -14,4 +14,18 @@ describe "brew --prefix", :integration_test do
.and not_to_output.to_stderr
.and be_a_success
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

View File

@ -202,6 +202,7 @@ _brew___prefix() {
__brewcomp "
--debug
--help
--installed
--quiet
--unbrewed
--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_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 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 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'

View File

@ -312,8 +312,9 @@ _brew___prefix() {
_arguments \
'--debug[Display any debugging information]' \
'--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]' \
'--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]' \
'::formula:__brew_formulae'
}

View File

@ -723,7 +723,7 @@ the list is formatted for export to `bash`(1) unless `--plain` is passed.
* `--plain`:
Generate plain output even when piped.
### `--prefix` [*`--unbrewed`*] [*`formula`* ...]
### `--prefix` [*`--unbrewed`*] [*`--installed`*] [*`formula`* ...]
Display Homebrew's install path. *Default:*
@ -736,6 +736,8 @@ is or would be installed.
* `--unbrewed`:
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`* ...]

View File

@ -978,7 +978,7 @@ Generate a list of environment variables for the specified shell, or \fB\-\-shel
\fB\-\-plain\fR
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
.
.IP "\(bu" 4
@ -999,6 +999,10 @@ If \fIformula\fR is provided, display the location in the Cellar where \fIformul
\fB\-\-unbrewed\fR
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 \.\.\.]"
Display where Homebrew\'s git repository is located\.
.