printServer/electron/build-offline-installer.ps1
2026-03-29 13:26:23 +08:00

128 lines
3.5 KiB
PowerShell

param(
[string]$ProjectRoot = (Split-Path -Parent $PSScriptRoot)
)
$ErrorActionPreference = 'Stop'
function Ensure-Dir([string]$PathValue) {
if (-not (Test-Path $PathValue)) {
New-Item -ItemType Directory -Path $PathValue -Force | Out-Null
}
}
function Get-ShortPath([string]$PathValue) {
$item = Get-Item -Path $PathValue -ErrorAction Stop
$shortPath = cmd.exe /d /c ('for %I in ("{0}") do @echo %~sI' -f $item.FullName)
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($shortPath)) {
return $item.FullName
}
return $shortPath.Trim()
}
$packageJsonPath = Join-Path $ProjectRoot 'package.json'
$packageJson = Get-Content -Path $packageJsonPath -Raw | ConvertFrom-Json
$version = $packageJson.version
$distDir = Join-Path $ProjectRoot 'dist'
$appOutDir = Join-Path $distDir 'win-unpacked'
$primaryExePath = Join-Path $appOutDir 'printServer.exe'
$fallbackExePath = Join-Path $appOutDir 'electron.exe'
$targetName = "printServer-$version-setup.exe"
$targetPath = Join-Path $distDir $targetName
$tempRootName = [string]([char]0x4E34) + [char]0x65F6
$stagingRoot = Join-Path (Join-Path $ProjectRoot $tempRootName) 'packaging\iexpress'
$payloadZipPath = Join-Path $stagingRoot 'printServer-app.zip'
$sedPath = Join-Path $stagingRoot 'offline-installer.sed'
$installScriptSource = Join-Path $ProjectRoot 'electron\offline-install.ps1'
$installCmdSource = Join-Path $ProjectRoot 'electron\offline-install.cmd'
$installScriptTarget = Join-Path $stagingRoot 'install.ps1'
$installCmdTarget = Join-Path $stagingRoot 'install.cmd'
if (-not (Test-Path $appOutDir)) {
throw 'win-unpacked directory not found, run the app stage first'
}
if (-not (Test-Path $primaryExePath)) {
if (-not (Test-Path $fallbackExePath)) {
throw 'win-unpacked executable not found'
}
Copy-Item -Path $fallbackExePath -Destination $primaryExePath -Force
}
if (Test-Path $stagingRoot) {
Remove-Item -Path $stagingRoot -Recurse -Force
}
Ensure-Dir $stagingRoot
Copy-Item -Path $installScriptSource -Destination $installScriptTarget -Force
Copy-Item -Path $installCmdSource -Destination $installCmdTarget -Force
Compress-Archive -Path (Join-Path $appOutDir '*') -DestinationPath $payloadZipPath -Force
$sourceFilesPath = Get-ShortPath $stagingRoot
$sedContent = @"
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=1
HideExtractAnimation=1
UseLongFileName=1
InsideCompressed=1
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[Strings]
InstallPrompt=
DisplayLicense=
FinishMessage=printServer installation completed.
TargetName=$targetPath
FriendlyName=printServer Installer
AppLaunched=install.cmd
PostInstallCmd=<None>
AdminQuietInstCmd=install.cmd /quiet
UserQuietInstCmd=install.cmd /quiet
FILE0=install.cmd
FILE1=install.ps1
FILE2=printServer-app.zip
[SourceFiles]
SourceFiles0=$sourceFilesPath
[SourceFiles0]
%FILE0%=
%FILE1%=
%FILE2%=
"@
Set-Content -Path $sedPath -Value $sedContent -Encoding ASCII
& iexpress.exe /N /Q $sedPath
if ($LASTEXITCODE -ne 0) {
throw "IExpress failed with exit code $LASTEXITCODE"
}
if (-not (Test-Path $targetPath)) {
throw 'IExpress completed without producing the setup executable'
}
Write-Host "Offline installer created: $targetPath"