Skip to content

Commit

Permalink
use dpkg for version_compare
Browse files Browse the repository at this point in the history
  • Loading branch information
jpawlowski committed Jul 26, 2024
1 parent 1d45596 commit 4fb0c13
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 79 deletions.
20 changes: 6 additions & 14 deletions src/powershell-extended/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,15 @@ if ! type pwsh >/dev/null 2>&1; then
fi

if [ "$POWERSHELL_UPDATE_PSRESOURCEGET" != 'none' ]; then
if [ "$POWERSHELL_VERSION" = 'latest' ] || ! version_compare 'ge' "$POWERSHELL_VERSION" '7.4.0'; then
if [ "$POWERSHELL_VERSION" = 'latest' ] || ! version_compare "$POWERSHELL_VERSION" 'ge' '7.4.0'; then
# Update Microsoft.PowerShell.PSResourceGet
prerelease=""
if [ "$POWERSHELL_UPDATE_PSRESOURCEGET" = 'prerelease' ]; then
prerelease="-Prerelease"
fi
currentVersion=$(pwsh -NoProfile -Command "(Get-Module -ListAvailable -Name Microsoft.PowerShell.PSResourceGet).Version.ToString()")
if [ -n "$prerelease" ]; then
latestVersion=$(pwsh -NoProfile -Command "(Find-Module -Name Microsoft.PowerShell.PSResourceGet -Repository PSGallery -AllVersions | Sort-Object { [semver]\$_.Version } -Descending | Select-Object -First 1).Version.ToString()")
else
latestVersion=$(pwsh -NoProfile -Command "(Find-Module -Name Microsoft.PowerShell.PSResourceGet -Repository PSGallery -AllVersions | Where-Object { -not \$_.Version.Contains('-') } | Sort-Object { [semver]\$_.Version } -Descending | Select-Object -First 1).Version.ToString()")
fi
if version_compare 'gt' "$latestVersion" "$currentVersion"; then
latestVersion=$(pwsh -NoProfile -Command "(Find-PSResource -Name Microsoft.PowerShell.PSResourceGet -Repository PSGallery -Type Module $prerelease | Sort-Object -Property {[version]\$_.Version} -Descending | Select-Object -First 1).Version.ToString()")
if version_compare "$latestVersion" 'gt' "$currentVersion"; then
echo "Updating Microsoft.PowerShell.PSResourceGet"
pwsh -NoProfile -Command "$prefs; Install-PSResource -Verbose -Repository PSGallery -TrustRepository -Scope AllUsers -Name Microsoft.PowerShell.PSResourceGet $prerelease"
fi
Expand Down Expand Up @@ -320,14 +316,10 @@ if [ -n "$POWERSHELL_UPDATE_PSREADLINE" ]; then
fi

currentVersion=$(pwsh -NoProfile -Command "(Get-Module -ListAvailable -Name PSReadLine).Version.ToString()")
if [ -n "$prerelease" ]; then
latestVersion=$(pwsh -NoProfile -Command "(Find-Module -Name PSReadLine -Repository PSGallery -AllVersions | Sort-Object { [semver]\$_.Version } -Descending | Select-Object -First 1).Version.ToString()")
else
latestVersion=$(pwsh -NoProfile -Command "(Find-Module -Name PSReadLine -Repository PSGallery -AllVersions | Where-Object { -not \$_.Version.Contains('-') } | Sort-Object { [semver]\$_.Version } -Descending | Select-Object -First 1).Version.ToString()")
fi
if version_compare 'gt' "$latestVersion" "$currentVersion"; then
latestVersion=$(pwsh -NoProfile -Command "(Find-PSResource -Name PSReadLine -Repository PSGallery -Type Module $prerelease | Sort-Object -Property {[version]\$_.Version} -Descending | Select-Object -First 1).Version.ToString()")
if version_compare "$latestVersion" 'gt' "$currentVersion"; then
echo "Updating PSReadLine"
pwsh -NoProfile -Command "$prefs; Install-PSResource -Verbose -Repository PSGallery -TrustRepository -Scope AllUsers -Name PSReadLine -Version '$latestVersion' $prerelease"
pwsh -NoProfile -Command "$prefs; Install-PSResource -Verbose -Repository PSGallery -TrustRepository -Scope AllUsers -Name PSReadLine $prerelease"
fi
fi

Expand Down
66 changes: 1 addition & 65 deletions src/powershell-extended/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -219,69 +219,5 @@ function install_using_github() {
}

function version_compare() {
local comparison_type="$1"
local version1="$2"
local version2="$3"

# Function to compare two versions
compare_versions() {
local v1="$1"
local v2="$2"

# Split versions into arrays
IFS='.-' read -r -a v1_parts <<< "$v1"
IFS='.-' read -r -a v2_parts <<< "$v2"

# Compare each part
for ((i=0; i<${#v1_parts[@]}; i++)); do
if [[ -z "${v2_parts[i]}" ]]; then
# If v2 part is missing, v1 is greater
return 0
fi

if [[ "${v1_parts[i]}" =~ ^[0-9]+$ && "${v2_parts[i]}" =~ ^[0-9]+$ ]]; then
# Numeric comparison
if ((10#${v1_parts[i]} > 10#${v2_parts[i]})); then
return 0
elif ((10#${v1_parts[i]} < 10#${v2_parts[i]})); then
return 1
fi
else
# Lexical comparison
if [[ "${v1_parts[i]}" > "${v2_parts[i]}" ]]; then
return 0
elif [[ "${v1_parts[i]}" < "${v2_parts[i]}" ]]; then
return 1
fi
fi
done

# If we reach here, all parts are equal, so compare lengths
if (( ${#v1_parts[@]} > ${#v2_parts[@]} )); then
return 0
elif (( ${#v1_parts[@]} < ${#v2_parts[@]} )); then
return 1
else
return 0
fi
}

# Compare the versions
compare_versions "$version1" "$version2"
local result=$?

# Determine the final result based on comparison type
if [[ "$comparison_type" == "ge" ]]; then
return $result
elif [[ "$comparison_type" == "gt" ]]; then
if [[ $result -eq 0 ]]; then
# If versions are equal, return false for "gt"
return 1
else
return $result
fi
else
echo "Invalid comparison type: $comparison_type"
return 2
fi
dpkg --compare-versions "$1" "$2" "$3"
}

0 comments on commit 4fb0c13

Please sign in to comment.