新增 test-update.ps1:一条命令验证「启动旧版 → 自动更新」(不卸载重装)

用法:
    cd desktop\installer
    .\build.ps1 -Environment Development -Version 1.0.7   # 发布更高版本
    .\test-update.ps1                                     # 启动旧版,等它自己升级

脚本流程:
1. 从后端 /download/version.json 读目标版本,和当前安装版本比较(已是最新直接退出);
2. 关掉运行中的软件(单文件自解压出来的真实进程);
3. 设 YUNCANG_AUTO_UPDATE=1 启动**旧版本**(更新窗口自动开始下载);
4. 每 3s 轮询安装目录 ProductVersion,直到等于目标版本或超时;
5. 检查本机服务 17300 响应与进程是否恢复,输出 ✔/✘ 与耗时。

实测(本次):
    当前安装版本:1.0.6
    后端目标版本:1.0.7
    [3s] 1.0.6 → [10.4s] 1.0.7
    ✔ 自动更新完成:1.0.6 → 1.0.7(耗时 10.4s)
    ✔ 本机服务 17300 有响应(HTTP 401)
    ✔ 软件进程已恢复(PID 12024)
    随后日志显示「已恢复登录:管理员」。

README:新增「验证自动更新(不卸载重装)」小节(命令、脚本行为、输出示例、
每次需打更高版本的提醒)。
This commit is contained in:
liufumingren 2026-10-17 12:47:49 +08:00
parent d32656a2f4
commit 457fbd6c9e
2 changed files with 157 additions and 0 deletions

View File

@ -230,6 +230,39 @@ msiexec /x out\YuncangTraceSetup-1.0.1.msi # 卸载
> ⚠️ `build.ps1` 必须保存为 **UTF-8 with BOM**:PowerShell 5.1 读无 BOM 的脚本会按 GBK 解析,
> 中文注释直接把语法搞崩。
### 验证自动更新(不卸载重装)
```powershell
cd desktop\installer
.\build.ps1 -Environment Development -Version 1.0.7 # 先发布一个更高版本(包+version.json 落到后端)
.\test-update.ps1 # 启动旧版本 → 等它自己升级上去
```
`test-update.ps1` 做的事:
1. 读后端 `/download/version.json` 的目标版本,和当前安装版本比较(已是最新就直接退出);
2. 关掉正在运行的软件(单文件自解压出来的真实进程);
3. 带 `YUNCANG_AUTO_UPDATE=1` 启动**旧版本**(更新窗口出现后自动下载,不用手点);
4. 轮询 `%LOCALAPPDATA%\Programs\YuncangTrace\*.exe` 的 `ProductVersion`,直到变成目标版本;
5. 检查本机服务 17300 是否有响应、进程是否已恢复。
输出形如:
```
当前安装版本:1.0.6
后端目标版本:1.0.7
启动旧版本 1.0.6(自动更新已开启)…
[3s] 安装版本 = 1.0.6+af3f8ab…
[6.1s] 安装版本 =
[10.4s] 安装版本 = 1.0.7+d32656a…
✔ 自动更新完成:1.0.6 → 1.0.7(耗时 10.4s)
✔ 本机服务 17300 有响应(HTTP 401)
✔ 软件进程已恢复(PID 12024)
```
> 每次测更新都要打一个**比当前安装版本高**的包(脚本会拒绝在"已是最新"时白跑)。
## 本机接口(前端 `/local` 页用的就是这两个)
服务固定监听 `http://localhost:17300`(端口写在前端页面与插件里)。

View File

@ -0,0 +1,124 @@
#requires -Version 5.1
<#
.SYNOPSIS
验证「自动更新」全链路(**不卸载重装**):启动当前已安装的旧版本,让它自己升级到后端最新版。
.DESCRIPTION
前置:先用 build.ps1 发布一个**比当前安装版本更高**的包(MSI + version.json 已落到后端)。
脚本做这些事:
1. 从后端 /download/version.json 读目标版本;
2. 与当前安装版本比较,已是最新就直接退出(避免无意义的测试);
3. 关掉正在运行的软件(含单文件自解压出来的真实进程);
4. 启动**旧版本**,并设置 YUNCANG_AUTO_UPDATE=1(更新窗口出现后自动开始下载);
5. 轮询安装目录的 ProductVersion,直到变成目标版本(或超时);
6. 检查本机服务 17300 是否有响应、进程是否已恢复。
.PARAMETER TimeoutSeconds
等更新完成的最长时间,默认 180 秒。
.PARAMETER Backend
后端地址(读 version.json 用),默认 http://127.0.0.1:7300。
.EXAMPLE
cd desktop\installer
.\build.ps1 -Environment Development -Version 1.0.7
.\test-update.ps1
.NOTES
本文件必须保存为 UTF-8 with BOM(PowerShell 5.1 读无 BOM 脚本按 GBK 解析,中文注释会语法报错)。
#>
param(
[int]$TimeoutSeconds = 180,
[string]$Backend = 'http://127.0.0.1:7300'
)
$ErrorActionPreference = 'Stop'
function Get-Base([string]$version) {
if (-not $version) { return $null }
return ($version -split '\+')[0]
}
$exe = Join-Path $env:LOCALAPPDATA 'Programs\YuncangTrace\YuncangTrace.LocalService.exe'
if (-not (Test-Path $exe)) {
throw "本机还没安装本软件:$exe(先装一次 MSI)"
}
$installed = (Get-Item $exe).VersionInfo.ProductVersion
$installedBase = Get-Base $installed
Write-Host "当前安装版本:$installedBase"
# 1) 目标版本
$url = "$($Backend.TrimEnd('/'))/download/version.json?t=$([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())"
$remote = Invoke-RestMethod -Uri $url -TimeoutSec 10
$target = [string]$remote.version
if (-not $target) { throw "version.json 里没有 version 字段" }
Write-Host "后端目标版本:$target"
Write-Host "下载地址:$($remote.downloadUrl)"
if ([version]$installedBase -ge [version]$target) {
Write-Host ""
Write-Host "当前已不低于目标版本,无需测试。先发布一个更高版本再跑本脚本:" -ForegroundColor Yellow
Write-Host " .\build.ps1 -Environment Development -Version <更高版本>" -ForegroundColor Yellow
return
}
# 2) 关掉正在运行的实例(单文件自包含会先解压,进程名相同)
Get-CimInstance Win32_Process |
Where-Object { $_.Name -like '*Yuncang*' } |
ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
Start-Sleep -Seconds 2
# 3) 启动旧版本(自动下载)
$env:YUNCANG_AUTO_UPDATE = '1'
Write-Host ""
Write-Host "启动旧版本 $installedBase(自动更新已开启)…" -ForegroundColor Cyan
$startedAt = Get-Date
Start-Process $exe
# 4) 等版本变化
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
$last = $null
while ((Get-Date) -lt $deadline) {
Start-Sleep -Seconds 3
$current = (Get-Item $exe -ErrorAction SilentlyContinue).VersionInfo.ProductVersion
if ($current -ne $last) {
$last = $current
$spent = [math]::Round(((Get-Date) - $startedAt).TotalSeconds, 1)
Write-Host " [${spent}s] 安装版本 = $current"
}
$base = Get-Base $current
if ($base -and [version]$base -eq [version]$target) { break }
}
# 5) 结果
$final = (Get-Item $exe).VersionInfo.ProductVersion
$finalBase = Get-Base $final
$elapsed = [math]::Round(((Get-Date) - $startedAt).TotalSeconds, 1)
Write-Host ""
if ([version]$finalBase -eq [version]$target) {
Write-Host "✔ 自动更新完成:$installedBase → $finalBase(耗时 ${elapsed}s)" -ForegroundColor Green
} else {
Write-Host "✘ 自动更新未完成:当前 $finalBase,目标 $target(已等 ${TimeoutSeconds}s)" -ForegroundColor Red
}
# 6) 服务与进程
$localCode = & curl.exe -s -o NUL -w "%{http_code}" --max-time 5 "http://127.0.0.1:17300/api/query/"
$processes = @(Get-CimInstance Win32_Process | Where-Object { $_.Name -like '*Yuncang*' })
if ($localCode -and $localCode -ne '000') {
Write-Host "✔ 本机服务 17300 有响应(HTTP $localCode)" -ForegroundColor Green
} else {
Write-Host "✘ 本机服务 17300 无响应" -ForegroundColor Red
}
if ($processes.Count -gt 0) {
Write-Host "✔ 软件进程已恢复(PID $($processes[0].ProcessId))" -ForegroundColor Green
} else {
Write-Host "✘ 没找到软件进程" -ForegroundColor Red
}