scripts\build-release.ps1¶
源码路径:
scripts\build-release.ps1· 106 行
# 出可执行文件:release 编译 + 按目录约定铺出 dist\
#
# 用法:
# .\scripts\build-release.ps1
# .\scripts\build-release.ps1 -Toolchain stable-x86_64-pc-windows-gnu
# .\scripts\build-release.ps1 -OutDir C:\release\nfarms
#
# 产物布局(**已改**:不再有 bin\ 和 src\,exe 与设置文件平铺在程序根):
#
# dist\
# ├─ nfarms.exe
# ├─ libs\ webview2 / ffmpeg / bililive-go / servo / sqlite
# ├─ logs\ nfarms-hub.log / nfos-printer.log / *.cy
# ├─ settings.json / store.json
# ├─ logs\bak\ 备份
# └─ libs\live\ 直播录制输出
[CmdletBinding()]
param(
[string]$Toolchain = '',
[string]$OutDir = 'dist'
)
$ErrorActionPreference = 'Stop'
$ws = Split-Path -Parent $PSScriptRoot
. (Join-Path $ws 'dev.ps1')
Push-Location $ws
# PowerShell 5.1 下 native 命令写 stderr 会被当成错误,配上 Stop 就成了终止性异常。
$prevEap = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
if ($Toolchain) { & cargo "+$Toolchain" build --release --offline 2>&1 | Write-Host }
else { & cargo build --release --offline 2>&1 | Write-Host }
$buildCode = $LASTEXITCODE
}
finally {
$ErrorActionPreference = $prevEap
Pop-Location
}
if ($buildCode -ne 0) { throw "cargo build 失败 (exit $buildCode)" }
$exeSrc = Join-Path $ws 'target\release\nfarms.exe'
if (-not (Test-Path $exeSrc)) { throw "没有产物:$exeSrc" }
$root = Join-Path $ws $OutDir
# **布局迁移**:老版本会在产物根下留 `bin\` 和 `src\`。
# 不清掉的话,从旧版升级上来的人会看到两个空壳目录,还会以为 exe 该在 `bin\` 里 ——
# 这正是这次布局改动最容易留下的误解。删不掉也不报错(可能被占用)。
foreach ($stale in 'bin', 'src') {
$p = Join-Path $root $stale
if (Test-Path $p) { Remove-Item $p -Recurse -Force -ErrorAction SilentlyContinue }
}
foreach ($d in 'libs', 'logs', 'wiki', 'logs\bak', 'libs\live') {
New-Item -ItemType Directory -Force -Path (Join-Path $root $d) | Out-Null
}
$exe = Join-Path $root 'nfarms.exe'
Copy-Item $exeSrc $exe -Force
# libs\scripts\ 一起铺过去:界面 Harness 页的「下载缺失组件 / 下载缺失模型」
# 会去调 <程序根>\libs\scripts\fetch-libs.ps1(复用已经跑通的断点续传脚本,
# 而不是在 Rust 里再写一遍)。
$dstScripts = Join-Path $root 'libs\scripts'
New-Item -ItemType Directory -Force -Path $dstScripts | Out-Null
Copy-Item (Join-Path $ws 'scripts\*.ps1') $dstScripts -Force
# libs\pet\ 是**桌宠的贴图**,只有一张 png。
#
# 必须一起铺:少了它,`PetWindow::spawn` 加载失败**不会 panic**,只是没有桌宠 ——
# 界面上没有任何提示,用户只会觉得"桌宠怎么没了"。
# (这个洞的实际表现:开发机上一切正常,换台机器/重装后桌宠静默消失。)
$dstPet = Join-Path $root 'libs\pet'
New-Item -ItemType Directory -Force -Path $dstPet | Out-Null
$petSrc = Join-Path $ws 'libs\pet'
if (Test-Path $petSrc) {
Copy-Item (Join-Path $petSrc '*') $dstPet -Force
Write-Host (" libs\pet\ {0} 个文件" -f (Get-ChildItem $petSrc -File).Count) -ForegroundColor DarkGray
}
else {
# **出声**,不要静默跳过 —— 静默正是这个洞最初没被发现的原因
Write-Host ' [!] 源码里没有 libs\pet\ —— 桌宠不会有贴图' -ForegroundColor Yellow
}
# logo 也放程序根 —— 和 C# 版约定一致:exe 里嵌了一份兜底,
# 根目录这份是可以让用户随时替换的。
$iconSrc = Join-Path $ws 'assets\nfarms.ico'
if (Test-Path $iconSrc) {
Copy-Item $iconSrc (Join-Path $root 'nfarms.ico') -Force
} else {
Write-Host '[!] assets\nfarms.ico 不存在 —— 窗口图标会退回 exe 里内嵌的那份' -ForegroundColor Yellow
}
Write-Host ''
Write-Host ("[OK] {0} {1} KB" -f $exe, [math]::Round((Get-Item $exe).Length / 1KB, 1)) -ForegroundColor Green
Write-Host ''
Write-Host '自检:dist\nfarms.exe --layout-test'
# 本程序是 Windows(GUI)子系统 —— PowerShell 对 GUI 程序 `&` 调用既不等待、
# 也拿不到输出(原版 README 里专门记过这条)。必须用 cmd 重定向到文件再读,
# 顺便把「程序根判定」验一遍 —— exe 现在就在根目录,所以根应当**等于 exe 目录**,
# 不再上溯。这一条自检看到 `程序根` 和 `exe 目录` 是同一个路径才算对。
$probe = Join-Path $env:TEMP ("nfarms-layout-" + [guid]::NewGuid().ToString('N').Substring(0, 6) + ".txt")
cmd /c ('"{0}" --layout-test > "{1}" 2>&1' -f $exe, $probe)
Get-Content $probe -Encoding UTF8 | Write-Host
Remove-Item $probe -Force -ErrorAction SilentlyContinue