liufumingren 77b5713d02 新增 MSI 安装包:安装即注册 yuncang:// 协议,卸载自动取消注册
需求:打包安装包;协议在**安装时**注册(不依赖先启动软件一次);卸载能清掉注册。

实现(desktop/installer/):
- Product.wxs(WiX 5):
  - Scope=perUser(免管理员、无 UAC),安装到 %LOCALAPPDATA%\Programs\YuncangTrace;
  - Component 里写 HKCU\Software\Classes\yuncang(默认值 / URL Protocol / shell\open\command),
    注册表项属于 MSI 组件 → **卸载时自动删除**,即「取消注册」;
  - 开始菜单快捷方式(RemoveFolder On=uninstall);
  - MajorUpgrade(升级替换、拒绝降级);用户数据 settings/session.json 不在安装目录,卸载保留。
  - 版本号用 $(var.Version) 由构建命令注入,避免和 csproj 两处维护。
- build.ps1:读取 csproj 的 <Version> → dotnet publish(自包含单文件)→ wix build
  → out\YuncangTraceSetup-<版本>.msi。
  ⚠️ 脚本保存为 UTF-8 with BOM:PowerShell 5.1 读无 BOM 脚本会按 GBK 解析,中文注释会让语法报错。
- 工具链:`dotnet tool install --global wix --version 5.0.2`
  (WiX v7 会要求接受 OSMF 付费维护费协议,改用 v5)。

实测(perUser 静默安装 → 协议 → 唤起 → 卸载):
- 安装:文件落到 %LOCALAPPDATA%\Programs\YuncangTrace,HKCU 协议立即指向安装目录
  (安装前指向 desktop\publish,安装后被正确覆盖),开始菜单快捷方式已创建;
- **不启动软件**,直接 Start-Process "yuncang://open?code=INSTALLTEST":
  进程路径 = 安装目录 exe,日志显示 "已恢复登录:管理员 → 收到启动协议 → 查询:INSTALLTEST";
- 卸载:安装目录删除、HKCU 协议删除、开始菜单删除,均无残留;
- build.ps1 完整跑通,产物 54.1 MB(cab 压缩)。

.gitignore:忽略 desktop/installer/out/ 与 *.wixpdb。
desktop/README.md:新增「打安装包(MSI)」章节(构建命令、安装范围、协议注册/卸载行为、
perMachine 改法、BOM 提醒)。
2026-10-17 11:49:26 +08:00

90 lines
3.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#requires -Version 5.1
<#
.SYNOPSIS
打「云仓视频溯源软件」的 MSI 安装包。
.DESCRIPTION
1. 自包含单文件发布 → desktop\publish\YuncangTrace.LocalService.exe
2. 用 WiX 5 打包 → desktop\installer\out\YuncangTraceSetup-<版本>.msi
安装包特性:
- perUser 安装(免管理员、无 UAC),装到 %LOCALAPPDATA%\Programs\YuncangTrace
- **安装即注册** yuncang:// 协议(HKCU),不需要先启动软件
- 卸载时 MSI 自动删除协议与开始菜单(= 取消注册)
.PARAMETER ServerBaseUrl
打进 exe 的生产后端地址(登录用)。默认是占位地址,正式发布必须传真实域名。
.PARAMETER Configuration
默认 Release。
.EXAMPLE
.\build.ps1 -ServerBaseUrl https://trace.example.com
.NOTES
前置:dotnet tool install --global wix --version 5.0.2
(WiX v6+ 需要 OSMF 付费协议,v5 不需要)
#>
param(
[string]$ServerBaseUrl = 'https://packs.example.com',
[string]$Configuration = 'Release'
)
$ErrorActionPreference = 'Stop'
$desktop = Split-Path -Parent $PSScriptRoot # desktop\
$project = Join-Path $desktop 'YuncangTrace.LocalService.csproj'
$publishDir = Join-Path $desktop 'publish'
$outDir = Join-Path $PSScriptRoot 'out'
# 版本号以 csproj 为准(只读文件,不改它)
$projXml = [xml][System.IO.File]::ReadAllText($project, (New-Object System.Text.UTF8Encoding($false)))
$version = ($projXml.Project.PropertyGroup.Version | Where-Object { $_ } | Select-Object -First 1)
if (-not $version) { throw "csproj 里没找到 <Version>" }
Write-Host "版本:$version"
Write-Host "后端:$ServerBaseUrl"
# 1) 发布(自包含单文件:双击/协议唤起都不依赖 .NET 运行时)
Push-Location $desktop
try {
dotnet publish $project -c $Configuration -r win-x64 `
--self-contained true `
-p:PublishSingleFile=true `
-p:IncludeNativeLibrariesForSelfExtract=true `
-p:ServerBaseUrl=$ServerBaseUrl `
-o $publishDir
if ($LASTEXITCODE -ne 0) { throw "dotnet publish 失败($LASTEXITCODE)" }
}
finally {
Pop-Location
}
$exe = Join-Path $publishDir 'YuncangTrace.LocalService.exe'
if (-not (Test-Path $exe)) { throw "发布产物不存在:$exe" }
# 2) 打 MSI
$wix = (Get-Command wix.exe -ErrorAction SilentlyContinue).Source
if (-not $wix) { $wix = Join-Path $env:USERPROFILE '.dotnet\tools\wix.exe' }
if (-not (Test-Path $wix)) {
throw "找不到 wix.exe。先执行:dotnet tool install --global wix --version 5.0.2"
}
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$msi = Join-Path $outDir "YuncangTraceSetup-$version.msi"
Push-Location $PSScriptRoot
try {
& $wix build Product.wxs -arch x64 -d "Version=$version" -o $msi
if ($LASTEXITCODE -ne 0) { throw "wix build 失败($LASTEXITCODE)" }
}
finally {
Pop-Location
}
$size = [math]::Round((Get-Item $msi).Length / 1MB, 1)
Write-Host ""
Write-Host "完成:$msi($size MB)" -ForegroundColor Green
Write-Host "安装(免管理员):msiexec /i `"$msi`""
Write-Host "卸载: msiexec /x `"$msi`""