Add Microsoft PowerShell support

This commit is contained in:
Florian Blanchet 2023-07-28 22:31:11 +03:00
parent 8aabba9485
commit 2df60ca4ea
3 changed files with 79 additions and 2 deletions

View file

@ -15,6 +15,7 @@ if [ "$1" = "--complete" ]; then
echo bash
echo fish
echo ksh
echo pwsh
echo zsh
exit
fi
@ -105,6 +106,10 @@ function detect_profile() {
profile_explain="~/.bash_profile if it exists, otherwise ~/.profile"
rc='~/.bashrc'
;;
pwsh )
profile='~/.config/powershell/profile.ps1'
rc='~/.config/powershell/profile.ps1'
;;
zsh )
profile='~/.zprofile'
rc='~/.zshrc'
@ -153,6 +158,21 @@ function help_() {
echo 'pyenv init - | source'
echo
;;
pwsh )
echo '# Load pyenv automatically by appending'
echo -n "# the following to "
if [ "$profile" == "$rc" ]; then
echo "$profile :"
else
echo
echo "${profile_explain:-$profile} (for login shells)"
echo "and $rc (for interactive shells) :"
fi
echo
echo '$Env:PYENV_ROOT="$HOME/.pyenv"'
echo '$Env:PATH="$Env:PYENV_ROOT/bin:$Env:PATH"'
echo 'iex ((pyenv init -) -join "`n")'
;;
* )
echo '# Load pyenv automatically by appending'
echo -n "# the following to "
@ -189,6 +209,11 @@ function print_path() {
print_path_prepend_shims
echo 'end'
;;
pwsh )
echo 'if ( $Env:PATH -match "'"${PYENV_ROOT}/shims"'" ) {'
print_path_prepend_shims
echo '}'
;;
* )
echo 'if [[ ":$PATH:" != *'\':"${PYENV_ROOT}"/shims:\''* ]]; then'
print_path_prepend_shims
@ -202,6 +227,10 @@ function print_path() {
echo 'set -eg PATH[$pyenv_index]; end; set -e pyenv_index'
print_path_prepend_shims
;;
pwsh )
echo '$Env:PATH="$(($Env:PATH -split '"':'"' | where { -not ($_ -match '"'${PYENV_ROOT}/shims'"') }) -join '"':'"')"'
print_path_prepend_shims
;;
* )
# Some distros (notably Debian-based) set Bash's SSH_SOURCE_BASHRC compilation option
# that makes it source `bashrc` under SSH even when not interactive.
@ -226,6 +255,9 @@ function print_path_prepend_shims() {
fish )
echo 'set -gx PATH '\'"${PYENV_ROOT}/shims"\'' $PATH'
;;
pwsh )
echo '$Env:PATH="'"${PYENV_ROOT}"'/shims:$Env:PATH"'
;;
* )
echo 'export PATH="'"${PYENV_ROOT}"'/shims:${PATH}"'
;;
@ -237,6 +269,9 @@ function print_env() {
fish )
echo "set -gx PYENV_SHELL $shell"
;;
pwsh )
echo '$Env:PYENV_SHELL="'"$shell"'"'
;;
* )
echo "export PYENV_SHELL=$shell"
;;
@ -252,7 +287,14 @@ function print_completion() {
function print_rehash() {
if [ -z "$no_rehash" ]; then
case "$shell" in
pwsh )
echo '& pyenv rehash 2>/dev/null'
;;
* )
echo 'command pyenv rehash 2>/dev/null'
;;
esac
fi
}
@ -272,6 +314,25 @@ function pyenv
command pyenv "\$command" \$argv
end
end
EOS
;;
pwsh )
cat <<EOS
function pyenv {
\$command=""
if ( \$args.Count -gt 0 ) {
\$command, \$args = \$args
}
if ( ("${commands[*]}" -split ' ') -contains \$command ) {
\$shell_cmds = & \$Env:PYENV_ROOT/bin/pyenv sh-\$command \$args
if ( \$shell_cmds.Count -gt 0 ) {
iex (\$shell_cmds -join "\`n")
}
} else {
& \$Env:PYENV_ROOT/bin/pyenv \$command \$args
}
}
EOS
;;
ksh | ksh93 | mksh )
@ -288,7 +349,7 @@ EOS
;;
esac
if [ "$shell" != "fish" ]; then
if [ "$shell" != "fish" ] && [ "$shell" != "pwsh" ]; then
IFS="|"
cat <<EOS
command="\${1:-}"

View file

@ -14,7 +14,7 @@ shell="$(basename "${PYENV_SHELL:-$SHELL}")"
pyenv-rehash
case "$shell" in
fish )
fish | pwsh )
# no rehash support
;;
* )

View file

@ -48,6 +48,9 @@ if [ "$versions" = "--unset" ]; then
echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
echo "set -e PYENV_VERSION"
;;
pwsh )
echo '$Env:PYENV_VERSION, $Env:PYENV_VERSION_OLD = $null, $Env:PYENV_VERSION'
;;
* )
echo 'PYENV_VERSION_OLD="${PYENV_VERSION-}"'
echo "unset PYENV_VERSION"
@ -74,6 +77,16 @@ else
echo "pyenv: PYENV_VERSION_OLD is not set" >&2
false
end
EOS
;;
pwsh )
cat <<EOS
if ( Get-Item -Path Env:\PYENV_VERSION* ) {
\$Env:PYENV_VERSION, \$Env:PYENV_VERSION_OLD = \$Env:PYENV_VERSION_OLD, \$Env:PYENV_VERSION
} else {
Write-Error "pyenv: Env:PYENV_VERSION_OLD is not set"
return \$false
}
EOS
;;
* )
@ -109,6 +122,9 @@ if pyenv-prefix "${versions[@]}" >/dev/null; then
echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"'
echo "set -gx PYENV_VERSION \"$version\""
;;
pwsh )
echo '$Env:PYENV_VERSION, $Env:PYENV_VERSION_OLD = "'"${version}"'", $Env:PYENV_VERSION'
;;
* )
echo 'PYENV_VERSION_OLD="${PYENV_VERSION-}"'
echo "export PYENV_VERSION=\"${version}\""