Files
marqdo-app-harness/scripts/package-windows-zip.ps1
cflmy 18be7c04e7
ci/woodpecker/push/ci Pipeline failed
ci/woodpecker/pr/ci Pipeline was successful
build: ship a formal Windows portable ZIP with a no-console launcher
The portable artifact is MQDH-<version>-win-x64.zip: extract and double-click MQDH.exe; the thin GUI launcher installs the bundled offline runtime and opens the inherited DSH Web UI. Adds the launcher build/packaging scripts, a portable-zip release-manifest format, docs and naming test.
2026-12-03 09:25:25 +08:00

155 lines
7.8 KiB
PowerShell

# Build the formal MQDH Windows portable ZIP from a verified application tree.
#
# The ZIP is a ready-to-run portable distribution: extract, double-click
# MQDH.exe, and the inherited DSH Web UI opens. It does not require PowerShell,
# install.ps1 or a terminal. The application tree itself comes from the verified
# Windows build (package-windows.ps1 / release archive); this script only adds
# the thin launcher and packages the bytes.
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$AppArchive,
[string]$OutDir = (Join-Path $PSScriptRoot '..\.ci'),
[string]$SourceSha = '',
[string]$LauncherExe = '',
[string]$ZipTool = ''
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Find-7z {
param([string]$Configured)
if ($Configured -ne '' -and (Test-Path -LiteralPath $Configured)) { return (Resolve-Path -LiteralPath $Configured).Path }
$command = Get-Command '7z.exe' -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
foreach ($candidate in @('C:\Program Files\7-Zip\7z.exe', 'C:\Program Files (x86)\7-Zip\7z.exe')) {
if (Test-Path -LiteralPath $candidate) { return $candidate }
}
throw '7z.exe not found; install 7-Zip or pass -ZipTool'
}
function Read-JsonFile {
param([string]$Path)
return [IO.File]::ReadAllText($Path, [Text.Encoding]::UTF8) | ConvertFrom-Json
}
$archive = (Resolve-Path -LiteralPath $AppArchive).Path
if (-not (Test-Path -LiteralPath $archive)) { throw "application archive is missing: $AppArchive" }
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
$work = Join-Path ([IO.Path]::GetTempPath()) ("mqdh-portable-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Force -Path $work | Out-Null
try {
Write-Host "extracting verified application tree from $archive"
& tar.exe -xzf $archive -C $work
if ($LASTEXITCODE -ne 0) { throw "tar extraction failed: $LASTEXITCODE" }
$top = Get-ChildItem -LiteralPath $work -Directory | Select-Object -First 1
if (-not $top) { throw 'application archive has no top-level directory' }
$tree = $top.FullName
$packageJson = Read-JsonFile (Join-Path $tree 'packages\marqdo\harness\package.json')
$version = [string]$packageJson.version
if ($version -eq '') { throw 'application tree has no product version' }
$folder = "MQDH-$version-win-x64"
if ($SourceSha -ne '') {
if ($SourceSha -notmatch '^[0-9a-f]{40}$') { throw "invalid -SourceSha: $SourceSha" }
Set-Content -LiteralPath (Join-Path $tree 'SOURCE_SHA') -Value $SourceSha -Encoding ASCII
}
# A portable build is launched through MQDH.exe; the transitional install
# script must not be part of the formal distribution.
Remove-Item -LiteralPath (Join-Path $tree 'install.ps1') -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath (Join-Path $tree 'mqdh.cmd') -Force -ErrorAction SilentlyContinue
if ($LauncherExe -eq '') {
& (Join-Path $PSScriptRoot 'launcher\build-launcher.ps1') -OutDir (Join-Path $work 'launcher')
if ($LASTEXITCODE -ne 0) { throw "launcher build failed: $LASTEXITCODE" }
$LauncherExe = Join-Path $work 'launcher\MQDH.exe'
}
if (-not (Test-Path -LiteralPath $LauncherExe)) { throw "launcher is missing: $LauncherExe" }
Copy-Item -LiteralPath $LauncherExe -Destination (Join-Path $tree 'MQDH.exe') -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'launcher\README-Windows.txt') -Destination (Join-Path $tree 'README-Windows.txt') -Force
$staged = Join-Path $work $folder
Move-Item -LiteralPath $tree -Destination $staged
$zipName = "$folder.zip"
$zip = Join-Path $OutDir $zipName
Remove-Item -LiteralPath $zip -Force -ErrorAction SilentlyContinue
$sevenZip = Find-7z $ZipTool
Write-Host "creating $zipName"
& $sevenZip a -tzip -mx=5 $zip $folder
if ($LASTEXITCODE -ne 0) { throw "zip creation failed: $LASTEXITCODE" }
$zipHash = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash.ToLower()
$sidecar = "$zip.sha256"
[IO.File]::WriteAllText($sidecar, "$zipHash $zipName`n", (New-Object Text.UTF8Encoding($false)))
# PE subsystem 2 = Windows GUI: the launcher must never open a console window.
$launcherBytes = [IO.File]::ReadAllBytes((Join-Path $staged 'MQDH.exe'))
$peOffset = [BitConverter]::ToInt32($launcherBytes, 0x3C)
$subsystem = [BitConverter]::ToUInt16($launcherBytes, $peOffset + 24 + 68)
if ($subsystem -ne 2) { throw "launcher is not a Windows GUI executable (subsystem=$subsystem)" }
# Cold start: extract the ZIP, run MQDH.exe with an empty home, and confirm the
# workbench answers. This exercises the actual portable user path.
$verify = Join-Path $work 'verify'
New-Item -ItemType Directory -Force -Path $verify | Out-Null
& $sevenZip x $zip -o"$verify" -y | Out-Null
if ($LASTEXITCODE -ne 0) { throw "zip extraction failed: $LASTEXITCODE" }
$verifyTree = Join-Path $verify $folder
$home = Join-Path $verify 'home'
New-Item -ItemType Directory -Force -Path $home | Out-Null
$previousHome = $env:MQDH_HOME
$env:MQDH_HOME = $home
try {
$launcher = Start-Process -FilePath (Join-Path $verifyTree 'MQDH.exe') -PassThru
$urlFile = Join-Path $home 'workbench.url'
$deadline = (Get-Date).AddSeconds(180)
while (-not (Test-Path -LiteralPath $urlFile) -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 500 }
if (-not (Test-Path -LiteralPath $urlFile)) { throw 'portable launcher did not report a workbench URL' }
$url = ([IO.File]::ReadAllText($urlFile)).Trim()
$response = Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 30
if ($response.StatusCode -ne 200) { throw "workbench returned $($response.StatusCode)" }
$environment = Get-ChildItem -LiteralPath (Join-Path $home 'ecosystem\environments') -Directory -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $environment) { throw 'portable launcher did not install a runtime environment' }
$pidFile = Join-Path $home 'launcher.pid'
if (Test-Path -LiteralPath $pidFile) {
$serverPid = [int](([IO.File]::ReadAllText($pidFile)).Trim())
& taskkill.exe /PID $serverPid /T /F | Out-Null
}
if (-not $launcher.HasExited) { Stop-Process -Id $launcher.Id -Force -ErrorAction SilentlyContinue }
$evidence = [ordered]@{
schema = 'marqdo.mqdh.package-evidence'
schema_version = 1
status = 'PASS'
format = 'portable-zip'
archive = $zipName
size = (Get-Item -LiteralPath $zip).Length
sha256 = $zipHash
application_sha = if ($SourceSha -ne '') { $SourceSha } else { (([IO.File]::ReadAllText((Join-Path $staged 'SOURCE_SHA'))).Trim()) }
platform = 'x86_64-pc-windows-msvc'
launcher = [ordered]@{ name = 'MQDH.exe'; subsystem = 'windows-gui'; sha256 = (Get-FileHash -LiteralPath (Join-Path $staged 'MQDH.exe') -Algorithm SHA256).Hash.ToLower() }
cold_portable = [ordered]@{
extracted = $true
version = $version
workbench_http = $response.StatusCode
runtime_environment = $environment.Name
user_home = $home
launch_requires_terminal = $false
}
unverified = @('code signing', 'native Windows CI runner', 'EXE installer desktop integration', 'MQDH-branded shell (marqdo-app-harness#25)')
}
$evidence | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $OutDir 'package-evidence.json') -Encoding UTF8
Write-Host ("portable zip: {0} ({1:N0} bytes, sha256 {2})" -f $zipName, $evidence.size, $zipHash)
}
finally {
$env:MQDH_HOME = $previousHome
}
}
finally {
Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue
}