From 87199460361280e8e8da3952ea2195d41e1fd996 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Thu, 4 May 2023 10:28:11 -0400 Subject: [PATCH] Allow brew shellenv to accept a shell name param Resolves #15358 With this change, `brew shellenv` will accept a shell name parameter to override its default output (based on `/bin/ps`) for advanced use cases such as with dotfile templating and caching with [twpayne/chezmoi](https://github.com/twpayne/chezmoi/discussions/2971). When running under `fish`, this is the output: ```console $ brew shellenv pwsh [System.Environment]::SetEnvironmentVariable('HOMEBREW_PREFIX','/opt/homebrew',[System.EnvironmentVariableTarget]::Process) [System.Environment]::SetEnvironmentVariable('HOMEBREW_CELLAR','/opt/homebrew/Cellar',[System.EnvironmentVariableTarget]::Process) [System.Environment]::SetEnvironmentVariable('HOMEBREW_REPOSITORY','/opt/homebrew',[System.EnvironmentVariableTarget]::Process) [System.Environment]::SetEnvironmentVariable('PATH',$('/opt/homebrew/bin:/opt/homebrew/sbin:'+$ENV:PATH),[System.EnvironmentVariableTarget]::Process) [System.Environment]::SetEnvironmentVariable('MANPATH',$('/opt/homebrew/share/man'+$(if(${ENV:MANPATH}){':'+${ENV:MANPATH}})+':'),[System.EnvironmentVariableTarget]::Process) [System.Environment]::SetEnvironmentVariable('INFOPATH',$('/opt/homebrew/share/info'+$(if(${ENV:INFOPATH}){':'+${ENV:INFOPATH}})),[System.EnvironmentVariableTarget]::Process) $ brew shellenv set -gx HOMEBREW_PREFIX "/opt/homebrew"; set -gx HOMEBREW_CELLAR "/opt/homebrew/Cellar"; set -gx HOMEBREW_REPOSITORY "/opt/homebrew"; set -q PATH; or set PATH ''; set -gx PATH "/opt/homebrew/bin" "/opt/homebrew/sbin" $PATH; set -q MANPATH; or set MANPATH ''; set -gx MANPATH "/opt/homebrew/share/man" $MANPATH; set -q INFOPATH; or set INFOPATH ''; set -gx INFOPATH "/opt/homebrew/share/info" $INFOPATH; ``` The specific case presented in the mentioned discussion could be mitigated by an additional level of indirection (`{{ output "fish" "-c" "'brew shellenv'" | trim }}`), that requires that the running system have the target shells installed, when they are not strictly necessary with `brew shellenv`. --- Library/Homebrew/brew.sh | 2 +- Library/Homebrew/cmd/shellenv.sh | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 35a74283fc..37f1112850 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -81,7 +81,7 @@ case "$*" in ;; shellenv) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh" - homebrew-shellenv + homebrew-shellenv "$1" exit 0 ;; formulae) diff --git a/Library/Homebrew/cmd/shellenv.sh b/Library/Homebrew/cmd/shellenv.sh index a19a2223d5..2f2af3ff1f 100644 --- a/Library/Homebrew/cmd/shellenv.sh +++ b/Library/Homebrew/cmd/shellenv.sh @@ -1,4 +1,4 @@ -#: * `shellenv` +#: * `shellenv [bash|csh|fish|pwsh|sh|tcsh|zsh]` #: #: Print export statements. When run in a shell, this installation of Homebrew will be added to your `PATH`, `MANPATH`, and `INFOPATH`. #: @@ -6,6 +6,8 @@ #: To help guarantee idempotence, this command produces no output when Homebrew's `bin` and `sbin` directories are first and second #: respectively in your `PATH`. Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, #: `~/.bash_profile`, or `~/.zprofile`) with: `eval "$(brew shellenv)"` +#: +#: The shell can be specified explicitly with a supported shell name parameter. Unknown shells will output POSIX exports. # HOMEBREW_CELLAR and HOMEBREW_PREFIX are set by extend/ENV/super.rb # HOMEBREW_REPOSITORY is set by bin/brew @@ -18,7 +20,14 @@ homebrew-shellenv() { return fi - case "$(/bin/ps -p "${PPID}" -c -o comm=)" in + if [[ -n "$1" ]] + then + HOMEBREW_SHELL_NAME="$1" + else + HOMEBREW_SHELL_NAME="$(/bin/ps -p "${PPID}" -c -o comm=)}" + fi + + case "${HOMEBREW_SHELL_NAME}" in fish | -fish) echo "set -gx HOMEBREW_PREFIX \"${HOMEBREW_PREFIX}\";" echo "set -gx HOMEBREW_CELLAR \"${HOMEBREW_CELLAR}\";"