From 28a8409b6e6ecc35f739f88d7e8e309c9265818d Mon Sep 17 00:00:00 2001 From: Adrian Ho Date: Sat, 30 Jan 2021 08:58:04 +0800 Subject: [PATCH] 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 $ ``` --- Library/Homebrew/cmd/--prefix.rb | 29 +++++++++++++++++++--- Library/Homebrew/test/cmd/--prefix_spec.rb | 14 +++++++++++ completions/bash/brew | 1 + completions/fish/brew.fish | 1 + completions/zsh/_brew | 3 ++- docs/Manpage.md | 4 ++- manpages/brew.1 | 6 ++++- 7 files changed, 52 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/cmd/--prefix.rb b/Library/Homebrew/cmd/--prefix.rb index d897a8b8c6..8d97ca3c0f 100644 --- a/Library/Homebrew/cmd/--prefix.rb +++ b/Library/Homebrew/cmd/--prefix.rb @@ -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 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 diff --git a/Library/Homebrew/test/cmd/--prefix_spec.rb b/Library/Homebrew/test/cmd/--prefix_spec.rb index 95ac7bb042..d0ed2f9422 100644 --- a/Library/Homebrew/test/cmd/--prefix_spec.rb +++ b/Library/Homebrew/test/cmd/--prefix_spec.rb @@ -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 diff --git a/completions/bash/brew b/completions/bash/brew index 8d89f2fedb..c2e315433a 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -202,6 +202,7 @@ _brew___prefix() { __brewcomp " --debug --help + --installed --quiet --unbrewed --verbose diff --git a/completions/fish/brew.fish b/completions/fish/brew.fish index c3775eec9e..49f32cd9f2 100644 --- a/completions/fish/brew.fish +++ b/completions/fish/brew.fish @@ -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' diff --git a/completions/zsh/_brew b/completions/zsh/_brew index 766c32adee..1549409b65 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -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' } diff --git a/docs/Manpage.md b/docs/Manpage.md index a596822af1..da06d4b927 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -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`* ...] diff --git a/manpages/brew.1 b/manpages/brew.1 index d5ec73d453..e6817cbe89 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -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\. .