用法:
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:新增「验证自动更新(不卸载重装)」小节(命令、脚本行为、输出示例、
每次需打更高版本的提醒)。
125 lines
4.6 KiB
PowerShell
125 lines
4.6 KiB
PowerShell
#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
|
||
}
|