feat: Support PowerShell Core (#1522)

This commit is contained in:
Edwin Kofler 2023-03-31 21:12:20 -07:00 committed by GitHub
parent 00fee78423
commit 213aa22378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 226 additions and 1 deletions

View File

@ -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"

31
asdf.ps1 Normal file
View File

@ -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
}
}

View File

@ -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:

View File

@ -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:

View File

@ -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` 文件中:

View File

@ -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"
;;

96
test/asdf_pwsh.bats Normal file
View File

@ -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" != "" ]
}