|
| 1 | +# This file is a copy of the |
| 2 | +# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file. |
| 3 | +# Please submit bugfixes/improvements to the above file to ensure that all tracks |
| 4 | +# benefit from the changes. |
| 5 | + |
1 | 6 | $ErrorActionPreference = "Stop"
|
2 | 7 | $ProgressPreference = "SilentlyContinue"
|
3 | 8 |
|
4 |
| -if ($PSVersionTable.PSVersion.Major -le 5) { |
5 |
| - # PreserveAuthorizationOnRedirect requires PowerShell Core 7+ |
6 |
| - # TODO create a tool that install pwsh in the directory https://docs.microsoft.com/en-us/dotnet/core/tools/local-tools-how-to-use |
7 |
| - throw "Please run with PowerShell Core 'pwsh'. If you need to install pwsh globally: 'dotnet tool install --global PowerShell'" |
8 |
| -} |
9 |
| - |
10 |
| -function Get-DownloadUrl($FileName, $RequestOpts) { |
11 |
| - $latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest" |
12 |
| - $assets = Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @RequestOpts | Select-Object -ExpandProperty assets |
13 |
| - return $assets | Where-Object { $_.browser_download_url -match $FileName } | Select-Object -ExpandProperty browser_download_url -First 1 |
14 |
| -} |
15 |
| - |
16 |
| -# Use GITHUB_TOKEN to download the configlet metadata and binaries from github releases for github actions. |
17 | 9 | $requestOpts = @{
|
18 | 10 | Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
|
19 | 11 | MaximumRetryCount = 3
|
20 | 12 | RetryIntervalSec = 1
|
21 | 13 | }
|
| 14 | + |
| 15 | +Function Get-DownloadUrl { |
| 16 | + $arch = If ([Environment]::Is64BitOperatingSystem) { "x86-64" } Else { "i386" } |
| 17 | + $latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest" |
| 18 | + Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts ` |
| 19 | + | Select-Object -ExpandProperty assets ` |
| 20 | + | Where-Object { $_.name -match "^configlet_.+_windows_${arch}.zip$" } ` |
| 21 | + | Select-Object -ExpandProperty browser_download_url -First 1 |
| 22 | +} |
| 23 | + |
22 | 24 | $outputDirectory = "bin"
|
23 |
| -$arch = If ([Environment]::Is64BitOperatingSystem) { "64bit" } Else { "32bit" } |
24 |
| -$fileName = "configlet-windows-$arch.zip" |
| 25 | +if (!(Test-Path -Path $outputDirectory)) { |
| 26 | + Write-Output "Error: no ./bin directory found. This script should be ran from a repo root." |
| 27 | + exit 1 |
| 28 | +} |
| 29 | + |
| 30 | +Write-Output "Fetching configlet..." |
| 31 | +$downloadUrl = Get-DownloadUrl |
| 32 | +$outputFileName = "configlet.zip" |
| 33 | +$outputPath = Join-Path -Path $outputDirectory -ChildPath $outputFileName |
| 34 | +Invoke-WebRequest -Uri $downloadUrl -OutFile $outputPath @requestOpts |
25 | 35 |
|
26 |
| -Write-Output "Fetching configlet download URL" |
27 |
| -$downloadUrl = Get-DownloadUrl -FileName $fileName -RequestOpts $requestOpts |
28 |
| -$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName |
| 36 | +$configletPath = Join-Path -Path $outputDirectory -ChildPath "configlet.exe" |
| 37 | +if (Test-Path -Path $configletPath) { Remove-Item -Path $configletPath } |
| 38 | +[System.IO.Compression.ZipFile]::ExtractToDirectory($outputPath, $outputDirectory) |
| 39 | +Remove-Item -Path $outputPath |
29 | 40 |
|
30 |
| -Write-Output "Fetching configlet binaries" |
31 |
| -# Use PreserveAuthorizationOnRedirect named parameter doesn't work on WSL |
32 |
| -Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts |
33 |
| -Expand-Archive -Path $outputFile -DestinationPath $outputDirectory -Force |
34 |
| -Remove-Item -Path $outputFile |
| 41 | +$configletVersion = (Select-String -Pattern "/releases/download/(.+?)/" -InputObject $downloadUrl -AllMatches).Matches.Groups[1].Value |
| 42 | +Write-Output "Downloaded configlet ${configletVersion} to ${configletPath}" |
0 commit comments