Skip to content

Commit 1d1458d

Browse files
Sync fetch-configlet.ps1 script (#9)
1 parent 30b8d20 commit 1d1458d

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

bin/fetch-configlet.ps1

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,31 @@ $requestOpts = @{
1212
RetryIntervalSec = 1
1313
}
1414

15-
$arch = If ([Environment]::Is64BitOperatingSystem) { "x86-64" } Else { "i386" }
16-
$fileName = "configlet_.+_windows_$arch.zip"
17-
1815
Function Get-DownloadUrl {
16+
$arch = If ([Environment]::Is64BitOperatingSystem) { "x86-64" } Else { "i386" }
1917
$latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest"
20-
Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts
21-
| Select-Object -ExpandProperty assets
22-
| Where-Object { $_.browser_download_url -match $FileName }
23-
| Select-Object -ExpandProperty browser_download_url
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
2422
}
2523

26-
$downloadUrl = Get-DownloadUrl
2724
$outputDirectory = "bin"
28-
$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName
29-
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts
30-
Expand-Archive $outputFile -DestinationPath $outputDirectory -Force
31-
Remove-Item -Path $outputFile
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
35+
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
40+
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)