From 77b5713d02c667a34953236f9e2b0d7144abb200 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 17 Oct 2026 11:49:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20MSI=20=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E5=8C=85=EF=BC=9A=E5=AE=89=E8=A3=85=E5=8D=B3=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=20yuncang://=20=E5=8D=8F=E8=AE=AE=EF=BC=8C=E5=8D=B8=E8=BD=BD?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8F=96=E6=B6=88=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 需求:打包安装包;协议在**安装时**注册(不依赖先启动软件一次);卸载能清掉注册。 实现(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 的 → 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 提醒)。 --- .gitignore | 4 ++ desktop/README.md | 37 +++++++++++++++ desktop/installer/Product.wxs | 86 +++++++++++++++++++++++++++++++++ desktop/installer/build.ps1 | 89 +++++++++++++++++++++++++++++++++++ 4 files changed, 216 insertions(+) create mode 100644 desktop/installer/Product.wxs create mode 100644 desktop/installer/build.ps1 diff --git a/.gitignore b/.gitignore index 1f76942..1dd3530 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,10 @@ artifacts/ *.user *.suo +# 安装包产物(installer/build.ps1 生成) +desktop/installer/out/ +*.wixpdb + # ---------- 运行期 / 日志 ---------- # backend 的 logs/、多实例自测时的 logs-test/、脚本产出的 run/ logs/ diff --git a/desktop/README.md b/desktop/README.md index 0cbeba2..d977f74 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -113,6 +113,43 @@ dotnet publish -c Release -r win-x64 --self-contained true ` 开发阶段想让 `bin\Debug` 的 exe 也能直接双击,两个办法:装官方 SDK/Desktop Runtime 到标准位置, 或者每次用发布产物验证唤起。 +## 打安装包(MSI) + +`desktop/installer/` 下是安装包定义(**WiX 5**,免费;WiX v6+ 需要 OSMF 付费协议,别装最新版)。 + +```powershell +# 工具只需装一次 +dotnet tool install --global wix --version 5.0.2 + +cd desktop\installer +.\build.ps1 -ServerBaseUrl https://正式域名 +# → out\YuncangTraceSetup-<版本>.msi(约 54 MB,cab 压缩) +``` + +`build.ps1` 做两件事:先 `dotnet publish`(自包含单文件,不依赖运行时)→ 再 `wix build`。 +版本号取自 csproj 的 ``,改版本只改那一处。 + +安装包行为: + +| 项 | 说明 | +| --- | --- | +| 安装范围 | **perUser**(免管理员、无 UAC),装到 `%LOCALAPPDATA%\Programs\YuncangTrace` | +| 启动协议 | **安装即注册** `yuncang://`(HKCU),不需要先启动软件 | +| 卸载 | MSI 自动删除 exe、协议注册、开始菜单 —— 即「取消注册」 | +| 升级 | `MajorUpgrade`:装新版会替换旧版;降级被拦 | +| 用户数据 | `%LOCALAPPDATA%\YuncangTrace\{settings,session}.json` 卸载**不删**(重装后仍保持设置/登录) | + +```powershell +msiexec /i out\YuncangTraceSetup-1.0.0.msi # 安装 +msiexec /x out\YuncangTraceSetup-1.0.0.msi # 卸载 +``` + +> 需要「本机所有用户都能用」时:`Product.wxs` 里 `Scope` 改 `perMachine`、注册表根改 `HKLM`, +> 安装时会要管理员权限(UAC)。默认的 perUser 更省事,也够用(软件数据本来就是按用户存的)。 + +> ⚠️ `build.ps1` 必须保存为 **UTF-8 with BOM**:PowerShell 5.1 读无 BOM 的脚本会按 GBK 解析, +> 中文注释直接把语法搞崩。 + ## 本机接口(前端 `/local` 页用的就是这两个) 服务固定监听 `http://localhost:17300`(端口写在前端页面与插件里)。 diff --git a/desktop/installer/Product.wxs b/desktop/installer/Product.wxs new file mode 100644 index 0000000..503954c --- /dev/null +++ b/desktop/installer/Product.wxs @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/desktop/installer/build.ps1 b/desktop/installer/build.ps1 new file mode 100644 index 0000000..8e24ca2 --- /dev/null +++ b/desktop/installer/build.ps1 @@ -0,0 +1,89 @@ +#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 里没找到 " } + +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`""