From 2df60ca4eac81e18336c612d732d6ab48c7afbf6 Mon Sep 17 00:00:00 2001 From: Florian Blanchet Date: Fri, 28 Jul 2023 22:31:11 +0300 Subject: [PATCH 1/5] Add Microsoft PowerShell support --- libexec/pyenv-init | 63 ++++++++++++++++++++++++++++++++++++++++- libexec/pyenv-sh-rehash | 2 +- libexec/pyenv-sh-shell | 16 +++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/libexec/pyenv-init b/libexec/pyenv-init index c1e0da7b..98b8c48e 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -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 <&2 false end +EOS + ;; + pwsh ) + cat </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}\"" From 2ac251faa19508c7ae4efce2984591987e1d64c1 Mon Sep 17 00:00:00 2001 From: Florian Blanchet Date: Fri, 28 Jul 2023 22:32:36 +0300 Subject: [PATCH 2/5] Add instructions for PowerShell in README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 7bb47ac7..74248b63 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,16 @@ See [Advanced configuration](#advanced-configuration) for details and more confi pyenv init - | source ~~~ + - For **Microsoft PowerShell**: + + Add the commands to `$profile.CurrentUserAllHosts` by running the following in your terminal: + + ~~~ pwsh + echo '$Env:PYENV_ROOT="$HOME/.pyenv"' >> $profile.CurrentUserAllHosts + echo '$Env:PATH="$Env:PYENV_ROOT/bin:$Env:PATH"' >> $profile.CurrentUserAllHosts + echo 'iex ((pyenv init -) -join "`n")' >> $profile.CurrentUserAllHosts + ~~~ + **Bash warning**: There are some systems where the `BASH_ENV` variable is configured to point to `.bashrc`. On such systems, you should almost certainly put the `eval "$(pyenv init -)"` line into `.bash_profile`, and **not** into `.bashrc`. Otherwise, you From 99c693df3916272ba3f844d33f116880cf35cc4f Mon Sep 17 00:00:00 2001 From: Florian Blanchet Date: Fri, 28 Jul 2023 22:33:37 +0300 Subject: [PATCH 3/5] Add PowerShell autocompletion feature --- README.md | 2 +- completions/pyenv.pwsh | 17 +++++++++++++++++ libexec/pyenv-init | 13 ++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 completions/pyenv.pwsh diff --git a/README.md b/README.md index 74248b63..bd37bb48 100644 --- a/README.md +++ b/README.md @@ -615,7 +615,7 @@ opposed to this idea. Here's what `eval "$(pyenv init -)"` actually does: 2. **Installs autocompletion.** This is entirely optional but pretty useful. Sourcing `$(pyenv root)/completions/pyenv.bash` will set that - up. There are also completions for Zsh and Fish. + up. There are also completions for Zsh, Fish and PowerShell. 3. **Rehashes shims.** From time to time you'll need to rebuild your shim files. Doing this on init makes sure everything is up to diff --git a/completions/pyenv.pwsh b/completions/pyenv.pwsh new file mode 100644 index 00000000..f422f468 --- /dev/null +++ b/completions/pyenv.pwsh @@ -0,0 +1,17 @@ +$scriptblock = { + param($wordToComplete, $commandAst, $cursorPosition) + $words = $commandAst.ToString() + if ( $wordToComplete ) { + $matches = (($words[0..$cursorPosition] -join '') | Select-String -Pattern "\s+" -AllMatches).Matches + if ( $matches ) { + $cursorPosition = $matches[-1].Index - 1 + } + } + $words = $words[0..$cursorPosition] -join '' -split "\s+" + if ( $words.Count -ge 2 ) { + pyenv completions $words[1] | where { $_ -match $wordToComplete } + } else { + pyenv commands | where { $_ -match $wordToComplete } + } +} +Register-ArgumentCompleter -Native -CommandName pyenv -ScriptBlock $scriptblock diff --git a/libexec/pyenv-init b/libexec/pyenv-init index 98b8c48e..cf0edc25 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -281,7 +281,14 @@ function print_env() { function print_completion() { completion="${root}/completions/pyenv.${shell}" if [ -r "$completion" ]; then - echo "source '$completion'" + case "$shell" in + pwsh ) + echo "iex (gc $completion -Raw)" + ;; + * ) + echo "source '$completion'" + ;; + esac fi } @@ -292,7 +299,7 @@ function print_rehash() { echo '& pyenv rehash 2>/dev/null' ;; * ) - echo 'command pyenv rehash 2>/dev/null' + echo 'command pyenv rehash 2>/dev/null' ;; esac fi @@ -348,7 +355,7 @@ pyenv() { EOS ;; esac - + if [ "$shell" != "fish" ] && [ "$shell" != "pwsh" ]; then IFS="|" cat < Date: Sat, 29 Jul 2023 03:56:08 +0300 Subject: [PATCH 4/5] Add unit tests for pwsh --- test/init.bats | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ test/rehash.bats | 7 ++++++ test/shell.bats | 28 +++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/test/init.bats b/test/init.bats index 21109c94..564153f2 100755 --- a/test/init.bats +++ b/test/init.bats @@ -63,6 +63,19 @@ OUT assert_line 'pyenv init - | source' } +@test "setup shell completions (pwsh)" { + root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" + run pyenv-init - pwsh + assert_success + assert_line "iex (gc ${root}/test/../libexec/../completions/pyenv.pwsh -Raw)" +} + +@test "pwsh instructions" { + run pyenv-init pwsh + assert [ "$status" -eq 1 ] + assert_line 'iex ((pyenv init -) -join "`n")' +} + @test "shell detection for installer" { run pyenv-init --detect-shell assert_success @@ -89,6 +102,13 @@ OUT assert_line "set -gx PATH '${PYENV_ROOT}/shims' \$PATH" } +@test "adds shims to PATH (pwsh)" { + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" + run pyenv-init - pwsh + assert_success + assert_line '$Env:PATH="'${PYENV_ROOT}'/shims:$Env:PATH"' +} + @test "removes existing shims from PATH" { OLDPATH="$PATH" export PATH="${BATS_TEST_DIRNAME}/nonexistent:${PYENV_ROOT}/shims:$PATH" @@ -113,6 +133,19 @@ echo "\$PATH" assert_output "${PYENV_ROOT}/shims:${BATS_TEST_DIRNAME}/nonexistent:${OLDPATH//${PYENV_ROOT}\/shims:/}" } +@test "removes existing shims from PATH (pwsh)" { + command -v pwsh >/dev/null || skip "-- pwsh not installed" + OLDPATH="$PATH" + export PATH="${BATS_TEST_DIRNAME}/nonexistent:${PYENV_ROOT}/shims:$PATH" + run pwsh -noni -c - </dev/null || skip "-- pwsh not installed" + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" + run pwsh -noni -c - </dev/null || skip "-- pwsh not installed" + export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:${PYENV_ROOT}/shims:/bin:/usr/local/bin" + run pwsh -noni -c - < Date: Tue, 20 May 2025 22:13:26 +0300 Subject: [PATCH 5/5] Fix test --- test/shell.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/shell.bats b/test/shell.bats index 553b9b01..e20df561 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -51,7 +51,7 @@ load test_helper @test "shell revert (pwsh)" { PYENV_SHELL=pwsh run pyenv-sh-shell - assert_success - assert_line 0 'if set -q PYENV_VERSION_OLD' + assert_line 0 'if ( Get-Item -Path Env:\PYENV_VERSION* ) {' } @test "shell unset" {