| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | # 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 | 
					
						
							| 
									
										
										
										
											2021-04-20 20:32:01 +09:00
										 |  |  | # HOMEBREW_PREFIX and HOMEBREW_REPOSITORY are set by brew.sh | 
					
						
							|  |  |  | # shellcheck disable=SC2154 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | homebrew-prefix() { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         ;; | 
					
						
							|  |  |  |       # 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 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   [[ -z "${prefix}" ]] && return 1 | 
					
						
							|  |  |  |   [[ -z "${formula}" ]] && echo "${HOMEBREW_PREFIX}" && return 0 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   local formula_path | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ -f "${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" ]] | 
					
						
							|  |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-04-20 20:32:01 +09:00
										 |  |  |     formula_path="${HOMEBREW_REPOSITORY}/Library/Taps/homebrew/homebrew-core/Formula/${formula}.rb" | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2021-03-06 21:07:33 +08:00
										 |  |  |     formula_path="$( | 
					
						
							|  |  |  |       shopt -s nullglob | 
					
						
							| 
									
										
										
										
											2021-04-20 20:32:01 +09:00
										 |  |  |       echo "${HOMEBREW_REPOSITORY}/Library/Taps"/*/*/{Formula/,HomebrewFormula/,}"${formula}.rb" | 
					
						
							| 
									
										
										
										
											2021-03-06 21:07:33 +08:00
										 |  |  |     )"
 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   [[ -z "${formula_path}" ]] && return 1 | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-20 20:32:01 +09:00
										 |  |  |   echo "${HOMEBREW_PREFIX}/opt/${formula}" | 
					
						
							| 
									
										
										
										
											2021-02-24 16:02:00 +00:00
										 |  |  |   return 0 | 
					
						
							|  |  |  | } |