Files
marqdo-app-harness/scripts/package-windows-zip.ps1
T
cflmy 217e37ec6f
ci/woodpecker/push/ci Pipeline is pending
ci/woodpecker/pr/ci Pipeline was successful
feat: ship the latest MQDH frontend in the portable ZIP
The launcher opens the branded /mqdh workbench; the portable packaging script accepts a prebuilt application tree so the ZIP carries the latest frontend build without rebuilding the bundle. Adds the packaging tree mode and the updated launcher.
2026-12-03 11:04:13 +08:00

168 lines
8.4 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(
[string]$AppArchive = '',
[string]$AppTree = '',
[string]$OutDir = (Join-Path $PSScriptRoot '..\.ci'),
[string]$SourceSha = '',
[string]$LauncherExe = ''
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
function Read-JsonFile {
param([string]$Path)
return [IO.File]::ReadAllText($Path, [Text.Encoding]::UTF8) | ConvertFrom-Json
}
if ($AppArchive -eq '' -and $AppTree -eq '') { throw 'pass -AppArchive or -AppTree' }
if ($AppTree -ne '' -and -not (Test-Path -LiteralPath $AppTree)) { throw "application tree is missing: $AppTree" }
$archive = ''
if ($AppArchive -ne '') {
$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 {
if ($AppTree -ne '') {
$tree = (Resolve-Path -LiteralPath $AppTree).Path
} else {
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
if ($AppTree -ne '') {
$staged = $tree
} else {
$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
Write-Host "creating $zipName"
Add-Type -AssemblyName System.IO.Compression
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zipStream = [IO.File]::Create($zip)
try {
$archive = New-Object System.IO.Compression.ZipArchive($zipStream, [System.IO.Compression.ZipArchiveMode]::Create, $false)
try {
foreach ($file in Get-ChildItem -LiteralPath $staged -Recurse -File -Force) {
$relative = $file.FullName.Substring($staged.Length + 1).Replace('\', '/')
$entry = $archive.CreateEntry("$folder/$relative", [System.IO.Compression.CompressionLevel]::Fastest)
$entryStream = $entry.Open()
$input = [IO.File]::OpenRead($file.FullName)
try { $input.CopyTo($entryStream) } finally { $input.Dispose(); $entryStream.Dispose() }
}
} finally { $archive.Dispose() }
} finally { $zipStream.Dispose() }
$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
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $verify)
$verifyTree = Join-Path $verify $folder
$userHome = Join-Path $verify 'home'
New-Item -ItemType Directory -Force -Path $userHome | Out-Null
$previousHome = $env:MQDH_HOME
$env:MQDH_HOME = $userHome
try {
$launcher = Start-Process -FilePath (Join-Path $verifyTree 'MQDH.exe') -PassThru
$urlFile = Join-Path $userHome '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 $userHome 'ecosystem\environments') -Directory -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $environment) { throw 'portable launcher did not install a runtime environment' }
$pidFile = Join-Path $userHome '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 = $userHome
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
}