1717 if : runner.os == 'Windows'
1818 shell : powershell
1919 run : |
20- $version = '${{ inputs.version }}'
20+ $version = '${{ inputs.Version }}'
2121
2222 # Get the version to install
2323 if ([string]::IsNullOrWhiteSpace($version)) {
4545 $targetVersionObj = [System.Version]$version
4646
4747 # Compare versions
48- if ($currentVersionObj -ge $targetVersionObj) {
48+ $comparison = $currentVersionObj.CompareTo($targetVersionObj)
49+ if ($comparison -ge 0) {
4950 Write-Host "Current version $currentVersion is greater than or equal to target version $version. Skipping installation."
5051 $needToInstall = $false
5152 } else {
5657 Uninstall-Package -Name $currentInstall.Name -Force -ErrorAction SilentlyContinue
5758 }
5859 } catch {
59- Write-Host "Error comparing versions: $_. Will proceed with installation."
60+ Write-Host "Error comparing versions: $_ Will proceed with installation."
6061 }
6162 } else {
6263 Write-Host "PowerShell Core not found. Will install version $version"
@@ -91,50 +92,67 @@ runs:
9192 if : runner.os == 'Linux'
9293 shell : bash
9394 run : |
94- version='${{ inputs.version }}'
95+ version='${{ inputs.Version }}'
96+
97+ # Function to compare versions
98+ compare_versions() {
99+ local current=$1
100+ local target=$2
101+
102+ # Split versions into arrays
103+ IFS='.' read -ra current_parts <<< "$current"
104+ IFS='.' read -ra target_parts <<< "$target"
105+
106+ # Ensure all parts exist (default to 0 if missing)
107+ for i in {0..2}; do
108+ current_parts[$i]=${current_parts[$i]:-0}
109+ target_parts[$i]=${target_parts[$i]:-0}
110+ done
111+
112+ # Compare major, minor, patch
113+ for i in {0..2}; do
114+ if [ "${current_parts[$i]}" -lt "${target_parts[$i]}" ]; then
115+ return 1 # current < target
116+ elif [ "${current_parts[$i]}" -gt "${target_parts[$i]}" ]; then
117+ return 2 # current > target
118+ fi
119+ done
120+
121+ return 0 # equal
122+ }
123+
95124 if [ -z "$version" ]; then
96125 # Get latest version if not specified
97126 version=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
98127 echo "Latest PowerShell version: $version"
99- else
100- version="v$version"
101- echo "Installing specified PowerShell version: $version"
102128 fi
103129
104- # Strip the 'v' prefix for version comparison
130+ # Always remove 'v' prefix if present
105131 clean_version=${version#v}
132+ echo "Installing PowerShell version: $clean_version"
106133
107134 # Check if PowerShell is already installed and get current version
108135 need_to_install=true
109136 if command -v pwsh &> /dev/null; then
110137 current_version=$(pwsh -c '$PSVersionTable.PSVersion.ToString()' | tr -d '\r\n')
111138 echo "Current PowerShell version: $current_version"
112139
113- # Properly compare versions with version components
114- current_major=$(echo "$current_version" | cut -d'.' -f1)
115- current_minor=$(echo "$current_version" | cut -d'.' -f2)
116- current_patch=$(echo "$current_version" | cut -d'.' -f3)
117-
118- target_major=$(echo "$clean_version" | cut -d'.' -f1)
119- target_minor=$(echo "$clean_version" | cut -d'.' -f2)
120- target_patch=$(echo "$clean_version" | cut -d'.' -f3)
121-
122- # Check if the current version is greater than or equal to target
123- if [ "$current_major" -gt "$target_major" ] ||
124- ([ "$current_major" -eq "$target_major" ] && [ "$current_minor" -gt "$target_minor" ]) ||
125- ([ "$current_major" -eq "$target_major" ] && [ "$current_minor" -eq "$target_minor" ] && [ "$current_patch" -ge "$target_patch" ]); then
126- echo "Current version $current_version is greater than or equal to target version $clean_version. Skipping installation."
127- need_to_install=false
128- else
129- echo "Current version $current_version is lower than target version $clean_version. Will upgrade."
130- sudo apt-get remove powershell -y
131- fi
140+ compare_versions "$current_version" "$clean_version"
141+ case $? in
142+ 0) echo "Versions are equal"; need_to_install=false ;;
143+ 2) echo "Current version is higher"; need_to_install=false ;;
144+ 1) echo "Current version is lower"; need_to_install=true ;;
145+ esac
132146 else
133147 echo "PowerShell is not installed. Will install version $clean_version"
134148 fi
135149
136150 if [ "$need_to_install" = true ]; then
137- wget https://github.com/PowerShell/PowerShell/releases/download/$version/powershell_${clean_version}-1.deb_amd64.deb
151+ # Install prerequisites
152+ sudo apt-get update
153+ sudo apt-get install -y wget apt-transport-https
154+
155+ wget https://github.com/PowerShell/PowerShell/releases/download/v$clean_version/powershell_${clean_version}-1.deb_amd64.deb
138156 sudo dpkg -i powershell_${clean_version}-1.deb_amd64.deb
139157 sudo apt-get install -f
140158
@@ -152,104 +170,85 @@ runs:
152170 if : runner.os == 'macOS'
153171 shell : bash
154172 run : |
155- # Get version information
156- version='${{ inputs.version }}'
173+ version='${{ inputs.Version }}'
174+
175+ # Function to compare versions
176+ compare_versions() {
177+ local current=$1
178+ local target=$2
179+
180+ # Split versions into arrays
181+ IFS='.' read -ra current_parts <<< "$current"
182+ IFS='.' read -ra target_parts <<< "$target"
183+
184+ # Ensure all parts exist (default to 0 if missing)
185+ for i in {0..2}; do
186+ current_parts[$i]=${current_parts[$i]:-0}
187+ target_parts[$i]=${target_parts[$i]:-0}
188+ done
189+
190+ # Compare major, minor, patch
191+ for i in {0..2}; do
192+ if [ "${current_parts[$i]}" -lt "${target_parts[$i]}" ]; then
193+ return 1 # current < target
194+ elif [ "${current_parts[$i]}" -gt "${target_parts[$i]}" ]; then
195+ return 2 # current > target
196+ fi
197+ done
198+
199+ return 0 # equal
200+ }
201+
157202 if [ -z "$version" ]; then
158203 # Get latest version if not specified
159204 version=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep 'tag_name' | cut -d '"' -f 4)
160205 echo "Latest PowerShell version: $version"
161- else
162- version="v$version"
163- echo "Installing specified PowerShell version: $version"
164206 fi
165207
166- # The version without 'v' prefix
208+ # Always remove 'v' prefix if present
167209 clean_version=${version#v}
210+ echo "Installing PowerShell version: $clean_version"
168211
169212 # Check if PowerShell is already installed and get current version
170213 need_to_install=true
171214 if [ -f "/usr/local/bin/pwsh" ]; then
172215 current_version=$(/usr/local/bin/pwsh -c '$PSVersionTable.PSVersion.ToString()' | tr -d '\r\n')
173216 echo "Current PowerShell version: $current_version"
174217
175- # Properly compare versions with version components
176- current_major=$(echo "$current_version" | cut -d'.' -f1)
177- current_minor=$(echo "$current_version" | cut -d'.' -f2)
178- current_patch=$(echo "$current_version" | cut -d'.' -f3)
179-
180- target_major=$(echo "$clean_version" | cut -d'.' -f1)
181- target_minor=$(echo "$clean_version" | cut -d'.' -f2)
182- target_patch=$(echo "$clean_version" | cut -d'.' -f3)
183-
184- # Check if the current version is greater than or equal to target
185- if [ "$current_major" -gt "$target_major" ] ||
186- ([ "$current_major" -eq "$target_major" ] && [ "$current_minor" -gt "$target_minor" ]) ||
187- ([ "$current_major" -eq "$target_major" ] && [ "$current_minor" -eq "$target_minor" ] && [ "$current_patch" -ge "$target_patch" ]); then
188- echo "Current version $current_version is greater than or equal to target version $clean_version. Skipping installation."
189- need_to_install=false
190- else
191- echo "Current version $current_version is lower than target version $clean_version. Will upgrade."
192- # Clean up existing PowerShell installation
193- sudo rm -rf /usr/local/microsoft/powershell
194- fi
218+ compare_versions "$current_version" "$clean_version"
219+ case $? in
220+ 0) echo "Versions are equal"; need_to_install=false ;;
221+ 2) echo "Current version is higher"; need_to_install=false ;;
222+ 1) echo "Current version is lower"; need_to_install=true ;;
223+ esac
195224 else
196225 echo "PowerShell is not installed. Will install version $clean_version"
197226 fi
198227
199228 if [ "$need_to_install" = true ]; then
200- # Try the different naming patterns for the macOS package
201- pkg_patterns=(
202- "powershell-$clean_version-osx-x64.pkg"
203- "powershell-$clean_version-osx-arm64.pkg"
204- "powershell-$clean_version-osx.pkg"
205- )
206-
207- # Determine if running on Apple Silicon or Intel
229+ # Determine architecture
208230 if [ "$(uname -m)" = "arm64" ]; then
209231 echo "Detected Apple Silicon (ARM64)"
210- arch=" arm64"
232+ pkg_name="powershell-$clean_version-osx- arm64.pkg "
211233 else
212234 echo "Detected Intel CPU (x64)"
213- arch=" x64"
235+ pkg_name="powershell-$clean_version-osx- x64.pkg "
214236 fi
215237
216- # Try to download the appropriate package based on architecture
217- download_success=false
218-
219- for pkg_name in "${pkg_patterns[@]}"; do
220- echo "Trying to download $pkg_name..."
221- url="https://github.com/PowerShell/PowerShell/releases/download/$version/$pkg_name"
222-
223- # Try to download the file
224- if curl -LO $url --fail; then
225- echo "Successfully downloaded $pkg_name"
226- download_success=true
227- break
228- else
229- echo "Failed to download $pkg_name"
230- fi
231- done
232-
233- if [ "$download_success" = false ]; then
234- echo "ERROR: Could not download PowerShell package for macOS. Please check version and available packages."
235- exit 1
236- fi
238+ # Download the package
239+ url="https://github.com/PowerShell/PowerShell/releases/download/v$clean_version/$pkg_name"
240+ echo "Downloading from $url"
241+ curl -LO $url
242+
243+ # Install the package
244+ sudo installer -pkg "$pkg_name" -target /
237245
238- # Verify the file exists before installation
239- if [ -f "$pkg_name" ]; then
240- echo "Installing PowerShell package: $pkg_name"
241- sudo installer -pkg "$pkg_name" -target /
242-
243- # Verify the installation
244- if [ -f "/usr/local/bin/pwsh" ]; then
245- echo "PowerShell installed successfully:"
246- /usr/local/bin/pwsh -Command '$PSVersionTable'
247- else
248- echo "ERROR: PowerShell installation failed. Could not find /usr/local/bin/pwsh"
249- exit 1
250- fi
246+ # Verify the installation
247+ if [ -f "/usr/local/bin/pwsh" ]; then
248+ echo "PowerShell installed successfully:"
249+ /usr/local/bin/pwsh -Command '$PSVersionTable'
251250 else
252- echo "ERROR: Downloaded package file not found: $pkg_name "
251+ echo "ERROR: PowerShell installation failed. Could not find /usr/local/bin/pwsh "
253252 exit 1
254253 fi
255254 fi
0 commit comments