1
0

update.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <#
  2. .SYNOPSIS
  3. 生成 bin/manifest.json —— Wails 内置升级器的更新清单(静态站点方案)。
  4. .DESCRIPTION
  5. 先用 `wails3 build -tags production` 编译应用,把编译产物 bin\腾图工具箱.exe
  6. 复制为 bin\TengTuToolbox-windows-amd64.exe(升级器期望的发布包名),
  7. 再读取 bin 下的发布包,计算文件大小与版本,写入升级清单 manifest.json。
  8. 将发布包与清单一起上传到更新站点 https://i.scwy.net/down/toolsbox/。
  9. 清单必须命名为 manifest.json;制品 url 即文件名(同一目录)。
  10. .PARAMETER Exe
  11. 用于生成清单的发布包(绝对或相对路径)。
  12. 默认 bin\TengTuToolbox-windows-amd64.exe,回退到 bin\TengTuToolbox.exe。
  13. .PARAMETER Version
  14. 清单版本。默认从 settings.go 的 AppVersion 读取,否则取 exe 版本信息。
  15. .PARAMETER Notes
  16. 更新说明(多行,用反引号 n 换行)。默认为空。
  17. .PARAMETER PublishedAt
  18. 发布时间(ISO 8601 或可解析格式)。默认为当前时间。
  19. .PARAMETER OutFile
  20. 输出清单路径。默认 bin\manifest.json。
  21. .PARAMETER Up
  22. 若设置,生成清单后用 scp 把发布包与清单上传到更新服务器,远端目录用 -UpTarget。
  23. .PARAMETER UpTarget
  24. scp 远端目标目录。默认:
  25. ease@192.168.100.22:/home/ease/server/blog/down/toolsbox
  26. .PARAMETER Build
  27. 若设置,先用 `wails3 build -tags production` 编译应用,再把编译产物
  28. bin\腾图工具箱.exe 复制为发布包名。不设置时直接使用 bin 下已有的发布包。
  29. .EXAMPLE
  30. .\update.ps1 -Build
  31. .EXAMPLE
  32. .\update.ps1 -Build -Version 0.2.0 -Notes "`n- 新增功能 A.`n- 修复 B."
  33. .EXAMPLE
  34. .\update.ps1 -Build -Up
  35. #>
  36. [CmdletBinding()]
  37. param(
  38. [string]$Exe = "",
  39. [string]$Version = "",
  40. [string]$Notes = "",
  41. [string]$PublishedAt = "",
  42. [string]$OutFile = "",
  43. [switch]$Up,
  44. [string]$UpTarget = "ease@192.168.100.22:/home/ease/server/blog/down/toolsbox",
  45. [switch]$Build
  46. )
  47. $ErrorActionPreference = 'Stop'
  48. $root = Split-Path -Parent $MyInvocation.MyCommand.Path
  49. $binDir = Join-Path $root 'bin'
  50. # 第 0 步:可选编译 —— 仅当 -Build 时用 `wails3 build -tags production` 编译应用
  51. # (production 标签:注册右键菜单「标签」注册表项),再把编译产物复制为升级器
  52. # 期望的发布包名。不设置 -Build 时直接用 bin 下已有的发布包。
  53. if ($Build) {
  54. Write-Host '==> 正在用 wails3 build -tags production 编译 ...'
  55. Push-Location $root
  56. try {
  57. wails3 build -tags production
  58. if ($LASTEXITCODE -ne 0) {
  59. throw "wails3 build 失败,退出码 $LASTEXITCODE"
  60. }
  61. } finally {
  62. Pop-Location
  63. }
  64. # 定位刚编译出的可执行文件。不要在脚本里硬编码中文产品名「腾图工具箱.exe」:
  65. # PowerShell 5.1 按系统 ANSI 代码页读取 .ps1,无 BOM 的 UTF-8 中文字面量会变乱码,
  66. # 导致 Test-Path 失败。改为取 bin 下最新写入、且非发布/安装/服务版制品的 .exe,
  67. # 编译总是把应用 GUI exe 写得最新。
  68. #
  69. # 注意:必须排除 service 版(TengTuToolbox-service.exe / 腾图工具箱-service.exe)!
  70. # 否则「最新写入 exe」可能选中 service 版,导致 manifest 的 size/摘要/签名全
  71. # 按 service 版生成、并把错误二进制当 GUI 发布包上传 —— 即此前升级失败的根因。
  72. $excluded = @(
  73. 'TengTuToolbox-windows-amd64.exe'
  74. 'TengTu Toolbox-amd64-installer.exe'
  75. 'TengTuToolbox.exe'
  76. 'TengTuToolbox-service.exe'
  77. '腾图工具箱-service.exe'
  78. )
  79. $builtExe = Get-ChildItem -Path $binDir -Filter '*.exe' |
  80. Where-Object { $excluded -notcontains $_.Name } |
  81. Sort-Object LastWriteTime -Descending |
  82. Select-Object -First 1
  83. if (-not $builtExe) {
  84. throw 'bin 下未找到 GUI 编译产物(service 版已排除),请检查 wails3 build 输出。'
  85. }
  86. $builtExe = $builtExe.FullName
  87. $releaseExe = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe'
  88. Copy-Item -Path $builtExe -Destination $releaseExe -Force
  89. Write-Host "==> 已复制 GUI 编译产物到 $releaseExe"
  90. }
  91. # 第 1 步:解析发布包
  92. if (-not $Exe) {
  93. # 优先英文发布包名;否则回退中文 GUI 版「腾图工具箱.exe」。
  94. # 两个候选都必须是 GUI 版 —— 绝不能回退到 service 版(否则清单会按 service 版
  95. # 生成摘要/签名并上传错误二进制,导致客户端升级校验失败)。
  96. $candidate = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe'
  97. if (-not (Test-Path $candidate)) {
  98. $candidate = Join-Path $binDir 'TengTuToolbox.exe'
  99. }
  100. if (-not (Test-Path $candidate)) {
  101. $candidate = Join-Path $binDir '腾图工具箱.exe'
  102. }
  103. if (-not (Test-Path $candidate)) {
  104. throw '未找到 GUI 发布包,请先运行 -Build 或用 wails3 build 编译,或传入 -Exe。'
  105. }
  106. $Exe = $candidate
  107. }
  108. $exePath = if ([System.IO.Path]::IsPathRooted($Exe)) { $Exe } else { Join-Path (Get-Location) $Exe }
  109. if (-not (Test-Path $exePath)) {
  110. throw "未找到发布包:$exePath"
  111. }
  112. # 第 2 步:文件名(制品 url = 文件名,与清单一起上传)
  113. $fileName = [System.IO.Path]::GetFileName($exePath)
  114. $fileSize = (Get-Item $exePath).Length
  115. # 第 3 步:版本 —— 优先 -Version,其次 settings.go 的 AppVersion,最后 exe 版本信息
  116. if (-not $Version) {
  117. $settingsPath = Join-Path $root 'settings.go'
  118. if (Test-Path $settingsPath) {
  119. $m = Select-String -Path $settingsPath -Pattern 'AppVersion\s*=\s*"([^"]+)"'
  120. if ($m) { $Version = $m.Matches[0].Groups[1].Value }
  121. }
  122. }
  123. if (-not $Version) {
  124. $ver = (Get-Item $exePath).VersionInfo
  125. $Version = if ($ver.ProductVersion) { $ver.ProductVersion } else { $ver.FileVersion }
  126. }
  127. if (-not $Version) { $Version = '0.0.0' }
  128. # 第 4 步:发布时间
  129. if (-not $PublishedAt) {
  130. $PublishedAt = (Get-Date).ToString('yyyy-MM-ddTHH:mm:sszzz')
  131. } else {
  132. try { $PublishedAt = ([datetime]$PublishedAt).ToString('yyyy-MM-ddTHH:mm:sszzz') }
  133. catch { }
  134. }
  135. # 第 5 步:规范化更新说明里的换行 —— 把用户传入的字面 \n(反斜杠+n)替换为真实换行。
  136. # PowerShell 的原生换行转义是反引号 n(`n),但命令行里常见反斜杠写法 \n。
  137. # 若用户已用 `n 得到真实换行,则不受影响;仅替换字面的反斜杠+n。
  138. $Notes = $Notes -replace '\\n', "`n"
  139. # 第 6 步:计算摘要与 Ed25519 签名 —— 升级器配置了 updater.key.pub 公钥后,
  140. # 要求每个 release 必须带签名(fail-closed)。签名方案:对发布包的 SHA-256
  141. # 摘要用 updater.key(PKCS#8 Ed25519 私钥)签名。
  142. # 若 updater.key 不存在,则只写摘要、不写签名(兼容未配公钥的旧版升级流程)。
  143. # 用 Go 临时程序计算,避免 PowerShell 5.1 的编码/字节处理问题。
  144. $digestAlgo = ''
  145. $digestB64 = ''
  146. $signatureAlgo = ''
  147. $signatureB64 = ''
  148. $keyPath = Join-Path $root 'updater.key'
  149. if (Test-Path $keyPath) {
  150. $tmpGo = Join-Path $env:TEMP ('tengtu-sign-' + [guid]::NewGuid().ToString('N') + '.go')
  151. $goSrc = @'
  152. package main
  153. import (
  154. "crypto/ed25519"
  155. "crypto/sha256"
  156. "crypto/x509"
  157. "encoding/base64"
  158. "encoding/pem"
  159. "fmt"
  160. "os"
  161. )
  162. func main() {
  163. if len(os.Args) != 3 {
  164. fmt.Fprintln(os.Stderr, "usage: sign <keyfile> <exe>")
  165. os.Exit(2)
  166. }
  167. keyRaw, err := os.ReadFile(os.Args[1])
  168. if err != nil {
  169. fmt.Fprintln(os.Stderr, "read key:", err)
  170. os.Exit(1)
  171. }
  172. block, _ := pem.Decode(keyRaw)
  173. if block == nil {
  174. fmt.Fprintln(os.Stderr, "pem decode failed")
  175. os.Exit(1)
  176. }
  177. privAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  178. if err != nil {
  179. fmt.Fprintln(os.Stderr, "parse pkcs8:", err)
  180. os.Exit(1)
  181. }
  182. priv, ok := privAny.(ed25519.PrivateKey)
  183. if !ok {
  184. fmt.Fprintln(os.Stderr, "not ed25519 private key")
  185. os.Exit(1)
  186. }
  187. data, err := os.ReadFile(os.Args[2])
  188. if err != nil {
  189. fmt.Fprintln(os.Stderr, "read exe:", err)
  190. os.Exit(1)
  191. }
  192. h := sha256.Sum256(data)
  193. sig := ed25519.Sign(priv, h[:])
  194. fmt.Println(base64.StdEncoding.EncodeToString(h[:]))
  195. fmt.Println(base64.StdEncoding.EncodeToString(sig))
  196. }
  197. '@
  198. [System.IO.File]::WriteAllText($tmpGo, $goSrc, (New-Object System.Text.UTF8Encoding($false)))
  199. try {
  200. $out = go run $tmpGo $keyPath $exePath 2>&1
  201. if ($LASTEXITCODE -ne 0) {
  202. throw "签名失败:$out"
  203. }
  204. $lines = @($out | ForEach-Object { $_.ToString() } | Where-Object { $_ -match '\S' })
  205. if ($lines.Count -lt 2) {
  206. throw "签名输出异常:$out"
  207. }
  208. $digestAlgo = 'sha256'
  209. $digestB64 = $lines[0].Trim()
  210. $signatureAlgo = 'ed25519'
  211. $signatureB64 = $lines[1].Trim()
  212. Write-Host "==> 已生成摘要与签名(Ed25519/SHA-256)"
  213. } finally {
  214. if (Test-Path $tmpGo) { Remove-Item $tmpGo -Force -ErrorAction SilentlyContinue }
  215. }
  216. } else {
  217. Write-Host '==> 未找到 updater.key,仅写摘要校验(无签名)'
  218. }
  219. # 第 7 步:写入输出
  220. if (-not $OutFile) { $OutFile = Join-Path $binDir 'manifest.json' }
  221. # 手工拼接 JSON,保持稳定的 2 空格缩进(不同 PS 版本的 ConvertTo-Json 缩进不一致)。
  222. # 只用 ConvertTo-Json 安全转义单个字符串值。
  223. function Esc([string]$s) { ($s | ConvertTo-Json -Compress) }
  224. $json = @(
  225. '{'
  226. ' "schemaVersion": 1,'
  227. ' "version": ' + (Esc $Version) + ','
  228. ' "name": ' + (Esc 'TengTu Toolbox') + ','
  229. ' "notes": ' + (Esc $Notes) + ','
  230. ' "publishedAt": ' + (Esc $PublishedAt) + ','
  231. ' "artifacts": ['
  232. ' {'
  233. ' "url": ' + (Esc $fileName) + ','
  234. ' "filename": ' + (Esc $fileName) + ','
  235. ' "platform": "windows",'
  236. ' "arch": "amd64",'
  237. ' "size": ' + $fileSize + ','
  238. ' "digestAlgo": ' + (Esc $digestAlgo) + ','
  239. ' "digest": ' + (Esc $digestB64) + ','
  240. ' "signatureAlgo": ' + (Esc $signatureAlgo) + ','
  241. ' "signature": ' + (Esc $signatureB64)
  242. ' }'
  243. ' ]'
  244. '}'
  245. ) -join "`r`n"
  246. # 无 BOM 写入(Go 的 json.Unmarshal 会拒绝 UTF-8 BOM;PS5.1 的 Out-File -Encoding utf8 会加 BOM)。
  247. [System.IO.File]::WriteAllText($OutFile, $json, (New-Object System.Text.UTF8Encoding($false)))
  248. Write-Host "已生成更新清单:$OutFile"
  249. Write-Host " 发布包 : $fileName ($fileSize 字节)"
  250. Write-Host " 版本 : $Version"
  251. Write-Host " 发布时间 : $PublishedAt"
  252. Write-Host ""
  253. Write-Host "请把 $fileName 与 manifest.json 上传到 https://i.scwy.net/down/toolsbox/"
  254. # 第 7 步:可选 —— 用 scp 上传发布包与清单到更新服务器
  255. if ($Up) {
  256. Write-Host "==> 正在通过 scp 上传到 $UpTarget ..."
  257. scp $exePath $OutFile "$UpTarget"
  258. if ($LASTEXITCODE -ne 0) {
  259. throw "scp 上传失败,退出码 $LASTEXITCODE"
  260. }
  261. Write-Host "==> 上传完成。"
  262. }