brew/Library/Homebrew/prefix.sh
Mike McQuaid ca47b47f77
Speedup brew --prefix <formula>
This case is _really_ slow even although it's something we encourage
people to run often and build systems often do. The `brew --prefix`
case is really fast because it's just in Bash so: let's pull the
`brew --prefix <formula>` case into Bash too.

This doesn't handle any edge-cases like `--installed` and the formula
detection is pretty simple.

Also, to make this behaviour consistent, never output `Cellar` paths
from the (Ruby) `brew --prefix`; we never want people relying on the
Cellar paths anyway, only output them if the formula wasn't installed
(where, arguably, using a Cellar path is even worse) and the speedup is
worth this deviation in behaviour.
2021-02-25 17:07:28 +00:00

30 lines
1.1 KiB
Bash

# does the quickest output of brew --prefix possible for the basic cases:
# - `brew --prefix` (output HOMEBREW_PREFIX)
# - `brew --prefix <formula>` (output HOMEBREW_PREFIX/opt/<formula>)
# anything else? delegate to the slower cmd/--prefix.rb
homebrew-prefix() {
while [[ "$#" -gt 0 ]]; do
case $1 in
# check we actually have --prefix and not e.g. --prefixsomething
--prefix) local prefix="1"; shift ;;
# reject all other flags
-*) return 1 ;;
*) [ -n "$formula" ] && return 1; local formula="$1"; shift ;;
esac
done
[ -z "$prefix" ] && return 1
[ -z "$formula" ] && echo "$HOMEBREW_PREFIX" && return 0
local formula_path
if [ -f "$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/${formula}.rb" ]; then
formula_path="$HOMEBREW_REPOSITORY/Library/Taps/homebrew/homebrew-core/${formula}.rb"
else
formula_path="$(find "$HOMEBREW_REPOSITORY/Library/Taps" -name "${formula}.rb" -print -quit)"
fi
[ -z "$formula_path" ] && return 1
echo "$HOMEBREW_PREFIX/opt/$formula"
return 0
}