axmol/1k/install-pwsh.sh

92 lines
2.7 KiB
Bash
Raw Normal View History

#!/bin/bash
# Install powershell 7 on macOS, Ubuntu, ArchLinux to system path
# usage: ./install-pwsh [pwsh_ver]
#
2023-08-30 08:34:33 +08:00
HOST_OS=$(uname)
myRoot=$(dirname "$0")
cacheDir=~/.1kiss
mkdir -p $cacheDir
2023-08-30 08:34:33 +08:00
pwsh_ver=$1
if [ "$pwsh_ver" = "" ] ; then
2024-07-25 01:16:01 +08:00
pwsh_ver='7.4.4'
fi
pwsh_min_ver=$2
if [ "$pwsh_min_ver" = "" ] ; then
pwsh_min_ver='7.3.0'
fi
if [[ "$pwsh_ver" < "$pwsh_min_ver" ]] ; then
pwsh_ver=$pwsh_min_ver
2023-08-30 12:40:27 +08:00
fi
function check_pwsh {
2024-07-25 01:16:01 +08:00
min_ver=$1
preferred_ver=$2
if command -v pwsh >/dev/null ; then
2024-07-25 01:16:01 +08:00
verx=$(pwsh --version)
very="PowerShell $min_ver"
if ([ "$preferred_ver" != "$min_ver" ] && ([[ "$verx" > "$very" ]] || [ "$verx" = "$very" ])) \
|| ([ "$preferred_ver" = "$min_ver" ] && [ "$verx" = "$very" ]) ; then
echo "1kiss: $verx installed."
exit 0
fi
fi
2024-07-25 01:16:01 +08:00
echo "Installing PowerShell $preferred_ver ..."
}
2023-08-30 12:40:27 +08:00
2023-08-31 12:38:53 +08:00
HOST_ARCH=$(uname -m)
2023-08-30 12:40:27 +08:00
if [ "$HOST_ARCH" = 'x86_64' ] ; then
2023-08-31 12:38:53 +08:00
HOST_ARCH=x64
2023-08-30 08:34:33 +08:00
fi
if [ $HOST_OS = 'Darwin' ] ; then
2024-07-25 01:16:01 +08:00
check_pwsh $pwsh_min_ver $preferred_ver
2023-08-30 12:40:27 +08:00
pwsh_pkg="powershell-$pwsh_ver-osx-$HOST_ARCH.pkg"
pwsh_pkg_out="$cacheDir/$pwsh_pkg"
2023-08-30 12:40:27 +08:00
if [ ! -f "$pwsh_pkg_out" ] ; then
2023-09-08 16:13:46 +08:00
pwsh_url="https://github.com/PowerShell/PowerShell/releases/download/v$pwsh_ver/$pwsh_pkg"
echo "Downloading $pwsh_url ..."
curl -L "$pwsh_url" -o "$pwsh_pkg_out"
2023-08-30 12:40:27 +08:00
fi
sudo xattr -rd com.apple.quarantine "$pwsh_pkg_out"
sudo installer -pkg "$pwsh_pkg_out" -target /
2023-08-30 08:34:33 +08:00
elif [ $HOST_OS = 'Linux' ] ; then
2024-07-25 01:16:01 +08:00
if command -v dpkg > /dev/null; then # Linux distro: deb (ubuntu)
check_pwsh $pwsh_min_ver $preferred_ver
2023-08-30 12:40:27 +08:00
pwsh_pkg="powershell_$pwsh_ver-1.deb_amd64.deb"
pwsh_pkg_out="$cacheDir/$pwsh_pkg"
2023-08-30 12:40:27 +08:00
if [ ! -f "$pwsh_pkg_out" ] ; then
curl -L "https://github.com/PowerShell/PowerShell/releases/download/v$pwsh_ver/$pwsh_pkg" -o "$pwsh_pkg_out"
fi
sudo_cmd=$(which sudo)
$sudo_cmd dpkg -i "$pwsh_pkg_out"
$sudo_cmd apt-get install -f
2024-07-25 01:16:01 +08:00
elif command -v pacman > /dev/null; then # Linux distro: Arch
2023-08-30 12:40:27 +08:00
# refer: https://ephos.github.io/posts/2018-9-17-Pwsh-ArchLinux
2023-11-18 13:16:40 +08:00
# available pwsh version, refer to: https://aur.archlinux.org/packages/powershell-bin
2024-07-25 01:16:01 +08:00
check_pwsh $pwsh_min_ver
git clone https://aur.archlinux.org/powershell-bin.git $cacheDir/powershell-bin
cd $cacheDir/powershell-bin
2023-08-31 12:40:45 +08:00
makepkg -si --needed --noconfirm
2023-08-30 12:40:27 +08:00
cd -
fi
2023-08-30 08:34:33 +08:00
else
echo "Unsupported HOST OS: $HOST_OS"
2023-08-30 12:40:27 +08:00
exit 1
2023-08-30 08:34:33 +08:00
fi
2023-08-30 12:40:27 +08:00
2024-07-25 01:16:01 +08:00
if command -v pwsh >/dev/null ; then
installed_pwsh_ver=$(pwsh --version)
echo "Install PowerShell $installed_pwsh_ver succeed."
else
2024-07-25 01:16:01 +08:00
echo "Install PowerShell fail, try again"
if [ -f "$pwsh_pkg_out" ] ; then
rm -f "$pwsh_pkg_out"
fi
fi