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`.
This commit is contained in:
Austin Ziegler 2023-05-04 10:28:11 -04:00
parent 54adebb1de
commit 8719946036
No known key found for this signature in database
2 changed files with 12 additions and 3 deletions

View File

@ -81,7 +81,7 @@ case "$*" in
;; ;;
shellenv) shellenv)
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh" source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"
homebrew-shellenv homebrew-shellenv "$1"
exit 0 exit 0
;; ;;
formulae) formulae)

View File

@ -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`. #: 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 #: 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`, #: 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)"` #: `~/.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_CELLAR and HOMEBREW_PREFIX are set by extend/ENV/super.rb
# HOMEBREW_REPOSITORY is set by bin/brew # HOMEBREW_REPOSITORY is set by bin/brew
@ -18,7 +20,14 @@ homebrew-shellenv() {
return return
fi 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) fish | -fish)
echo "set -gx HOMEBREW_PREFIX \"${HOMEBREW_PREFIX}\";" echo "set -gx HOMEBREW_PREFIX \"${HOMEBREW_PREFIX}\";"
echo "set -gx HOMEBREW_CELLAR \"${HOMEBREW_CELLAR}\";" echo "set -gx HOMEBREW_CELLAR \"${HOMEBREW_CELLAR}\";"