From 213aa22378cf0ecf5b1924f1bfc4fee43338255a Mon Sep 17 00:00:00 2001 From: Edwin Kofler Date: Fri, 31 Mar 2023 21:12:20 -0700 Subject: [PATCH] feat: Support PowerShell Core (#1522) --- .github/workflows/tests.yml | 4 +- asdf.ps1 | 31 ++++++ docs/guide/getting-started.md | 30 ++++++ docs/pt-br/guide/getting-started.md | 30 ++++++ docs/zh-hans/guide/getting-started.md | 30 ++++++ .../command-export-shell-version.bash | 6 ++ test/asdf_pwsh.bats | 96 +++++++++++++++++++ 7 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 asdf.ps1 create mode 100644 test/asdf_pwsh.bats diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ded80768..aec6c696 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -59,9 +59,11 @@ jobs: - name: Install test dependencies run: | + curl -fsSLo- https://packages.microsoft.com/keys/microsoft.asc | sudo tee >/dev/null /etc/apt/trusted.gpg.d/microsoft.asc + sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list' sudo add-apt-repository -y ppa:fish-shell/nightly-master sudo apt-get update - sudo apt-get -y install fish curl parallel + sudo apt-get -y install fish curl parallel powershell # Create $HOME/bin mkdir -p "$HOME/bin" diff --git a/asdf.ps1 b/asdf.ps1 new file mode 100644 index 00000000..a4b405ee --- /dev/null +++ b/asdf.ps1 @@ -0,0 +1,31 @@ +$Env:ASDF_DIR = $PSScriptRoot + +$_asdf_bin = "$Env:ASDF_DIR/bin" +if ($null -eq $ASDF_DATA_DIR -or $ASDF_DATA_DIR -eq '') { + $_asdf_shims = "${env:HOME}/.asdf/shims" +} +else { + $_asdf_shims = "$ASDF_DATA_DIR/shims" +} + +$env:PATH = "${_asdf_bin}:${_asdf_shims}:${env:PATH}" + +if ($env:PATH -cnotlike "*${_asdf_bin}*") { + $env:PATH = "_asdf_bin:${env:PATH}" +} +if ($env:PATH -cnotlike "*${_asdf_shims}*") { + $env:PATH = "_asdf_shims:${env:PATH}" +} + +Remove-Variable -Force _asdf_bin, _asdf_shims + +function asdf { + $asdf = $(Get-Command -CommandType Application asdf).Source + + if ($args.Count -gt 0 -and $args[0] -eq 'shell') { + Invoke-Expression $(& $asdf 'export-shell-version' pwsh $args[1..($args.Count + -1)]) + } + else { + & $asdf $args + } +} diff --git a/docs/guide/getting-started.md b/docs/guide/getting-started.md index 03edfaf1..38d679c1 100644 --- a/docs/guide/getting-started.md +++ b/docs/guide/getting-started.md @@ -261,6 +261,36 @@ Add the following to `~/.zshrc`: Completions are placed in a ZSH friendly location, but [ZSH must be configured to use the autocompletions](https://wiki.archlinux.org/index.php/zsh#Command_completion). ::: +::: details PowerShell Core & Git + +Add the following to `~/.config/powershell/profile.ps1`: + +```shell +. "$HOME/.asdf/asdf.ps1" +``` + +::: + +::: details PowerShell Core & Homebrew + +Add `asdf.sh` to your `~/.config/powershell/profile.ps1` with: + +```shell:no-line-numbers +echo -e "\n. \"$(brew --prefix asdf)/libexec/asdf.ps1\"" >> ~/.config/powershell/profile.ps1 +``` + +::: + +::: details PowerShell Core & Pacman + +Add the following to `~/.config/powershell/profile.ps1`: + +```shell +. /opt/asdf-vm/asdf.ps1 +``` + +::: + ::: details Nushell & Git Add `asdf.nu` to your `~/.config/nushell/config.nu` with: diff --git a/docs/pt-br/guide/getting-started.md b/docs/pt-br/guide/getting-started.md index 9ad55b54..9c9bac8e 100644 --- a/docs/pt-br/guide/getting-started.md +++ b/docs/pt-br/guide/getting-started.md @@ -262,6 +262,36 @@ Adicione a seguinte linha ao seu `~/.zshrc`: . /opt/asdf-vm/asdf.sh ``` +::: details PowerShell Core & Git + +Adicione a seguinte linha ao seu `~/.config/powershell/profile.ps1`: + +```shell +. "$HOME/.asdf/asdf.ps1" +``` + +::: + +::: details PowerShell Core & Homebrew + +Adicione `asdf.ps1` ao seu `~/.config/powershell/profile.ps1` através do comando: + +```shell:no-line-numbers +echo -e "\n. \"$(brew --prefix asdf)/libexec/asdf.ps1\"" >> ~/.config/powershell/profile.ps1 +``` + +::: + +::: details PowerShell Core & Pacman + +Adicione a seguinte linha ao seu `~/.config/powershell/profile.ps1`: + +```shell +. /opt/asdf-vm/asdf.ps1 +``` + +::: + ::: details Nushell & Git Adicione `asdf.nu` ao seu `~/.config/nushell/config.nu` através do comando: diff --git a/docs/zh-hans/guide/getting-started.md b/docs/zh-hans/guide/getting-started.md index fdf70f42..b4b57a38 100644 --- a/docs/zh-hans/guide/getting-started.md +++ b/docs/zh-hans/guide/getting-started.md @@ -263,6 +263,36 @@ echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc 补全功能会被放在一个对 ZSH 很友好的位置,但是 [ZSH 必须使用自动补全完成配置](https://wiki.archlinux.org/index.php/zsh#Command_completion)。 ::: +::: details PowerShell Core & Git + +在 `~/.config/powershell/profile.ps1` 文件中加入以下内容: + +```shell +. "$HOME/.asdf/asdf.ps1" +``` + +::: + +::: details PowerShell Core & Homebrew + +使用以下命令将 `asdf.ps1` 加入到 `~/.config/powershell/profile.ps1` 文件中: + +```shell:no-line-numbers +echo -e "\n. \"$(brew --prefix asdf)/libexec/asdf.ps1\"" >> ~/.config/powershell/profile.ps1 +``` + +::: + +::: details PowerShell Core & Pacman + +在 `~/.config/powershell/profile.ps1` 文件中加入以下内容: + +```shell +. /opt/asdf-vm/asdf.ps1 +``` + +::: + ::: details Nushell & Git 使用以下命令将 `asdf.nu` 加入到 `~/.config/nushell/config.nu` 文件中: diff --git a/lib/commands/command-export-shell-version.bash b/lib/commands/command-export-shell-version.bash index 13b779d1..92ace8ab 100644 --- a/lib/commands/command-export-shell-version.bash +++ b/lib/commands/command-export-shell-version.bash @@ -31,6 +31,9 @@ shell_command() { # and pass to unset-env. printf "unset-env\n%s" "$version_env_var" ;; + pwsh) + printf '%s\n' "if (\$(Test-Path Env:$version_env_var) -eq 'True') { Remove-Item Env:$version_env_var }" + ;; *) printf "unset %s\n" "$version_env_var" ;; @@ -56,6 +59,9 @@ shell_command() { # and pass to set-env. printf "set-env\n%s\n%s" "$version_env_var" "$version" ;; + pwsh) + printf '%s\n' "\$Env:$version_env_var = '$version'" + ;; *) printf "export %s=\"%s\"\n" "$version_env_var" "$version" ;; diff --git a/test/asdf_pwsh.bats b/test/asdf_pwsh.bats new file mode 100644 index 00000000..4f27d12f --- /dev/null +++ b/test/asdf_pwsh.bats @@ -0,0 +1,96 @@ +#!/usr/bin/env bats +# shellcheck disable=SC2164 + +load test_helpers + +setup() { + cd "$(dirname "$BATS_TEST_DIRNAME")" + + if ! command -v pwsh &>/dev/null && [ -z "$GITHUB_ACTIONS" ]; then + skip "Powershell Core is not installed" + fi +} + +cleaned_path() { + echo "$PATH" | tr ':' '\n' | grep -v "asdf" | tr '\n' ':' +} + +@test "exports ASDF_DIR" { + run pwsh -Command " + function asdf() {} # checkstyle-ignore + Remove-item Function:asdf + \$Env:ASDF_DIR = '' + \$Env:ASDF_DATA_DIR = '' + \$Env:PATH = \"$(cleaned_path)\" + + . ./asdf.ps1 + Write-Output \"\$env:ASDF_DIR\"" + + [ "$status" -eq 0 ] + [ "$output" != "" ] +} + +@test "adds asdf dirs to PATH" { + run pwsh -Command " + function asdf() {} # checkstyle-ignore + Remove-item Function:asdf + \$Env:ASDF_DIR = '' + \$Env:ASDF_DATA_DIR = '' + \$Env:PATH = \"$(cleaned_path)\" + + . ./asdf.ps1 + Write-Output \$Env:PATH" + + [ "$status" -eq 0 ] + result=$(echo "$output" | grep "asdf") + [ "$result" != "" ] +} + +@test "does not add paths to PATH more than once" { + run pwsh -Command " + function asdf() {} # checkstyle-ignore + Remove-item Function:asdf + \$Env:ASDF_DIR = '' + \$Env:ASDF_DATA_DIR = '' + \$Env:PATH = \"$(cleaned_path)\" + + . ./asdf.ps1 + . ./asdf.ps1 + Write-Output \$Env:PATH" + + [ "$status" -eq 0 ] + + result=$(echo "$output" | tr ' ' '\n' | grep "asdf" | sort | uniq -d) + [ "$result" = "" ] +} + +@test "defines the asdf function" { + run pwsh -Command " + function asdf() {} # checkstyle-ignore + Remove-item Function:asdf + \$Env:ASDF_DIR = '' + \$Env:ASDF_DATA_DIR = '' + \$Env:PATH = \"$(cleaned_path)\" + + ./ asdf.ps1 + \$(Get-Command -CommandType asdf).Name" + + [ "$status" -eq 0 ] + [[ "$output" =~ "asdf" ]] +} + +@test "function calls asdf command" { + run pwsh -Command " + function asdf() {} # checkstyle-ignore + Remove-item Function:asdf + \$Env:ASDF_DIR = '' + \$Env:ASDF_DATA_DIR = '' + \$Env:PATH = \"$(cleaned_path)\" + + . ./asdf.ps1 + asdf info" + + [ "$status" -eq 0 ] + result=$(echo "$output" | grep "ASDF INSTALLED PLUGINS:") + [ "$result" != "" ] +}