<# .SYNOPSIS Bookworm Portable — 打包工具 (管理员使用) .DESCRIPTION 将 auto-setup.ps1 打包为 Bookworm-Setup.exe (PS2EXE) 将 gen-authcode.js 打包为 gen-authcode.exe (pkg) 输出到 dist\ 目录 .USAGE .\build.ps1 # 打包两个 .\build.ps1 -Setup # 只打包用户安装器 .\build.ps1 -Admin # 只打包管理员工具 #> param( [switch]$Setup, # 只打 Bookworm-Setup.exe [switch]$Admin # 只打 gen-authcode.exe ) $ErrorActionPreference = "Stop" $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $DistDir = Join-Path $ScriptDir "dist" # 默认两个都打 $buildSetup = $Setup -or (-not $Setup -and -not $Admin) $buildAdmin = $Admin -or (-not $Setup -and -not $Admin) function Write-Step($msg) { Write-Host "" Write-Host " ── $msg" -ForegroundColor Cyan } function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } function Write-Warn($msg) { Write-Host " [!] $msg" -ForegroundColor Yellow } function Write-Fail($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } Write-Host "" Write-Host " +------------------------------------------+" -ForegroundColor Cyan Write-Host " | Bookworm Portable — Build Script |" -ForegroundColor Cyan Write-Host " +------------------------------------------+" -ForegroundColor Cyan if (-not (Test-Path $DistDir)) { New-Item -ItemType Directory $DistDir | Out-Null Write-OK "创建 dist\ 目录" } # ════════════════════════════════════════════════════════ # 1. Bookworm-Setup.exe (auto-setup.ps1 → PS2EXE) # ════════════════════════════════════════════════════════ if ($buildSetup) { Write-Step "打包 Bookworm-Setup.exe (PS2EXE)" # 安装/检查 PS2EXE if (-not (Get-Command Invoke-ps2exe -ErrorAction SilentlyContinue)) { Write-Warn "PS2EXE 未安装,正在安装..." Install-Module ps2exe -Scope CurrentUser -Force -AllowClobber Import-Module ps2exe } $inputPs1 = Join-Path $ScriptDir "auto-setup.ps1" $outputExe = Join-Path $DistDir "Bookworm-Setup.exe" if (-not (Test-Path $inputPs1)) { Write-Fail "找不到 auto-setup.ps1" exit 1 } Write-Host " 输入: $inputPs1" -ForegroundColor Gray Write-Host " 输出: $outputExe" -ForegroundColor Gray $iconFile = Join-Path $ScriptDir "bookworm.ico" $ps2exeArgs = @{ InputFile = $inputPs1 OutputFile = $outputExe Title = "Bookworm Portable Setup" Description = "Bookworm Smart Assistant 安装向导" Company = "Bookworm" Version = "1.5.1.0" NoConsole = $true NoOutput = $true # 抑制所有 Write-Host 弹窗化, 只保留 Show-MsgBox / GUI dialog NoError = $true # 抑制 stderr 弹窗化, 异常仍走 try-catch } if (Test-Path $iconFile) { $ps2exeArgs.IconFile = $iconFile Write-Host " 图标: $iconFile" -ForegroundColor Gray } Invoke-ps2exe @ps2exeArgs if (Test-Path $outputExe) { $sizeKB = [math]::Round((Get-Item $outputExe).Length / 1KB) Write-OK "Bookworm-Setup.exe 打包完成 (${sizeKB} KB)" } else { Write-Fail "Bookworm-Setup.exe 打包失败" exit 1 } } # ════════════════════════════════════════════════════════ # 2. gen-authcode.exe (gen-authcode.js → pkg) # ════════════════════════════════════════════════════════ if ($buildAdmin) { Write-Step "打包 gen-authcode.exe (pkg)" # 检查 Node.js if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Fail "需要 Node.js,请先安装: https://nodejs.org" exit 1 } # 安装/检查 pkg # 优先找 npm global bin 目录下的 pkg(不依赖 PATH 是否刷新) $npmGlobalBin = Join-Path (npm root -g 2>$null) "..\pkg.cmd" | Resolve-Path -ErrorAction SilentlyContinue $pkgCmd = if ($npmGlobalBin) { $npmGlobalBin.Path } else { (Get-Command pkg -ErrorAction SilentlyContinue)?.Source } if (-not $pkgCmd) { Write-Warn "pkg 未安装,正在安装..." npm install -g pkg $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") $npmGlobalBin = Join-Path (npm root -g 2>$null) "..\pkg.cmd" | Resolve-Path -ErrorAction SilentlyContinue $pkgCmd = $npmGlobalBin.Path } $inputJs = Join-Path $ScriptDir "gen-authcode.js" $outputExe = Join-Path $DistDir "gen-authcode.exe" if (-not (Test-Path $inputJs)) { Write-Fail "找不到 gen-authcode.js" exit 1 } Write-Host " 输入: $inputJs" -ForegroundColor Gray Write-Host " 输出: $outputExe" -ForegroundColor Gray Write-Host " (内嵌 Node.js 运行时,首次打包约 1 分钟)" -ForegroundColor Gray & $pkgCmd $inputJs --targets node18-win-x64 --output $outputExe 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } if (Test-Path $outputExe) { $sizeMB = [math]::Round((Get-Item $outputExe).Length / 1MB, 1) Write-OK "gen-authcode.exe 打包完成 (${sizeMB} MB)" # 注入图标 (rcedit) $iconFile = Join-Path $ScriptDir "bookworm.ico" $rcedit = "$env:APPDATA\npm\node_modules\rcedit\bin\rcedit-x64.exe" if ((Test-Path $iconFile) -and (Test-Path $rcedit)) { & $rcedit $outputExe --set-icon $iconFile | Out-Null Write-OK "gen-authcode.exe 图标注入完成" } } else { Write-Fail "gen-authcode.exe 打包失败" exit 1 } } # ════════════════════════════════════════════════════════ # 完成 # ════════════════════════════════════════════════════════ Write-Host "" Write-Host " ============================================" -ForegroundColor Green Write-Host " 打包完成!输出目录: dist\" -ForegroundColor Green Write-Host " ============================================" -ForegroundColor Green Write-Host "" Get-ChildItem $DistDir | ForEach-Object { $sizeMB = [math]::Round($_.Length / 1MB, 1) Write-Host " $($_.Name.PadRight(30)) ${sizeMB} MB" -ForegroundColor White } Write-Host "" Write-Host " 分发说明:" -ForegroundColor Gray Write-Host " Bookworm-Setup.exe → 放入 bookworm-boot 仓库根目录,发给用户" -ForegroundColor Gray Write-Host " gen-authcode.exe → 管理员本机使用,勿对外分发" -ForegroundColor Gray Write-Host ""