Simplify update logic in Get-WinUtilUpdates function

This commit is contained in:
Cryostrixx 2024-09-23 02:22:31 -07:00
parent 683e681a9a
commit 602a15cad8
No known key found for this signature in database
GPG Key ID: 2FC11420AFB82801

View File

@ -98,23 +98,20 @@ function Get-WinUtilUpdates {
# Make a web request to the latest WinUtil release URL and store the script's raw code for processing. # Make a web request to the latest WinUtil release URL and store the script's raw code for processing.
$RawWinUtilScript = (Invoke-WebRequest $WinUtilReleaseURL -UseBasicParsing).RawContent $RawWinUtilScript = (Invoke-WebRequest $WinUtilReleaseURL -UseBasicParsing).RawContent
# Match the 'Version:' header and replace it with an empty string, leaving the script's version number behind. # Create the regular expression pattern used to extract the script's version number from the script's raw content.
$RemoteWinUtilVersion = ([regex]"\bVersion\s*:\s[\d.]+").Match($RawWinUtilScript).Value -replace ".*:\s", "" $VersionExtractionRegEx = "(\bVersion\s*:\s)([\d.]+)"
$LocalWinUtilVersion = ([regex]"\bVersion\s*:\s[\d.]+").Match((Get-Content $WinUtilScriptPath)).Value -replace ".*:\s", ""
# If WinUtil has been upgraded since the last time it has been launched, re-download the WinUtil script. # Match the entire 'Version:' header and extract the script's version number directly using RegEx capture groups.
if ([version]$RemoteWinUtilVersion -gt [version]$LocalWinUtilVersion) { $RemoteWinUtilVersion = (([regex]($VersionExtractionRegEx)).Match($RawWinUtilScript).Groups[2].Value)
Write-Host "WinUtil has been upgraded since the last time it was launched. Downloading '$($RemoteWinUtilVersion)'..." -ForegroundColor DarkYellow $LocalWinUtilVersion = (([regex]($VersionExtractionRegEx)).Match((Get-Content $WinUtilScriptPath)).Groups[2].Value)
# If WinUtil has been modified since the last time it was launched, download a fresh copy of the script.
if ([version]$RemoteWinUtilVersion -ne [version]$LocalWinUtilVersion) {
Write-Host "WinUtil is out-of-date. Downloading WinUtil '$($RemoteWinUtilVersion)'..." -ForegroundColor DarkYellow
Invoke-RestMethod $WinUtilReleaseURL -OutFile $WinUtilScriptPath Invoke-RestMethod $WinUtilReleaseURL -OutFile $WinUtilScriptPath
} }
# If WinUtil has been downgraded since the last time it has been launched, re-download the WinUtil script. # If WinUtil has not been modified, skip updating the script and let the user know it is already up-to-date.
if ([version]$RemoteWinUtilVersion -lt [version]$LocalWinUtilVersion) {
Write-Host "WinUtil has been downgraded since the last time it was launched. Downloading '$($RemoteWinUtilVersion)'..." -ForegroundColor DarkYellow
Invoke-RestMethod $WinUtilReleaseURL -OutFile $WinUtilScriptPath
}
# If WinUtil is already up-to-date, skip updating the script and let the user know it is already up-to-date.
if ([version]$RemoteWinUtilVersion -eq [version]$LocalWinUtilVersion) { if ([version]$RemoteWinUtilVersion -eq [version]$LocalWinUtilVersion) {
Write-Host "WinUtil is already up-to-date. Skipped update checking." -ForegroundColor Yellow Write-Host "WinUtil is already up-to-date. Skipped update checking." -ForegroundColor Yellow
} }