| 1 | #requires -Version 7.0 |
| 2 | [CmdletBinding()] |
| 3 | param( |
| 4 | [switch]$Release, |
| 5 | [switch]$Sign, |
| 6 | [switch]$SkipBuild, |
| 7 | [string]$AppDirectory, |
| 8 | [string]$OutputDirectory, |
| 9 | [string]$QemuSourceArchive |
| 10 | ) |
| 11 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 12 | $repository = Split-Path $PSScriptRoot -Parent |
| 13 | $version = Get-CageVersion $repository |
| 14 | $signed = $Release -or $Sign |
| 15 | if (-not $AppDirectory) { $AppDirectory = Join-Path $repository '_output/windows-amd64/app' } |
| 16 | if (-not $OutputDirectory) { $OutputDirectory = Join-Path $repository "_output/releases/$version/windows" } |
| 17 | $AppDirectory = [IO.Path]::GetFullPath($AppDirectory) |
| 18 | $OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory) |
| 19 | if (Test-Path -LiteralPath (Join-Path $AppDirectory 'CageData')) { throw 'App directory contains CageData user data. Package a clean build directory; do not package an installed application.' } |
| 20 | if (-not $SkipBuild) { & (Join-Path $PSScriptRoot 'build-windows.ps1') -Release:$Release -Sign:$Sign -OutputDirectory $AppDirectory } |
| 21 | if ((Get-Content -LiteralPath (Join-Path $AppDirectory 'Cage.install-id') -Raw) -ne 'io.github.lee-xeo.claude-cage') { throw 'Invalid Cage app directory.' } |
| 22 | if ((Get-Content -LiteralPath (Join-Path $AppDirectory 'version.txt') -Raw).Trim() -ne $version) { throw 'App version differs from VERSION; rebuild before packaging.' } |
| 23 | $makeNSIS = Get-BuildCommand @('makensis.exe', 'makensis') |
| 24 | $sevenZip = Get-BuildCommand @('7z.exe', '7zz', '7z') |
| 25 | if ($IsWindows) { |
| 26 | if ([Diagnostics.FileVersionInfo]::GetVersionInfo((Join-Path $AppDirectory 'Cage.exe')).ProductVersion -ne $version) { throw 'Cage.exe version differs from VERSION; rebuild before packaging.' } |
| 27 | Invoke-BuildTool (Join-Path $AppDirectory 'resources/cage.exe') @('verify-vm-assets') |
| 28 | Invoke-BuildTool (Join-Path $AppDirectory 'resources/cage.exe') @('verify-web-assets') |
| 29 | if ($SkipBuild) { Assert-WindowsWorkbench $AppDirectory } |
| 30 | } else { |
| 31 | $metadata = & $sevenZip 'l' '-tPE' (Join-Path $AppDirectory 'Cage.exe') |
| 32 | if ($LASTEXITCODE -ne 0 -or -not ($metadata -match ('^ProductVersion: ' + [regex]::Escape($version) + '$'))) { throw 'Cage.exe version differs from VERSION; rebuild before packaging.' } |
| 33 | } |
| 34 | $signTool = $null |
| 35 | if ($signed) { |
| 36 | Assert-WindowsSigningConfiguration |
| 37 | if ($env:CAGE_WINDOWS_CERT_SHA1 -notmatch '^[0-9A-Fa-f]{40}$') { throw 'Build and verify the signed app before packaging.' } |
| 38 | if (Test-Path -LiteralPath (Join-Path $AppDirectory 'DEVELOPMENT-BUILD.txt')) { throw 'Rebuild with build-windows.ps1 -Sign or -Release before packaging a signed installer.' } |
| 39 | if ($Release -and (-not $QemuSourceArchive -or -not (Test-Path -LiteralPath $QemuSourceArchive -PathType Leaf))) { |
| 40 | throw 'Release packaging requires -QemuSourceArchive containing the exact QEMU, firmware and linked library sources. Generate it from the built runtime with bundle-qemu-sources.sh.' |
| 41 | } |
| 42 | if ($Release) { Invoke-BuildTool $sevenZip @('t', '-bso0', '-bsp0', $QemuSourceArchive) } |
| 43 | $signTool = Get-SigningTool |
| 44 | foreach ($file in @('Cage.exe', 'resources/cage.exe', 'resources/mihomo.exe')) { |
| 45 | $signature = Get-AuthenticodeSignature -LiteralPath (Join-Path $AppDirectory $file) |
| 46 | if ($signature.Status -ne 'Valid' -or $signature.SignerCertificate.Thumbprint -ne $env:CAGE_WINDOWS_CERT_SHA1) { throw "Invalid release signature: $file" } |
| 47 | } |
| 48 | $webview = Get-AuthenticodeSignature -LiteralPath (Join-Path $AppDirectory 'resources/runtime/MicrosoftEdgeWebView2RuntimeInstallerX64.exe') |
| 49 | if ($webview.Status -ne 'Valid' -or $webview.SignerCertificate.Subject -notmatch 'O=Microsoft Corporation') { throw 'WebView2 installer must have a valid Microsoft signature.' } |
| 50 | } |
| 51 | New-Item -ItemType Directory -Path $OutputDirectory -Force | Out-Null |
| 52 | # Verify source material before the expensive installer compression. |
| 53 | $dependencies = Get-Content -LiteralPath (Join-Path $repository 'packaging/windows/dependencies.json') -Raw | ConvertFrom-Json |
| 54 | $qemuTar = Get-VerifiedGitArchive $dependencies.qemuSource (Join-Path $repository '_output/windows-deps') |
| 55 | $suffix = if ($Release) { '' } elseif ($Sign) { '-test-signed' } else { '-unsigned' } |
| 56 | $artifact = Join-Path $OutputDirectory "Cage-$version-windows-amd64-setup$suffix.exe" |
| 57 | $manifest = Join-Path $AppDirectory 'Cage.install-files.json' |
| 58 | $files = @(Get-ChildItem -LiteralPath $AppDirectory -File -Recurse -Force | Where-Object FullName -ne $manifest) |
| 59 | $ownership = [Collections.Generic.List[object]]::new() |
| 60 | foreach ($file in $files) { |
| 61 | $relative = [IO.Path]::GetRelativePath($AppDirectory, $file.FullName).Replace('\', '/') |
| 62 | $ownership.Add(@{ path = $relative; sha256 = (Get-FileHash -LiteralPath $file.FullName -Algorithm SHA256).Hash.ToLowerInvariant() }) |
| 63 | } |
| 64 | @{ version = 1; files = @($ownership | Sort-Object path) } | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $manifest -Encoding utf8 |
| 65 | $numericVersion = ($version -split '-')[0] + '.0' |
| 66 | $prefix = if ($IsWindows) { '/' } else { '-' } |
| 67 | # Source files are UTF-8; Windows ACP must not reinterpret Chinese UI text. |
| 68 | $arguments = @("${prefix}V3", "${prefix}INPUTCHARSET", 'UTF8', "${prefix}DAPP_DIR=$AppDirectory", "${prefix}DVERSION=$version", "${prefix}DVERSION_NUMERIC=$numericVersion", "${prefix}DOUTPUT_FILE=$artifact", "${prefix}DFILE_MANIFEST=$manifest") |
| 69 | if ($signed) { |
| 70 | $arguments += @("${prefix}DSIGN_PWSH=$((Get-Process -Id $PID).Path)", "${prefix}DSIGN_SCRIPT=$(Join-Path $PSScriptRoot 'sign-windows-file.ps1')") |
| 71 | } |
| 72 | $arguments += (Join-Path $repository 'packaging/windows/Cage.nsi') |
| 73 | Invoke-BuildTool $makeNSIS $arguments |
| 74 | Invoke-BuildTool (Get-BuildCommand @('node.exe', 'node')) @( |
| 75 | (Join-Path $PSScriptRoot 'verify-windows-installer-icon.mjs'), |
| 76 | $artifact, (Join-Path $repository 'packaging/windows/Cage.ico'), $sevenZip |
| 77 | ) |
| 78 | if ($signed) { Sign-CageFile $artifact $signTool } |
| 79 | if ((Get-Item -LiteralPath $artifact).Length -gt 512MB) { throw 'Installer exceeds the application updater 512 MiB limit.' } |
| 80 | |
| 81 | # Source materials travel beside the binary, not inside the application directory. |
| 82 | $coreSource = Join-Path $OutputDirectory "mihomo-$($dependencies.mihomo.version)-source.tar.gz" |
| 83 | $verifiedCoreSource = Get-VerifiedDependency $dependencies.mihomoSource (Join-Path $repository '_output/windows-deps') |
| 84 | Copy-Item -LiteralPath $verifiedCoreSource -Destination $coreSource -Force |
| 85 | Invoke-BuildTool $sevenZip @('t', '-bso0', '-bsp0', $coreSource) |
| 86 | $releaseFiles = @($artifact, $coreSource) |
| 87 | $qemuSource = Join-Path $OutputDirectory ($dependencies.qemuSource.file + '.gz') |
| 88 | if (Test-Path -LiteralPath $qemuSource) { Remove-Item -LiteralPath $qemuSource } |
| 89 | Invoke-BuildTool $sevenZip @('a', '-tgzip', '-mx=9', '-bso0', '-bsp0', $qemuSource, $qemuTar) |
| 90 | Invoke-BuildTool $sevenZip @('t', '-bso0', '-bsp0', $qemuSource) |
| 91 | $releaseFiles += $qemuSource |
| 92 | $qemuRuntime = Join-Path $AppDirectory 'resources/runtime/qemu' |
| 93 | if (Test-Path (Join-Path $qemuRuntime 'cage-build.json')) { |
| 94 | $keymapArchive = Get-VerifiedDependency $dependencies.qemuKeycodemap (Join-Path $repository '_output/windows-deps') |
| 95 | $keymapSource = Join-Path $OutputDirectory ('qemu-keycodemapdb-' + $dependencies.qemuKeycodemap.version + '-source.tar.gz') |
| 96 | Copy-Item $keymapArchive $keymapSource -Force |
| 97 | $changesSource = Join-Path $OutputDirectory ('qemu-' + $dependencies.qemu.version + '-cage-changes-source.tar.gz') |
| 98 | Invoke-BuildTool 'tar' @('-czf', $changesSource, '-C', $repository, 'patches/qemu', 'scripts/build-qemu-windows.ps1', |
| 99 | 'scripts/build-qemu-windows.sh', '-C', $qemuRuntime, 'cage-build.json', 'build-packages.txt') |
| 100 | $releaseFiles += @($keymapSource, $changesSource) |
| 101 | } |
| 102 | if ($Release) { |
| 103 | $sourceExtension = if ($QemuSourceArchive.EndsWith('.tar.gz', [StringComparison]::OrdinalIgnoreCase)) { '.tar.gz' } else { [IO.Path]::GetExtension($QemuSourceArchive) } |
| 104 | $qemuSourceTarget = Join-Path $OutputDirectory ('qemu-' + $dependencies.qemu.version + '-windows-full-source' + $sourceExtension) |
| 105 | Copy-Item -LiteralPath $QemuSourceArchive -Destination $qemuSourceTarget -Force |
| 106 | $releaseFiles += $qemuSourceTarget |
| 107 | } |
| 108 | $sums = foreach ($file in $releaseFiles) { (Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash.ToLowerInvariant() + ' ' + [IO.Path]::GetFileName($file) } |
| 109 | [IO.File]::WriteAllLines((Join-Path $OutputDirectory 'SHA256SUMS'), $sums) |
| 110 | Write-Host "Windows installer: $artifact" |
| 111 | if (-not $signed) { Write-Warning 'Unsigned development installer. Do not publish it as an automatic update; Windows VM and installation acceptance are still required.' } |