Skip to content

Commit 442eb22

Browse files
committed
build(picotool): fetch latest release via GitHub API, not pinned version
scripts/fetch-picotool.ps1 was hardcoded to v2.2.0-3 with a literal asset filename. Replace that with a GitHub API call to the .../releases/latest endpoint that picks the picotool-*-x64-win.zip asset out of the response. -Version <tag> still pins to a specific release, and a new -AssetUrl <url> bypasses the API entirely if the runner can't reach api.github.com. The defaults pull whatever pico-sdk-tools tagged most recently, so we stop ageing-out as upstream cuts new builds.
1 parent a3d5a36 commit 442eb22

1 file changed

Lines changed: 63 additions & 8 deletions

File tree

scripts/fetch-picotool.ps1

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
# build (`go build -tags embed_picotool`) can embed it. Default builds
55
# don't need this -- they fall back to the host PATH.
66
#
7+
# Resolution order:
8+
# 1. -AssetUrl <url> -- direct download URL (skips API lookup).
9+
# 2. -Version <ver> -- pin to that pico-sdk-tools release tag.
10+
# 3. Latest release on raspberrypi/pico-sdk-tools (the default).
11+
#
712
# Idempotent: skips the download if the target already exists. Pass
813
# -Force to re-fetch.
914

1015
param(
11-
[string]$Version = "2.2.0-3",
12-
[string]$AssetName = "picotool-2.2.0-a4-x64-win.zip",
16+
[string]$Version = "", # e.g. "2.2.0-3" to pin; empty = latest
17+
[string]$AssetUrl = "", # direct URL override
1318
[switch]$Force
1419
)
1520

@@ -26,14 +31,58 @@ if ((Test-Path -LiteralPath $outExe) -and (-not $Force)) {
2631

2732
New-Item -ItemType Directory -Force -Path $binariesDir | Out-Null
2833

29-
$url = "https://github.com/raspberrypi/pico-sdk-tools/releases/download/v$Version/$AssetName"
30-
Write-Host "fetching $url" -ForegroundColor Cyan
31-
3234
# TLS 1.2 for Windows PowerShell 5.1.
3335
[Net.ServicePointManager]::SecurityProtocol = `
3436
[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
3537

36-
$tmpZip = Join-Path $binariesDir $AssetName
38+
# Resolve the asset URL. Priority: explicit -AssetUrl > -Version pin > latest.
39+
function Resolve-PicotoolAssetUrl {
40+
param([string]$RequestedVersion)
41+
42+
$api = if ($RequestedVersion) {
43+
# Tag URL pattern: tags on this repo are 'v2.2.0-3' shape.
44+
$tag = if ($RequestedVersion.StartsWith('v')) { $RequestedVersion } else { "v$RequestedVersion" }
45+
"https://api.github.com/repos/raspberrypi/pico-sdk-tools/releases/tags/$tag"
46+
} else {
47+
"https://api.github.com/repos/raspberrypi/pico-sdk-tools/releases/latest"
48+
}
49+
50+
$headers = @{ 'User-Agent' = 'Handoff-fetch-picotool' }
51+
if ($env:GITHUB_TOKEN) { $headers['Authorization'] = "Bearer $env:GITHUB_TOKEN" }
52+
53+
Write-Host "querying $api" -ForegroundColor Cyan
54+
try {
55+
$release = Invoke-RestMethod -Uri $api -Headers $headers -UseBasicParsing
56+
} catch {
57+
throw "GitHub API query failed: $($_.Exception.Message)"
58+
}
59+
60+
# Find the x64 Windows picotool asset. Real-world names follow the
61+
# 'picotool-<ver>-x64-win.zip' shape; match that to ignore the Linux
62+
# / macOS / aarch64 assets that ship in the same release.
63+
$asset = $release.assets | Where-Object {
64+
$_.name -match '^picotool-.*-x64-win\.zip$'
65+
} | Select-Object -First 1
66+
67+
if ($null -eq $asset) {
68+
$names = ($release.assets | ForEach-Object { $_.name }) -join ', '
69+
throw "no x64-win picotool asset in release $($release.tag_name); available assets: $names"
70+
}
71+
72+
Write-Host "release : $($release.tag_name)" -ForegroundColor Gray
73+
Write-Host "asset : $($asset.name) ($([math]::Round($asset.size / 1MB, 2)) MB)" -ForegroundColor Gray
74+
return $asset.browser_download_url
75+
}
76+
77+
if ($AssetUrl) {
78+
$url = $AssetUrl
79+
Write-Host "using explicit asset url" -ForegroundColor Cyan
80+
} else {
81+
$url = Resolve-PicotoolAssetUrl -RequestedVersion $Version
82+
}
83+
84+
Write-Host "fetching $url" -ForegroundColor Cyan
85+
$tmpZip = Join-Path $binariesDir 'picotool-download.zip'
3786
try {
3887
Invoke-WebRequest -Uri $url -OutFile $tmpZip -UseBasicParsing
3988
} catch {
@@ -44,14 +93,20 @@ Expand-Archive -LiteralPath $tmpZip -DestinationPath $binariesDir -Force
4493
Remove-Item -LiteralPath $tmpZip -Force
4594

4695
# Some sdk-tools releases nest picotool.exe inside a versioned subfolder;
47-
# find it and lift it to the canonical location.
96+
# find it and lift it to the canonical location. Then prune the nested dir.
4897
if (-not (Test-Path -LiteralPath $outExe)) {
49-
$found = Get-ChildItem -LiteralPath $binariesDir -Recurse -File -Filter picotool.exe | Select-Object -First 1
98+
$found = Get-ChildItem -LiteralPath $binariesDir -Recurse -File -Filter picotool.exe |
99+
Select-Object -First 1
50100
if ($null -eq $found) {
51101
throw "picotool.exe not found in extracted archive at $binariesDir"
52102
}
53103
Copy-Item -LiteralPath $found.FullName -Destination $outExe -Force
54104
}
55105

106+
# Sweep any unpacked subdirectories so the binaries/ folder only ever
107+
# holds picotool.exe + .keep. Otherwise repeated runs leave stale dirs.
108+
Get-ChildItem -LiteralPath $binariesDir -Directory -ErrorAction SilentlyContinue |
109+
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Recurse -Force }
110+
56111
$size = (Get-Item -LiteralPath $outExe).Length
57112
Write-Host "[OK] picotool.exe ready at $outExe ($([math]::Round($size / 1MB, 2)) MB)" -ForegroundColor Green

0 commit comments

Comments
 (0)