Skip to content

Commit a1523c1

Browse files
Sync fetch-configlet.ps1 script (#2207)
1 parent a3f72d9 commit a1523c1

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

bin/fetch-configlet.ps1

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
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+
16
$ErrorActionPreference = "Stop"
27
$ProgressPreference = "SilentlyContinue"
38

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.
179
$requestOpts = @{
1810
Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
1911
MaximumRetryCount = 3
2012
RetryIntervalSec = 1
2113
}
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+
2224
$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
2535

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
2940

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

Comments
 (0)