Skip to content

Commit 3de9cf4

Browse files
Refactor PowerShell installation logic to improve version comparison and uninstallation process across platforms
1 parent 2362a3a commit 3de9cf4

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

action.yml

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,6 @@ runs:
1919
run: |
2020
$version = '${{ inputs.version }}'
2121
22-
# Try to remove existing PowerShell installation
23-
try {
24-
# Find existing PowerShell installations to remove
25-
Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue | ForEach-Object {
26-
Write-Host "Uninstalling $($_.Name) version $($_.Version)"
27-
Uninstall-Package -Name $_.Name -Force -ErrorAction SilentlyContinue
28-
}
29-
} catch {
30-
Write-Host "Error removing existing PowerShell: $_"
31-
}
32-
3322
# Get the version to install
3423
if ([string]::IsNullOrWhiteSpace($version)) {
3524
# Get latest version from GitHub API
@@ -40,6 +29,47 @@ runs:
4029
Write-Host "Installing specified PowerShell version: $version"
4130
}
4231
32+
# Check existing PowerShell installation
33+
$shouldUninstall = $false
34+
try {
35+
$existingPowerShell = Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue
36+
if ($existingPowerShell) {
37+
# Convert versions to System.Version objects for proper comparison
38+
$targetVersion = [System.Version]::new($version)
39+
foreach ($installation in $existingPowerShell) {
40+
$installedVersionStr = $installation.Version
41+
$installedVersion = [System.Version]::new($installedVersionStr)
42+
43+
Write-Host "Found existing PowerShell: $($installation.Name) version $installedVersionStr"
44+
45+
# Only uninstall if target version is lower than installed version
46+
if ($targetVersion -lt $installedVersion) {
47+
Write-Host "Target version $version is lower than installed version $installedVersionStr. Will uninstall."
48+
$shouldUninstall = $true
49+
break
50+
} else {
51+
Write-Host "Target version $version is equal to or higher than installed version $installedVersionStr. No need to uninstall."
52+
}
53+
}
54+
} else {
55+
Write-Host "No existing PowerShell installation found."
56+
}
57+
} catch {
58+
Write-Host "Error checking existing PowerShell: $_"
59+
}
60+
61+
# Remove existing PowerShell installation if needed
62+
if ($shouldUninstall) {
63+
try {
64+
Get-Package -Name "PowerShell*" -ErrorAction SilentlyContinue | ForEach-Object {
65+
Write-Host "Uninstalling $($_.Name) version $($_.Version)"
66+
Uninstall-Package -Name $_.Name -Force -ErrorAction SilentlyContinue
67+
}
68+
} catch {
69+
Write-Host "Error removing existing PowerShell: $_"
70+
}
71+
}
72+
4373
# Download and install PowerShell
4474
$msiName = "PowerShell-$version-win-x64.msi"
4575
$downloadUrl = "https://github.com/PowerShell/PowerShell/releases/download/v$version/$msiName"
@@ -64,15 +94,42 @@ runs:
6494
if: runner.os == 'Linux'
6595
shell: bash
6696
run: |
67-
sudo apt-get remove powershell -y
6897
version='${{ inputs.version }}'
6998
if [ -z "$version" ]; then
99+
# Get latest version if not specified
70100
version=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
101+
echo "Latest PowerShell version: $version"
71102
else
72103
version="v$version"
104+
echo "Installing specified PowerShell version: $version"
73105
fi
74-
wget https://github.com/PowerShell/PowerShell/releases/download/$version/powershell_${version#v}-1.deb_amd64.deb
75-
sudo dpkg -i powershell_${version#v}-1.deb_amd64.deb
106+
107+
# Clean version without v prefix
108+
clean_version=${version#v}
109+
110+
# Check if PowerShell is already installed
111+
if command -v pwsh &> /dev/null; then
112+
installed_version=$(pwsh -NoProfile -Command '$PSVersionTable.PSVersion.ToString()' | tr -d '\r\n')
113+
echo "Found existing PowerShell version: $installed_version"
114+
115+
# Compare versions
116+
target_version=$clean_version
117+
118+
# Only uninstall if target version is lower than installed version
119+
if [ "$(printf '%s\n' "$target_version" "$installed_version" | sort -V | head -n1)" = "$target_version" ] &&
120+
[ "$target_version" != "$installed_version" ]; then
121+
echo "Target version $clean_version is lower than installed version $installed_version. Will uninstall."
122+
sudo apt-get remove powershell -y
123+
else
124+
echo "Target version $clean_version is equal to or higher than installed version $installed_version. No need to uninstall."
125+
fi
126+
else
127+
echo "No existing PowerShell installation found."
128+
fi
129+
130+
# Download and install PowerShell
131+
wget https://github.com/PowerShell/PowerShell/releases/download/$version/powershell_${clean_version}-1.deb_amd64.deb
132+
sudo dpkg -i powershell_${clean_version}-1.deb_amd64.deb
76133
sudo apt-get install -f
77134
78135
- name: Install PowerShell on macOS

0 commit comments

Comments
 (0)