Files
dxdesigner-part-replacement/packaging/Build-DxPartReplace.ps1
T

137 lines
6.6 KiB
PowerShell

[CmdletBinding()]
param(
[string]$PythonExecutable
)
$ErrorActionPreference = "Stop"
function Remove-VerifiedBuildTarget {
param(
[Parameter(Mandatory = $true)]
[string]$RepositoryRoot,
[Parameter(Mandatory = $true)]
[string]$RelativeTarget
)
$expected = [System.IO.Path]::GetFullPath((Join-Path $RepositoryRoot $RelativeTarget))
$expectedRoot = [System.IO.Path]::GetFullPath($RepositoryRoot).TrimEnd('\', '/')
if (-not $expected.StartsWith($expectedRoot + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to remove a target outside the repository: $expected"
}
if (-not (Test-Path -LiteralPath $expected)) {
return
}
$resolved = (Resolve-Path -LiteralPath $expected -ErrorAction Stop).Path
if (-not [string]::Equals($resolved, $expected, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to remove a redirected target: $resolved"
}
Remove-Item -LiteralPath $resolved -Recurse -Force
}
$repositoryRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot "..")).Path
$sourceRoot = (Resolve-Path -LiteralPath (Join-Path $repositoryRoot "src")).Path
$projectPython = Join-Path $repositoryRoot ".venv\Scripts\python.exe"
if (-not $PythonExecutable) {
if (Test-Path -LiteralPath $projectPython -PathType Leaf) {
$PythonExecutable = $projectPython
}
elseif ($env:DX_PART_REPLACE_PYTHON) {
$PythonExecutable = $env:DX_PART_REPLACE_PYTHON
}
else {
throw "Create .venv or pass -PythonExecutable with a Python 3.12 interpreter"
}
}
$PythonExecutable = (Resolve-Path -LiteralPath $PythonExecutable -ErrorAction Stop).Path
$env:PYTHONPATH = $sourceRoot
$env:QT_QPA_PLATFORM = "offscreen"
$smokeRoot = $null
$distRoot = [System.IO.Path]::GetFullPath((Join-Path $repositoryRoot "dist\DxPartReplace"))
$distData = Join-Path $distRoot "data"
$preservedData = [System.IO.Path]::GetFullPath((Join-Path $repositoryRoot "build\DxPartReplace-preserved-data"))
$dataWasPreserved = $false
$locationPushed = $false
try {
if (Test-Path -LiteralPath $distData) {
$resolvedData = (Resolve-Path -LiteralPath $distData -ErrorAction Stop).Path
if (-not [string]::Equals($resolvedData, $distData, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Refusing to preserve redirected application data: $resolvedData"
}
if (Test-Path -LiteralPath $preservedData) {
throw "Preserved application data target already exists: $preservedData"
}
New-Item -ItemType Directory -Path (Split-Path -Parent $preservedData) -Force | Out-Null
Move-Item -LiteralPath $distData -Destination $preservedData -ErrorAction Stop
$dataWasPreserved = $true
}
Push-Location $repositoryRoot
$locationPushed = $true
& $PythonExecutable -m pytest -q tests\v1_1 tests\trial_task_center\test_application.py tests\trial_task_center\test_execution_safety.py tests\part_replace\test_verification.py
if ($LASTEXITCODE -ne 0) { throw "Release verification failed; build stopped" }
Remove-VerifiedBuildTarget -RepositoryRoot $repositoryRoot -RelativeTarget "build\DxPartReplace"
Remove-VerifiedBuildTarget -RepositoryRoot $repositoryRoot -RelativeTarget "dist\DxPartReplace"
& $PythonExecutable -m PyInstaller --noconfirm packaging\DxPartReplace.spec
if ($LASTEXITCODE -ne 0) { throw "PyInstaller failed; build stopped" }
$executable = [System.IO.Path]::GetFullPath((Join-Path $repositoryRoot "dist\DxPartReplace\DxPartReplace.exe"))
if (-not (Test-Path -LiteralPath $executable -PathType Leaf)) {
throw "PyInstaller did not produce $executable"
}
$sourceQuickPartReplace = Join-Path $repositoryRoot "integration\dxdesigner795\QuickPartReplace.vbs"
$packagedQuickPartReplace = Join-Path $repositoryRoot "dist\DxPartReplace\_internal\legacy-runtime\dxdesigner795\QuickPartReplace.vbs"
if (-not (Test-Path -LiteralPath $sourceQuickPartReplace -PathType Leaf)) {
throw "QuickPartReplace.vbs source is missing: $sourceQuickPartReplace"
}
if (-not (Test-Path -LiteralPath $packagedQuickPartReplace -PathType Leaf)) {
throw "PyInstaller did not package QuickPartReplace.vbs: $packagedQuickPartReplace"
}
$sourceQuickHash = (Get-FileHash -LiteralPath $sourceQuickPartReplace -Algorithm SHA256).Hash
$packagedQuickHash = (Get-FileHash -LiteralPath $packagedQuickPartReplace -Algorithm SHA256).Hash
if (-not [string]::Equals($sourceQuickHash, $packagedQuickHash, [System.StringComparison]::OrdinalIgnoreCase)) {
throw "packaged QuickPartReplace.vbs SHA256 differs from the source"
}
$smokeRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("DxPartReplace-smoke-" + [Guid]::NewGuid().ToString("N"))
try {
New-Item -ItemType Directory -Path $smokeRoot -ErrorAction Stop | Out-Null
$quotedSmokeRoot = '"' + $smokeRoot + '"'
$smokeProcess = Start-Process -FilePath $executable -ArgumentList @("--smoke-test", "--data-root", $quotedSmokeRoot) -Wait -PassThru -WindowStyle Hidden
if ($smokeProcess.ExitCode -ne 0) {
throw "DxPartReplace smoke test failed with exit code $($smokeProcess.ExitCode)"
}
$smokeLog = Join-Path $smokeRoot "logs\application.log"
if (-not (Test-Path -LiteralPath $smokeLog -PathType Leaf)) {
throw "DxPartReplace smoke test did not create $smokeLog"
}
$smokeLogText = Get-Content -LiteralPath $smokeLog -Raw -ErrorAction Stop
if ($smokeLogText -notmatch "application_start") {
throw "DxPartReplace smoke test did not log application_start"
}
if ($smokeLogText -notmatch "application_clean_shutdown") {
throw "DxPartReplace smoke test did not log application_clean_shutdown"
}
}
finally {
if ($null -ne $smokeRoot -and (Test-Path -LiteralPath $smokeRoot)) {
Remove-Item -LiteralPath $smokeRoot -Recurse -Force
}
}
$logRoot = if ($env:DX_TASK_CENTER_HOME) { $env:DX_TASK_CENTER_HOME } else { Join-Path (Split-Path -Parent $executable) "data" }
Write-Host "EXE: $executable"
Write-Host "Logs: $([System.IO.Path]::GetFullPath((Join-Path $logRoot 'logs\application.log')))"
}
finally {
if ($locationPushed) {
Pop-Location
}
if ($dataWasPreserved) {
New-Item -ItemType Directory -Path $distRoot -Force | Out-Null
if (Test-Path -LiteralPath $distData) {
throw "New build unexpectedly created an application data directory: $distData"
}
Move-Item -LiteralPath $preservedData -Destination $distData -ErrorAction Stop
}
}