Add shell optimisation for --cellar

This commit is contained in:
Bo Anderson 2023-02-16 21:56:50 +00:00
parent c2342eca91
commit dc286a11ff
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
2 changed files with 31 additions and 10 deletions

View File

@ -75,10 +75,10 @@ case "$*" in
homebrew-casks
exit 0
;;
# falls back to cmd/prefix.rb on a non-zero return
--prefix*)
source "${HOMEBREW_LIBRARY}/Homebrew/prefix.sh"
homebrew-prefix "$@" && exit 0
# falls back to cmd/--prefix.rb and cmd/--cellar.rb on a non-zero return
--prefix* | --cellar*)
source "${HOMEBREW_LIBRARY}/Homebrew/formula_path.sh"
homebrew-formula-path "$@" && exit 0
;;
esac

View File

@ -1,10 +1,12 @@
# does the quickest output of brew --prefix possible for the basic cases:
# does the quickest output of brew --prefix/--cellar possible for the basic cases:
# - `brew --prefix` (output HOMEBREW_PREFIX)
# - `brew --cellar` (output HOMEBREW_CELLAR)
# - `brew --prefix <formula>` (output HOMEBREW_PREFIX/opt/<formula>)
# anything else? delegate to the slower cmd/--prefix.rb
# - `brew --cellar <formula>` (output HOMEBREW_CELLAR/<formula>)
# anything else? delegate to the slower cmd/--prefix.rb and cmd/--cellar.rb
# HOMEBREW_PREFIX and HOMEBREW_REPOSITORY are set by brew.sh
# shellcheck disable=SC2154
homebrew-prefix() {
homebrew-formula-path() {
while [[ "$#" -gt 0 ]]
do
case "$1" in
@ -13,6 +15,10 @@ homebrew-prefix() {
local prefix="1"
shift
;;
--cellar)
local cellar="1"
shift
;;
# reject all other flags
-*) return 1 ;;
*)
@ -22,8 +28,18 @@ homebrew-prefix() {
;;
esac
done
[[ -z "${prefix}" ]] && return 1
[[ -z "${formula}" ]] && echo "${HOMEBREW_PREFIX}" && return 0
[[ -z "${prefix}" && -z "${cellar}" ]] && return 1
[[ -n "${prefix}" && -n "${cellar}" ]] && return 1 # don't allow both!
if [[ -z "${formula}" ]]
then
if [[ -n "${prefix}" ]]
then
echo "${HOMEBREW_PREFIX}"
else
echo "${HOMEBREW_CELLAR}"
fi
return 0
fi
local formula_exists
if [[ -f "${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]]
@ -63,6 +79,11 @@ homebrew-prefix() {
[[ -z "${formula_exists}" ]] && return 1
echo "${HOMEBREW_PREFIX}/opt/${formula}"
if [[ -n "${prefix}" ]]
then
echo "${HOMEBREW_PREFIX}/opt/${formula}"
else
echo "${HOMEBREW_CELLAR}/${formula}"
fi
return 0
}