<# .SYNOPSIS 生成 bin/manifest.json —— Wails 内置升级器的更新清单(静态站点方案)。 .DESCRIPTION 先用 `wails3 build -tags production` 编译应用,把编译产物 bin\腾图工具箱.exe 复制为 bin\TengTuToolbox-windows-amd64.exe(升级器期望的发布包名), 再读取 bin 下的发布包,计算文件大小与版本,写入升级清单 manifest.json。 将发布包与清单一起上传到更新站点 https://i.scwy.net/down/toolsbox/。 清单必须命名为 manifest.json;制品 url 即文件名(同一目录)。 .PARAMETER Exe 用于生成清单的发布包(绝对或相对路径)。 默认 bin\TengTuToolbox-windows-amd64.exe,回退到 bin\TengTuToolbox.exe。 .PARAMETER Version 清单版本。默认从 settings.go 的 AppVersion 读取,否则取 exe 版本信息。 .PARAMETER Notes 更新说明(多行,用反引号 n 换行)。默认为空。 .PARAMETER PublishedAt 发布时间(ISO 8601 或可解析格式)。默认为当前时间。 .PARAMETER OutFile 输出清单路径。默认 bin\manifest.json。 .PARAMETER Up 若设置,生成清单后用 scp 把发布包与清单上传到更新服务器,远端目录用 -UpTarget。 .PARAMETER UpTarget scp 远端目标目录。默认: ease@192.168.100.22:/home/ease/server/blog/down/toolsbox .PARAMETER Build 若设置,先用 `wails3 build -tags production` 编译应用,再把编译产物 bin\腾图工具箱.exe 复制为发布包名。不设置时直接使用 bin 下已有的发布包。 .EXAMPLE .\update.ps1 -Build .EXAMPLE .\update.ps1 -Build -Version 0.2.0 -Notes "`n- 新增功能 A.`n- 修复 B." .EXAMPLE .\update.ps1 -Build -Up #> [CmdletBinding()] param( [string]$Exe = "", [string]$Version = "", [string]$Notes = "", [string]$PublishedAt = "", [string]$OutFile = "", [switch]$Up, [string]$UpTarget = "ease@192.168.100.22:/home/ease/server/blog/down/toolsbox", [switch]$Build ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $MyInvocation.MyCommand.Path $binDir = Join-Path $root 'bin' # 第 0 步:可选编译 —— 仅当 -Build 时用 `wails3 build -tags production` 编译应用 # (production 标签:注册右键菜单「标签」注册表项),再把编译产物复制为升级器 # 期望的发布包名。不设置 -Build 时直接用 bin 下已有的发布包。 if ($Build) { Write-Host '==> 正在用 wails3 build -tags production 编译 ...' Push-Location $root try { wails3 build -tags production if ($LASTEXITCODE -ne 0) { throw "wails3 build 失败,退出码 $LASTEXITCODE" } } finally { Pop-Location } # 定位刚编译出的可执行文件。不要在脚本里硬编码中文产品名「腾图工具箱.exe」: # PowerShell 5.1 按系统 ANSI 代码页读取 .ps1,无 BOM 的 UTF-8 中文字面量会变乱码, # 导致 Test-Path 失败。改为取 bin 下最新写入、且非发布/安装/服务版制品的 .exe, # 编译总是把应用 GUI exe 写得最新。 # # 注意:必须排除 service 版(TengTuToolbox-service.exe / 腾图工具箱-service.exe)! # 否则「最新写入 exe」可能选中 service 版,导致 manifest 的 size/摘要/签名全 # 按 service 版生成、并把错误二进制当 GUI 发布包上传 —— 即此前升级失败的根因。 $excluded = @( 'TengTuToolbox-windows-amd64.exe' 'TengTu Toolbox-amd64-installer.exe' 'TengTuToolbox.exe' 'TengTuToolbox-service.exe' '腾图工具箱-service.exe' ) $builtExe = Get-ChildItem -Path $binDir -Filter '*.exe' | Where-Object { $excluded -notcontains $_.Name } | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if (-not $builtExe) { throw 'bin 下未找到 GUI 编译产物(service 版已排除),请检查 wails3 build 输出。' } $builtExe = $builtExe.FullName $releaseExe = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe' Copy-Item -Path $builtExe -Destination $releaseExe -Force Write-Host "==> 已复制 GUI 编译产物到 $releaseExe" } # 第 1 步:解析发布包 if (-not $Exe) { # 优先英文发布包名;否则回退中文 GUI 版「腾图工具箱.exe」。 # 两个候选都必须是 GUI 版 —— 绝不能回退到 service 版(否则清单会按 service 版 # 生成摘要/签名并上传错误二进制,导致客户端升级校验失败)。 $candidate = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe' if (-not (Test-Path $candidate)) { $candidate = Join-Path $binDir 'TengTuToolbox.exe' } if (-not (Test-Path $candidate)) { $candidate = Join-Path $binDir '腾图工具箱.exe' } if (-not (Test-Path $candidate)) { throw '未找到 GUI 发布包,请先运行 -Build 或用 wails3 build 编译,或传入 -Exe。' } $Exe = $candidate } $exePath = if ([System.IO.Path]::IsPathRooted($Exe)) { $Exe } else { Join-Path (Get-Location) $Exe } if (-not (Test-Path $exePath)) { throw "未找到发布包:$exePath" } # 第 2 步:文件名(制品 url = 文件名,与清单一起上传) $fileName = [System.IO.Path]::GetFileName($exePath) $fileSize = (Get-Item $exePath).Length # 第 3 步:版本 —— 优先 -Version,其次 settings.go 的 AppVersion,最后 exe 版本信息 if (-not $Version) { $settingsPath = Join-Path $root 'settings.go' if (Test-Path $settingsPath) { $m = Select-String -Path $settingsPath -Pattern 'AppVersion\s*=\s*"([^"]+)"' if ($m) { $Version = $m.Matches[0].Groups[1].Value } } } if (-not $Version) { $ver = (Get-Item $exePath).VersionInfo $Version = if ($ver.ProductVersion) { $ver.ProductVersion } else { $ver.FileVersion } } if (-not $Version) { $Version = '0.0.0' } # 第 4 步:发布时间 if (-not $PublishedAt) { $PublishedAt = (Get-Date).ToString('yyyy-MM-ddTHH:mm:sszzz') } else { try { $PublishedAt = ([datetime]$PublishedAt).ToString('yyyy-MM-ddTHH:mm:sszzz') } catch { } } # 第 5 步:规范化更新说明里的换行 —— 把用户传入的字面 \n(反斜杠+n)替换为真实换行。 # PowerShell 的原生换行转义是反引号 n(`n),但命令行里常见反斜杠写法 \n。 # 若用户已用 `n 得到真实换行,则不受影响;仅替换字面的反斜杠+n。 $Notes = $Notes -replace '\\n', "`n" # 第 6 步:计算摘要与 Ed25519 签名 —— 升级器配置了 updater.key.pub 公钥后, # 要求每个 release 必须带签名(fail-closed)。签名方案:对发布包的 SHA-256 # 摘要用 updater.key(PKCS#8 Ed25519 私钥)签名。 # 若 updater.key 不存在,则只写摘要、不写签名(兼容未配公钥的旧版升级流程)。 # 用 Go 临时程序计算,避免 PowerShell 5.1 的编码/字节处理问题。 $digestAlgo = '' $digestB64 = '' $signatureAlgo = '' $signatureB64 = '' $keyPath = Join-Path $root 'updater.key' if (Test-Path $keyPath) { $tmpGo = Join-Path $env:TEMP ('tengtu-sign-' + [guid]::NewGuid().ToString('N') + '.go') $goSrc = @' package main import ( "crypto/ed25519" "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/pem" "fmt" "os" ) func main() { if len(os.Args) != 3 { fmt.Fprintln(os.Stderr, "usage: sign ") os.Exit(2) } keyRaw, err := os.ReadFile(os.Args[1]) if err != nil { fmt.Fprintln(os.Stderr, "read key:", err) os.Exit(1) } block, _ := pem.Decode(keyRaw) if block == nil { fmt.Fprintln(os.Stderr, "pem decode failed") os.Exit(1) } privAny, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { fmt.Fprintln(os.Stderr, "parse pkcs8:", err) os.Exit(1) } priv, ok := privAny.(ed25519.PrivateKey) if !ok { fmt.Fprintln(os.Stderr, "not ed25519 private key") os.Exit(1) } data, err := os.ReadFile(os.Args[2]) if err != nil { fmt.Fprintln(os.Stderr, "read exe:", err) os.Exit(1) } h := sha256.Sum256(data) sig := ed25519.Sign(priv, h[:]) fmt.Println(base64.StdEncoding.EncodeToString(h[:])) fmt.Println(base64.StdEncoding.EncodeToString(sig)) } '@ [System.IO.File]::WriteAllText($tmpGo, $goSrc, (New-Object System.Text.UTF8Encoding($false))) try { $out = go run $tmpGo $keyPath $exePath 2>&1 if ($LASTEXITCODE -ne 0) { throw "签名失败:$out" } $lines = @($out | ForEach-Object { $_.ToString() } | Where-Object { $_ -match '\S' }) if ($lines.Count -lt 2) { throw "签名输出异常:$out" } $digestAlgo = 'sha256' $digestB64 = $lines[0].Trim() $signatureAlgo = 'ed25519' $signatureB64 = $lines[1].Trim() Write-Host "==> 已生成摘要与签名(Ed25519/SHA-256)" } finally { if (Test-Path $tmpGo) { Remove-Item $tmpGo -Force -ErrorAction SilentlyContinue } } } else { Write-Host '==> 未找到 updater.key,仅写摘要校验(无签名)' } # 第 7 步:写入输出 if (-not $OutFile) { $OutFile = Join-Path $binDir 'manifest.json' } # 手工拼接 JSON,保持稳定的 2 空格缩进(不同 PS 版本的 ConvertTo-Json 缩进不一致)。 # 只用 ConvertTo-Json 安全转义单个字符串值。 function Esc([string]$s) { ($s | ConvertTo-Json -Compress) } $json = @( '{' ' "schemaVersion": 1,' ' "version": ' + (Esc $Version) + ',' ' "name": ' + (Esc 'TengTu Toolbox') + ',' ' "notes": ' + (Esc $Notes) + ',' ' "publishedAt": ' + (Esc $PublishedAt) + ',' ' "artifacts": [' ' {' ' "url": ' + (Esc $fileName) + ',' ' "filename": ' + (Esc $fileName) + ',' ' "platform": "windows",' ' "arch": "amd64",' ' "size": ' + $fileSize + ',' ' "digestAlgo": ' + (Esc $digestAlgo) + ',' ' "digest": ' + (Esc $digestB64) + ',' ' "signatureAlgo": ' + (Esc $signatureAlgo) + ',' ' "signature": ' + (Esc $signatureB64) ' }' ' ]' '}' ) -join "`r`n" # 无 BOM 写入(Go 的 json.Unmarshal 会拒绝 UTF-8 BOM;PS5.1 的 Out-File -Encoding utf8 会加 BOM)。 [System.IO.File]::WriteAllText($OutFile, $json, (New-Object System.Text.UTF8Encoding($false))) Write-Host "已生成更新清单:$OutFile" Write-Host " 发布包 : $fileName ($fileSize 字节)" Write-Host " 版本 : $Version" Write-Host " 发布时间 : $PublishedAt" Write-Host "" Write-Host "请把 $fileName 与 manifest.json 上传到 https://i.scwy.net/down/toolsbox/" # 第 7 步:可选 —— 用 scp 上传发布包与清单到更新服务器 if ($Up) { Write-Host "==> 正在通过 scp 上传到 $UpTarget ..." scp $exePath $OutFile "$UpTarget" if ($LASTEXITCODE -ne 0) { throw "scp 上传失败,退出码 $LASTEXITCODE" } Write-Host "==> 上传完成。" }