| 1 | #requires -Version 7.0 |
| 2 | [CmdletBinding()] |
| 3 | param([string]$Destination = $env:RUNNER_TEMP) |
| 4 | |
| 5 | $ErrorActionPreference = 'Stop' |
| 6 | if (-not $Destination) { throw 'Set RUNNER_TEMP or pass -Destination for the NSIS tool directory.' } |
| 7 | $version = '3.13' |
| 8 | $expectedSHA256 = 'ba63dffc4410ee89193e1cb5a41989991bd77c61068da17e3156d136b7b0b3d8' |
| 9 | $url = "https://downloads.sourceforge.net/project/nsis/NSIS%203/$version/nsis-$version.zip" |
| 10 | New-Item -ItemType Directory -Path $Destination -Force | Out-Null |
| 11 | $zip = Join-Path $Destination "nsis-$version.zip" |
| 12 | |
| 13 | # SourceForge returns a browser download page (HTTP 200 HTML) for PowerShell's |
| 14 | # default user agent. Use the command-line downloader it redirects to the ZIP. |
| 15 | $curl = if ($IsWindows) { 'curl.exe' } else { 'curl' } |
| 16 | & $curl --fail --location --silent --show-error --retry 3 --connect-timeout 15 --max-time 120 --output $zip --write-out "NSIS download: HTTP %{http_code}, %{content_type}, %{size_download} bytes\n" $url |
| 17 | if ($LASTEXITCODE -ne 0) { throw "NSIS download failed (curl exit $LASTEXITCODE)." } |
| 18 | |
| 19 | $stream = [IO.File]::OpenRead($zip) |
| 20 | try { |
| 21 | $magic = [byte[]]::new(4) |
| 22 | $read = $stream.Read($magic, 0, 4) |
| 23 | } finally { $stream.Dispose() } |
| 24 | if ($read -ne 4 -or [Convert]::ToHexString($magic) -ne '504B0304') { |
| 25 | throw 'NSIS download is not a ZIP. SourceForge may have returned an HTML page; the downloaded content will not be installed.' |
| 26 | } |
| 27 | $actualSHA256 = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash |
| 28 | if ($actualSHA256 -ne $expectedSHA256) { |
| 29 | throw "NSIS checksum mismatch. Expected $expectedSHA256; received $actualSHA256." |
| 30 | } |
| 31 | Expand-Archive -LiteralPath $zip -DestinationPath $Destination -Force |
| 32 | $toolDirectory = Join-Path $Destination "nsis-$version" |
| 33 | if (-not (Test-Path -LiteralPath (Join-Path $toolDirectory 'makensis.exe') -PathType Leaf)) { |
| 34 | throw 'Verified NSIS ZIP did not contain the expected compiler.' |
| 35 | } |
| 36 | if ($env:GITHUB_PATH) { Add-Content -LiteralPath $env:GITHUB_PATH -Value $toolDirectory } |
| 37 | Write-Host "Verified NSIS $version ZIP and extracted the compiler." |