Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync fetch-configlet.ps1 script #20

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions bin/fetch-configlet.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
Function DownloadUrl ([string] $FileName, $Headers) {
$latestUrl = "https://api.github.com/repos/exercism/configlet/releases/latest"
$json = Invoke-RestMethod -Headers $Headers -Uri $latestUrl
$json.assets | Where-Object { $_.browser_download_url -match $FileName } | Select-Object -ExpandProperty browser_download_url
}
# 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.

Function Headers {
If ($GITHUB_TOKEN) { @{ Authorization = "Bearer ${GITHUB_TOKEN}" } } Else { @{ } }
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

$requestOpts = @{
Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
MaximumRetryCount = 3
RetryIntervalSec = 1
}

Function Arch {
If ([Environment]::Is64BitOperatingSystem) { "64bit" } Else { "32bit" }
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
}

$arch = Arch
$headers = Headers
$fileName = "configlet-windows-$arch.zip"
$outputDirectory = "bin"
$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName
$zipUrl = DownloadUrl -FileName $fileName -Headers $headers
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

Invoke-WebRequest -Headers $headers -Uri $zipUrl -OutFile $outputFile
Expand-Archive $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}"
Loading