From a1523c192b5512ef495735b9ae024ec4cbc762c4 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 4 Jan 2024 06:16:02 +0100 Subject: [PATCH] Sync fetch-configlet.ps1 script (#2207) --- bin/fetch-configlet.ps1 | 54 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/bin/fetch-configlet.ps1 b/bin/fetch-configlet.ps1 index ae831961d5..a7896b2251 100644 --- a/bin/fetch-configlet.ps1 +++ b/bin/fetch-configlet.ps1 @@ -1,34 +1,42 @@ +# This file is a copy of the +# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file. +# Please submit bugfixes/improvements to the above file to ensure that all tracks +# benefit from the changes. + $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" -if ($PSVersionTable.PSVersion.Major -le 5) { - # PreserveAuthorizationOnRedirect requires PowerShell Core 7+ - # TODO create a tool that install pwsh in the directory https://docs.microsoft.com/en-us/dotnet/core/tools/local-tools-how-to-use - throw "Please run with PowerShell Core 'pwsh'. If you need to install pwsh globally: 'dotnet tool install --global PowerShell'" -} - -function Get-DownloadUrl($FileName, $RequestOpts) { - $latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest" - $assets = Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @RequestOpts | Select-Object -ExpandProperty assets - return $assets | Where-Object { $_.browser_download_url -match $FileName } | Select-Object -ExpandProperty browser_download_url -First 1 -} - -# Use GITHUB_TOKEN to download the configlet metadata and binaries from github releases for github actions. $requestOpts = @{ Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } } MaximumRetryCount = 3 RetryIntervalSec = 1 } + +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 { $_.name -match "^configlet_.+_windows_${arch}.zip$" } ` + | Select-Object -ExpandProperty browser_download_url -First 1 +} + $outputDirectory = "bin" -$arch = If ([Environment]::Is64BitOperatingSystem) { "64bit" } Else { "32bit" } -$fileName = "configlet-windows-$arch.zip" +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 -Write-Output "Fetching configlet download URL" -$downloadUrl = Get-DownloadUrl -FileName $fileName -RequestOpts $requestOpts -$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName +$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 -Write-Output "Fetching configlet binaries" -# Use PreserveAuthorizationOnRedirect named parameter doesn't work on WSL -Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts -Expand-Archive -Path $outputFile -DestinationPath $outputDirectory -Force -Remove-Item -Path $outputFile +$configletVersion = (Select-String -Pattern "/releases/download/(.+?)/" -InputObject $downloadUrl -AllMatches).Matches.Groups[1].Value +Write-Output "Downloaded configlet ${configletVersion} to ${configletPath}"