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
1015param (
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
2732New-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 / 1 MB , 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'
3786try {
3887 Invoke-WebRequest - Uri $url - OutFile $tmpZip - UseBasicParsing
3988} catch {
@@ -44,14 +93,20 @@ Expand-Archive -LiteralPath $tmpZip -DestinationPath $binariesDir -Force
4493Remove-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.
4897if (-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
57112Write-Host " [OK] picotool.exe ready at $outExe ($ ( [math ]::Round($size / 1 MB , 2 )) MB)" - ForegroundColor Green
0 commit comments