| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  | # does the quickest output of brew --prefix/--cellar possible for the basic cases: | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | # - `brew --prefix` (output HOMEBREW_PREFIX) | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  | # - `brew --cellar` (output HOMEBREW_CELLAR) | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | # - `brew --prefix <formula>` (output HOMEBREW_PREFIX/opt/<formula>) | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  | # - `brew --cellar <formula>` (output HOMEBREW_CELLAR/<formula>) | 
					
						
							|  |  |  | # anything else? delegate to the slower cmd/--prefix.rb and cmd/--cellar.rb | 
					
						
							| 
									
										
										
										
											2021-04-20 20:32:01 +09:00
										 |  |  | # HOMEBREW_PREFIX and HOMEBREW_REPOSITORY are set by brew.sh | 
					
						
							|  |  |  | # shellcheck disable=SC2154 | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  | homebrew-formula-path() { | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   while [[ "$#" -gt 0 ]] | 
					
						
							|  |  |  |   do | 
					
						
							|  |  |  |     case "$1" in | 
					
						
							|  |  |  |       # check we actually have --prefix and not e.g. --prefixsomething | 
					
						
							|  |  |  |       --prefix) | 
					
						
							| 
									
										
										
										
											2021-09-16 01:08:02 +08:00
										 |  |  |         local prefix="1" | 
					
						
							|  |  |  |         shift | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  |       --cellar) | 
					
						
							|  |  |  |         local cellar="1" | 
					
						
							|  |  |  |         shift | 
					
						
							|  |  |  |         ;; | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |       # reject all other flags | 
					
						
							|  |  |  |       -*) return 1 ;; | 
					
						
							|  |  |  |       *) | 
					
						
							|  |  |  |         [[ -n "${formula}" ]] && return 1 | 
					
						
							| 
									
										
										
										
											2021-09-16 01:08:02 +08:00
										 |  |  |         local formula="$1" | 
					
						
							|  |  |  |         shift | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |         ;; | 
					
						
							|  |  |  |     esac | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   done | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  |   [[ -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 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |   local formula_exists | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ -f "${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |     formula_exists="1" | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |     local formula_path | 
					
						
							| 
									
										
										
										
											2021-03-06 21:07:33 +08:00
										 |  |  |     formula_path="$( | 
					
						
							|  |  |  |       shopt -s nullglob | 
					
						
							| 
									
										
										
										
											2023-08-04 16:21:31 +01:00
										 |  |  |       echo "${HOMEBREW_REPOSITORY}/Library/Taps"/*/*/{Formula/,HomebrewFormula/,Formula/*/,}"${formula}.rb" | 
					
						
							| 
									
										
										
										
											2021-03-06 21:07:33 +08:00
										 |  |  |     )"
 | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |     [[ -n "${formula_path}" ]] && formula_exists="1" | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if [[ -z "${formula_exists}" && | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |         -z "${HOMEBREW_NO_INSTALL_FROM_API}" ]] | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2023-02-16 21:49:03 +00:00
										 |  |  |     if [[ -f "${HOMEBREW_CACHE}/api/formula_names.txt" ]] && | 
					
						
							|  |  |  |        grep -Fxq "${formula}" "${HOMEBREW_CACHE}/api/formula_names.txt" | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       formula_exists="1" | 
					
						
							|  |  |  |     elif [[ -f "${HOMEBREW_CACHE}/api/formula_aliases.txt" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       while IFS="|" read -r alias_name real_name | 
					
						
							|  |  |  |       do | 
					
						
							|  |  |  |         case "${alias_name}" in | 
					
						
							|  |  |  |           "${formula}") | 
					
						
							|  |  |  |             formula_exists="1" | 
					
						
							|  |  |  |             formula="${real_name}" | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |             ;; | 
					
						
							|  |  |  |           *) ;; | 
					
						
							|  |  |  |         esac | 
					
						
							|  |  |  |       done <"${HOMEBREW_CACHE}/api/formula_aliases.txt" | 
					
						
							|  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2023-02-14 04:31:18 +00:00
										 |  |  |   fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   [[ -z "${formula_exists}" ]] && return 1 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 21:56:50 +00:00
										 |  |  |   if [[ -n "${prefix}" ]] | 
					
						
							|  |  |  |   then | 
					
						
							|  |  |  |     echo "${HOMEBREW_PREFIX}/opt/${formula}" | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     echo "${HOMEBREW_CELLAR}/${formula}" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   return 0 | 
					
						
							|  |  |  | } |