go-win.ps1 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <#
  2. go.ps1 – Tailscale Go toolchain fetching wrapper for Windows/PowerShell
  3. • Reads go.toolchain.rev one dir above this script
  4. • If the requested commit hash isn't cached, downloads and unpacks
  5. https://github.com/tailscale/go/releases/download/build-${REV}/${OS}-${ARCH}.tar.gz
  6. • Finally execs the toolchain's "go" binary, forwarding all args & exit-code
  7. #>
  8. param(
  9. [Parameter(ValueFromRemainingArguments = $true)]
  10. [string[]] $Args
  11. )
  12. Set-StrictMode -Version Latest
  13. $ErrorActionPreference = 'Stop'
  14. if ($env:CI -eq 'true' -and $env:NODEBUG -ne 'true') {
  15. $VerbosePreference = 'Continue'
  16. }
  17. $repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..')
  18. $REV = (Get-Content (Join-Path $repoRoot 'go.toolchain.rev') -Raw).Trim()
  19. if ([IO.Path]::IsPathRooted($REV)) {
  20. $toolchain = $REV
  21. } else {
  22. if (-not [string]::IsNullOrWhiteSpace($env:TSGO_CACHE_ROOT)) {
  23. $cacheRoot = $env:TSGO_CACHE_ROOT
  24. } else {
  25. $cacheRoot = Join-Path $env:USERPROFILE '.cache\tsgo'
  26. }
  27. $toolchain = Join-Path $cacheRoot $REV
  28. $marker = "$toolchain.extracted"
  29. if (-not (Test-Path $marker)) {
  30. Write-Host "# Downloading Go toolchain $REV" -ForegroundColor Cyan
  31. if (Test-Path $toolchain) { Remove-Item -Recurse -Force $toolchain }
  32. # Removing the marker file again (even though it shouldn't still exist)
  33. # because the equivalent Bash script also does so (to guard against
  34. # concurrent cache fills?).
  35. # TODO(bradfitz): remove this and add some proper locking instead?
  36. if (Test-Path $marker ) { Remove-Item -Force $marker }
  37. New-Item -ItemType Directory -Path $cacheRoot -Force | Out-Null
  38. $url = "https://github.com/tailscale/go/releases/download/build-$REV/windows-amd64.tar.gz"
  39. $tgz = "$toolchain.tar.gz"
  40. Invoke-WebRequest -Uri $url -OutFile $tgz -UseBasicParsing -ErrorAction Stop
  41. New-Item -ItemType Directory -Path $toolchain -Force | Out-Null
  42. tar --strip-components=1 -xzf $tgz -C $toolchain
  43. Remove-Item $tgz
  44. Set-Content -Path $marker -Value $REV
  45. }
  46. }
  47. $goExe = Join-Path $toolchain 'bin\go.exe'
  48. if (-not (Test-Path $goExe)) { throw "go executable not found at $goExe" }
  49. & $goExe @Args
  50. exit $LASTEXITCODE