update.ps1 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. # 编译总是把应用 exe 写得最新。
  68. $excluded = @(
  69. 'TengTuToolbox-windows-amd64.exe'
  70. 'TengTu Toolbox-amd64-installer.exe'
  71. 'TengTuToolbox.exe'
  72. )
  73. $builtExe = Get-ChildItem -Path $binDir -Filter '*.exe' |
  74. Where-Object { $excluded -notcontains $_.Name } |
  75. Sort-Object LastWriteTime -Descending |
  76. Select-Object -First 1
  77. if (-not $builtExe) {
  78. throw 'bin 下未找到编译产物,请检查 wails3 build 输出。'
  79. }
  80. $builtExe = $builtExe.FullName
  81. $releaseExe = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe'
  82. Copy-Item -Path $builtExe -Destination $releaseExe -Force
  83. Write-Host "==> 已复制编译产物到 $releaseExe"
  84. }
  85. # 第 1 步:解析发布包
  86. if (-not $Exe) {
  87. $candidate = Join-Path $binDir 'TengTuToolbox-windows-amd64.exe'
  88. if (-not (Test-Path $candidate)) {
  89. $candidate = Join-Path $binDir 'TengTuToolbox.exe'
  90. }
  91. if (-not (Test-Path $candidate)) {
  92. throw '未找到发布包,请把安装包放入 bin 或传入 -Exe。'
  93. }
  94. $Exe = $candidate
  95. }
  96. $exePath = if ([System.IO.Path]::IsPathRooted($Exe)) { $Exe } else { Join-Path (Get-Location) $Exe }
  97. if (-not (Test-Path $exePath)) {
  98. throw "未找到发布包:$exePath"
  99. }
  100. # 第 2 步:文件名(制品 url = 文件名,与清单一起上传)
  101. $fileName = [System.IO.Path]::GetFileName($exePath)
  102. $fileSize = (Get-Item $exePath).Length
  103. # 第 3 步:版本 —— 优先 -Version,其次 settings.go 的 AppVersion,最后 exe 版本信息
  104. if (-not $Version) {
  105. $settingsPath = Join-Path $root 'settings.go'
  106. if (Test-Path $settingsPath) {
  107. $m = Select-String -Path $settingsPath -Pattern 'AppVersion\s*=\s*"([^"]+)"'
  108. if ($m) { $Version = $m.Matches[0].Groups[1].Value }
  109. }
  110. }
  111. if (-not $Version) {
  112. $ver = (Get-Item $exePath).VersionInfo
  113. $Version = if ($ver.ProductVersion) { $ver.ProductVersion } else { $ver.FileVersion }
  114. }
  115. if (-not $Version) { $Version = '0.0.0' }
  116. # 第 4 步:发布时间
  117. if (-not $PublishedAt) {
  118. $PublishedAt = (Get-Date).ToString('yyyy-MM-ddTHH:mm:sszzz')
  119. } else {
  120. try { $PublishedAt = ([datetime]$PublishedAt).ToString('yyyy-MM-ddTHH:mm:sszzz') }
  121. catch { }
  122. }
  123. # 第 5 步:规范化更新说明里的换行 —— 把用户传入的字面 \n(反斜杠+n)替换为真实换行。
  124. # PowerShell 的原生换行转义是反引号 n(`n),但命令行里常见反斜杠写法 \n。
  125. # 若用户已用 `n 得到真实换行,则不受影响;仅替换字面的反斜杠+n。
  126. $Notes = $Notes -replace '\\n', "`n"
  127. # 第 6 步:计算摘要与 Ed25519 签名 —— 升级器配置了 updater.key.pub 公钥后,
  128. # 要求每个 release 必须带签名(fail-closed)。签名方案:对发布包的 SHA-256
  129. # 摘要用 updater.key(PKCS#8 Ed25519 私钥)签名。
  130. # 若 updater.key 不存在,则只写摘要、不写签名(兼容未配公钥的旧版升级流程)。
  131. # 用 Go 临时程序计算,避免 PowerShell 5.1 的编码/字节处理问题。
  132. $digestAlgo = ''
  133. $digestB64 = ''
  134. $signatureAlgo = ''
  135. $signatureB64 = ''
  136. $keyPath = Join-Path $root 'updater.key'
  137. if (Test-Path $keyPath) {
  138. $tmpGo = Join-Path $env:TEMP ('tengtu-sign-' + [guid]::NewGuid().ToString('N') + '.go')
  139. $goSrc = @'
  140. package main
  141. import (
  142. "crypto/ed25519"
  143. "crypto/sha256"
  144. "crypto/x509"
  145. "encoding/base64"
  146. "encoding/pem"
  147. "fmt"
  148. "os"
  149. )
  150. func main() {
  151. if len(os.Args) != 3 {
  152. fmt.Fprintln(os.Stderr, "usage: sign <keyfile> <exe>")
  153. os.Exit(2)
  154. }
  155. keyRaw, err := os.ReadFile(os.Args[1])
  156. if err != nil {
  157. fmt.Fprintln(os.Stderr, "read key:", err)
  158. os.Exit(1)
  159. }
  160. block, _ := pem.Decode(keyRaw)
  161. if block == nil {
  162. fmt.Fprintln(os.Stderr, "pem decode failed")
  163. os.Exit(1)
  164. }
  165. privAny, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  166. if err != nil {
  167. fmt.Fprintln(os.Stderr, "parse pkcs8:", err)
  168. os.Exit(1)
  169. }
  170. priv, ok := privAny.(ed25519.PrivateKey)
  171. if !ok {
  172. fmt.Fprintln(os.Stderr, "not ed25519 private key")
  173. os.Exit(1)
  174. }
  175. data, err := os.ReadFile(os.Args[2])
  176. if err != nil {
  177. fmt.Fprintln(os.Stderr, "read exe:", err)
  178. os.Exit(1)
  179. }
  180. h := sha256.Sum256(data)
  181. sig := ed25519.Sign(priv, h[:])
  182. fmt.Println(base64.StdEncoding.EncodeToString(h[:]))
  183. fmt.Println(base64.StdEncoding.EncodeToString(sig))
  184. }
  185. '@
  186. [System.IO.File]::WriteAllText($tmpGo, $goSrc, (New-Object System.Text.UTF8Encoding($false)))
  187. try {
  188. $out = go run $tmpGo $keyPath $exePath 2>&1
  189. if ($LASTEXITCODE -ne 0) {
  190. throw "签名失败:$out"
  191. }
  192. $lines = @($out | ForEach-Object { $_.ToString() } | Where-Object { $_ -match '\S' })
  193. if ($lines.Count -lt 2) {
  194. throw "签名输出异常:$out"
  195. }
  196. $digestAlgo = 'sha256'
  197. $digestB64 = $lines[0].Trim()
  198. $signatureAlgo = 'ed25519'
  199. $signatureB64 = $lines[1].Trim()
  200. Write-Host "==> 已生成摘要与签名(Ed25519/SHA-256)"
  201. } finally {
  202. if (Test-Path $tmpGo) { Remove-Item $tmpGo -Force -ErrorAction SilentlyContinue }
  203. }
  204. } else {
  205. Write-Host '==> 未找到 updater.key,仅写摘要校验(无签名)'
  206. }
  207. # 第 7 步:写入输出
  208. if (-not $OutFile) { $OutFile = Join-Path $binDir 'manifest.json' }
  209. # 手工拼接 JSON,保持稳定的 2 空格缩进(不同 PS 版本的 ConvertTo-Json 缩进不一致)。
  210. # 只用 ConvertTo-Json 安全转义单个字符串值。
  211. function Esc([string]$s) { ($s | ConvertTo-Json -Compress) }
  212. $json = @(
  213. '{'
  214. ' "schemaVersion": 1,'
  215. ' "version": ' + (Esc $Version) + ','
  216. ' "name": ' + (Esc 'TengTu Toolbox') + ','
  217. ' "notes": ' + (Esc $Notes) + ','
  218. ' "publishedAt": ' + (Esc $PublishedAt) + ','
  219. ' "artifacts": ['
  220. ' {'
  221. ' "url": ' + (Esc $fileName) + ','
  222. ' "filename": ' + (Esc $fileName) + ','
  223. ' "platform": "windows",'
  224. ' "arch": "amd64",'
  225. ' "size": ' + $fileSize + ','
  226. ' "digestAlgo": ' + (Esc $digestAlgo) + ','
  227. ' "digest": ' + (Esc $digestB64) + ','
  228. ' "signatureAlgo": ' + (Esc $signatureAlgo) + ','
  229. ' "signature": ' + (Esc $signatureB64)
  230. ' }'
  231. ' ]'
  232. '}'
  233. ) -join "`r`n"
  234. # 无 BOM 写入(Go 的 json.Unmarshal 会拒绝 UTF-8 BOM;PS5.1 的 Out-File -Encoding utf8 会加 BOM)。
  235. [System.IO.File]::WriteAllText($OutFile, $json, (New-Object System.Text.UTF8Encoding($false)))
  236. Write-Host "已生成更新清单:$OutFile"
  237. Write-Host " 发布包 : $fileName ($fileSize 字节)"
  238. Write-Host " 版本 : $Version"
  239. Write-Host " 发布时间 : $PublishedAt"
  240. Write-Host ""
  241. Write-Host "请把 $fileName 与 manifest.json 上传到 https://i.scwy.net/down/toolsbox/"
  242. # 第 7 步:可选 —— 用 scp 上传发布包与清单到更新服务器
  243. if ($Up) {
  244. Write-Host "==> 正在通过 scp 上传到 $UpTarget ..."
  245. scp $exePath $OutFile "$UpTarget"
  246. if ($LASTEXITCODE -ne 0) {
  247. throw "scp 上传失败,退出码 $LASTEXITCODE"
  248. }
  249. Write-Host "==> 上传完成。"
  250. }