mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-03 04:26:26 -05:00
126 lines
6.6 KiB
PowerShell
126 lines
6.6 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
if ($env:RUNNER_ENVIRONMENT -ne 'github-hosted' -or $env:RUNNER_OS -ne 'Windows') {
|
|
throw 'This capture requires a disposable GitHub-hosted Windows runner.'
|
|
}
|
|
$output = New-Item -ItemType Directory captured-postscript
|
|
$kit = Get-Item -LiteralPath 'copier-kit/print_acceptance.exe'
|
|
$digest = (Get-FileHash -LiteralPath $kit.FullName -Algorithm SHA256).Hash
|
|
if ($digest -ne '3156EDDD8562650737BB9280DCAB3E3285692663620A215E318B948DA95D063A') {
|
|
throw 'The downloaded kit does not match the previously captured executable.'
|
|
}
|
|
Copy-Item -LiteralPath 'copier-kit/revision.txt' -Destination $output
|
|
|
|
Start-Service Spooler
|
|
$store = Join-Path $env:SystemRoot 'System32\DriverStore\FileRepository'
|
|
$packages = @(Get-WindowsDriver -Online -All | Where-Object {
|
|
$_.Inbox -and $_.ClassName -eq 'Printer' -and $_.ProviderName -eq 'Microsoft'
|
|
})
|
|
$models = @(
|
|
@{ Key = 'ps-class'; Driver = 'Microsoft PS Class Driver'; Inf = 'prnms005.inf' },
|
|
@{ Key = 'imagesetter'; Driver = 'MS Publisher Imagesetter'; Inf = 'prnge001.inf' }
|
|
)
|
|
|
|
function Capture-File($Port, $Queue, $Destination) {
|
|
$deadline = [DateTime]::UtcNow.AddSeconds(50)
|
|
while ([DateTime]::UtcNow -lt $deadline) {
|
|
if ((Test-Path -LiteralPath $Port -PathType Leaf) -and !(Get-PrintJob -PrinterName $Queue)) {
|
|
try {
|
|
# The local port must have closed its file before it can be opened exclusively.
|
|
$file = [IO.File]::Open($Port, [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::None)
|
|
try { $length = $file.Length } finally { $file.Dispose() }
|
|
if ($length -gt 0) {
|
|
Move-Item -LiteralPath $Port -Destination $Destination
|
|
return
|
|
}
|
|
} catch [IO.IOException] {
|
|
# The spooler can release the job record before the last handle closes.
|
|
}
|
|
}
|
|
Start-Sleep -Milliseconds 250
|
|
}
|
|
throw "No complete file arrived on the private port for $Queue"
|
|
}
|
|
|
|
$failed = @()
|
|
foreach ($model in $models) {
|
|
$folder = New-Item -ItemType Directory (Join-Path $output.FullName $model.Key)
|
|
$port = Join-Path $folder.FullName 'incoming.prn'
|
|
$queue = "Craft PS capture $($model.Key) $env:GITHUB_RUN_ID-$env:GITHUB_RUN_ATTEMPT"
|
|
$driverName = $model.Driver
|
|
$portAdded = $false
|
|
$queueAdded = $false
|
|
$driverAdded = $false
|
|
try {
|
|
$inbox = @($packages | Where-Object { [IO.Path]::GetFileName($_.OriginalFileName) -eq $model.Inf })
|
|
if ($inbox.Count -ne 1) { throw "Expected one inbox package for $($model.Inf), got $($inbox.Count)" }
|
|
$inf = $inbox[0].OriginalFileName
|
|
if (!$inf.StartsWith("$store\", [StringComparison]::OrdinalIgnoreCase) -or !(Test-Path -LiteralPath $inf -PathType Leaf)) {
|
|
throw 'The printer INF is not inside the local Driver Store.'
|
|
}
|
|
if (!(Select-String -LiteralPath $inf -Pattern $driverName -SimpleMatch -Quiet)) {
|
|
throw "OS package $($model.Inf) does not declare $driverName"
|
|
}
|
|
$signature = @(Get-ChildItem -LiteralPath (Split-Path -Parent $inf) -Filter '*.cat' | ForEach-Object {
|
|
$auth = Get-AuthenticodeSignature -LiteralPath $_.FullName
|
|
[pscustomobject]@{ Catalog = $_.Name; Status = $auth.Status.ToString(); Signer = $auth.SignerCertificate.Subject }
|
|
})
|
|
$signature | ConvertTo-Json -AsArray | Set-Content -LiteralPath (Join-Path $folder.FullName 'catalog-signatures.json')
|
|
if (!($signature | Where-Object { $_.Status -eq 'Valid' -and $_.Signer -like 'CN=Microsoft Windows,*' })) {
|
|
throw "No valid Microsoft Windows catalog signature for $($model.Inf)"
|
|
}
|
|
# Only the OS inbox package above is eligible. Install it through the
|
|
# standard spooler package path and leave signature enforcement enabled.
|
|
if (!(Get-PrinterDriver -Name $driverName -ErrorAction SilentlyContinue)) {
|
|
Add-PrinterDriver -Name $driverName -InfPath $inf
|
|
$driverAdded = $true
|
|
}
|
|
$driver = Get-PrinterDriver -Name $driverName
|
|
[pscustomobject]@{
|
|
Model = $driver.Name
|
|
Manufacturer = $driver.Manufacturer
|
|
Version = $driver.MajorVersion
|
|
Package = $model.Inf
|
|
Inbox = $inbox[0].Inbox
|
|
DriverFile = [IO.Path]::GetFileName($driver.DriverPath)
|
|
DataFile = [IO.Path]::GetFileName($driver.DataFile)
|
|
} | ConvertTo-Json | Tee-Object -FilePath (Join-Path $folder.FullName 'driver.json')
|
|
if (Test-Path -LiteralPath $port) { throw 'The private port already has an output file.' }
|
|
Add-PrinterPort -Name $port
|
|
$portAdded = $true
|
|
Add-Printer -Name $queue -DriverName $driverName -PortName $port
|
|
$queueAdded = $true
|
|
$created = Get-Printer -Name $queue
|
|
if ($created.PortName -ne $port -or $created.Shared -or $created.DriverName -ne $driverName) {
|
|
throw 'The queue is not bound exclusively to the file port and expected driver.'
|
|
}
|
|
Set-PrintConfiguration -PrinterName $queue -PaperSize Letter
|
|
Get-PrintConfiguration -PrinterName $queue | Select-Object Color, PaperSize |
|
|
ConvertTo-Json | Tee-Object -FilePath (Join-Path $folder.FullName 'configuration.json')
|
|
foreach ($case in 'baseline', 'range') {
|
|
if ((Test-Path -LiteralPath $port) -or (Get-PrintJob -PrinterName $queue)) { throw 'The private queue is not empty.' }
|
|
& $kit.FullName submit $case $queue | Tee-Object -FilePath (Join-Path $folder.FullName "$case-receipt.log")
|
|
if ($LASTEXITCODE) { throw "Submission failed: $case" }
|
|
Capture-File $port $queue (Join-Path $folder.FullName "$case.prn")
|
|
}
|
|
} catch {
|
|
$failed += "$($model.Key): $_"
|
|
$_ | Out-String | Set-Content -LiteralPath (Join-Path $folder.FullName 'failure.txt')
|
|
} finally {
|
|
try {
|
|
if ($queueAdded) { Remove-Printer -Name $queue }
|
|
if ($portAdded) {
|
|
# The name overload parses backslashes as a server qualifier.
|
|
$localPort = Get-PrinterPort | Where-Object Name -eq $port
|
|
if ($localPort) { Remove-PrinterPort -InputObject $localPort }
|
|
}
|
|
if ($driverAdded) { Remove-PrinterDriver -Name $driverName }
|
|
} catch {
|
|
# The disposable runner is deprovisioned. Record any teardown
|
|
# failure and keep the independent captures available.
|
|
Write-Warning "Private queue teardown failed: $_"
|
|
$_ | Out-String | Set-Content -LiteralPath (Join-Path $folder.FullName 'cleanup.txt')
|
|
}
|
|
}
|
|
}
|
|
if ($failed.Count) { throw ($failed -join [Environment]::NewLine) }
|