From 26304586b446063ea5f7aa74cd82d29fb4828908 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 15 Dec 2023 11:54:10 +0100 Subject: [PATCH] fetch-configlet.ps1: fix for newer PowerShell versions (#841) Under some conditions, running the fetch-configlet PowerShell script could now produce an error like the below: $ pwsh ./fetch-configlet.ps1 Invoke-WebRequest: /mnt/c/Users/bugma/Source/Repos/3rdParty/euphoria/bin/fetch-configlet.ps1:29 Line | 29 | Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts | ~~~~~~~~~~~~ | Cannot validate argument on parameter 'Uri'. The argument is null or | empty. Provide an argument that is not null or empty, and then try | the command again. Fix that, and make the script more similar to the bash one. Changes: - Fix command chaining - Move $arch closer to place of use - Use name field to exactly match filename - Test if script is run from repo root - Add message for downloading - Replace broken expand-archive with ZipFile - Use explicit output filename - Output download info Closes: #839 --- scripts/fetch-configlet.ps1 | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/scripts/fetch-configlet.ps1 b/scripts/fetch-configlet.ps1 index 1892739f..a7896b22 100644 --- a/scripts/fetch-configlet.ps1 +++ b/scripts/fetch-configlet.ps1 @@ -12,20 +12,31 @@ $requestOpts = @{ RetryIntervalSec = 1 } -$arch = If ([Environment]::Is64BitOperatingSystem) { "x86-64" } Else { "i386" } -$fileName = "configlet_.+_windows_$arch.zip" - Function Get-DownloadUrl { + $arch = If ([Environment]::Is64BitOperatingSystem) { "x86-64" } Else { "i386" } $latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest" - Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts - | Select-Object -ExpandProperty assets - | Where-Object { $_.browser_download_url -match $FileName } + Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts ` + | Select-Object -ExpandProperty assets ` + | Where-Object { $_.name -match "^configlet_.+_windows_${arch}.zip$" } ` | Select-Object -ExpandProperty browser_download_url -First 1 } -$downloadUrl = Get-DownloadUrl $outputDirectory = "bin" -$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName -Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts -Expand-Archive $outputFile -DestinationPath $outputDirectory -Force -Remove-Item -Path $outputFile +if (!(Test-Path -Path $outputDirectory)) { + Write-Output "Error: no ./bin directory found. This script should be ran from a repo root." + exit 1 +} + +Write-Output "Fetching configlet..." +$downloadUrl = Get-DownloadUrl +$outputFileName = "configlet.zip" +$outputPath = Join-Path -Path $outputDirectory -ChildPath $outputFileName +Invoke-WebRequest -Uri $downloadUrl -OutFile $outputPath @requestOpts + +$configletPath = Join-Path -Path $outputDirectory -ChildPath "configlet.exe" +if (Test-Path -Path $configletPath) { Remove-Item -Path $configletPath } +[System.IO.Compression.ZipFile]::ExtractToDirectory($outputPath, $outputDirectory) +Remove-Item -Path $outputPath + +$configletVersion = (Select-String -Pattern "/releases/download/(.+?)/" -InputObject $downloadUrl -AllMatches).Matches.Groups[1].Value +Write-Output "Downloaded configlet ${configletVersion} to ${configletPath}"