#requires -Version 5.1 <# .SYNOPSIS 打「云仓视频溯源软件」的安装包(MSI 和/或 EXE),并生成本机自动更新用的 version.json。 .DESCRIPTION 1. 按 -Environment 从 package.config.json 取该环境的域名(开发 / 生产); 2. 打包 Chrome 插件:源码压缩到 extension\dist(esbuild,只压缩不混淆); 3. dotnet publish(自包含单文件,不依赖 .NET 运行时;csproj 打的是 extension\dist); 4. wix build 生成 MSI;Inno Setup(ISCC)生成 EXE; 5. 计算 MD5 → 写 version.json(version / downloadUrl / md5 / size / publishedAt / buildId; EXE 版另有 downloadUrlExe / md5Exe / sizeExe); 6. 把 version.json + 安装包复制到后端目录(安装包 → backend/app/public/download/desktop/、 version.json → backend/app/public/version/desktop/),供软件启动时 GET /download/version.json 检测更新; 7. 本地 out/ 与后端目录各只保留最近 keepVersions(默认 5)个安装包。 .PARAMETER Environment Development(默认,连本机 7300)/ Production(用正式域名)。 .PARAMETER Version 覆盖版本号(默认取 csproj 的 )。例:-Version 1.0.1 .PARAMETER ServerBaseUrl 覆盖该环境配置里的域名(临时打测试包用)。 .PARAMETER Target 打哪种安装包:Both(默认,MSI + EXE)/ Msi / Exe。 .PARAMETER Keep 保留多少个历史安装包(默认读 package.config.json 的 keepVersions = 5)。 .PARAMETER ReleaseNotes 写进 version.json 的更新说明(软件弹更新提示时展示)。 .PARAMETER SkipDeploy 只在本机 out\ 出包,不复制到后端目录(调试打包脚本用)。 .EXAMPLE .\build.ps1 -Environment Production -Version 1.0.1 -ReleaseNotes "修复若干问题" .EXAMPLE .\build.ps1 -Target Exe # 只出 EXE(部分电脑装不了 MSI 时用) .EXAMPLE .\build.ps1 # 开发环境、版本取 csproj、MSI + EXE .NOTES 前置: dotnet tool install --global wix --version 5.0.2 (WiX v6+ 需要 OSMF 付费协议,v5 不需要) winget install --id JRSoftware.InnoSetup (EXE 包用,装完有 ISCC.exe) 本文件必须保存为 UTF-8 with BOM —— PowerShell 5.1 读无 BOM 脚本会按 GBK 解析,中文注释直接语法报错。 #> param( [ValidateSet('Development', 'Production')] [string]$Environment = 'Development', [string]$Version, [string]$ServerBaseUrl, [ValidateSet('Both', 'Msi', 'Exe')] [string]$Target = 'Both', [int]$Keep = 0, [string]$ReleaseNotes = '', [switch]$SkipDeploy ) $ErrorActionPreference = 'Stop' # dotnet 可能装在用户目录(免管理员安装):wix 是 dotnet 全局工具(apphost), # **只认 DOTNET_ROOT / 默认安装路径,不认 PATH** —— 不设置会报「You must install .NET」。 $dotnetRoot = Join-Path $env:LOCALAPPDATA 'Microsoft\dotnet' $dotnetExe = Join-Path $dotnetRoot 'dotnet.exe' if (Test-Path $dotnetExe) { $env:DOTNET_ROOT = $dotnetRoot if (($env:Path -split ';') -notcontains $dotnetRoot) { $env:Path = "$dotnetRoot;$env:Path" } Write-Host "DOTNET_ROOT=$dotnetRoot" } else { # 兜底:用 PATH 里的 dotnet $dotnetExe = 'dotnet' } if (-not (Get-Command $dotnetExe -ErrorAction SilentlyContinue)) { throw "找不到 dotnet。请先安装 .NET 8 SDK,或设置 DOTNET_ROOT。" } $desktop = Split-Path -Parent $PSScriptRoot # desktop\ $repoRoot = Split-Path -Parent $desktop # 仓库根(backend\ / extension\ 在这里) $project = Join-Path $desktop 'YuncangTrace.LocalService.csproj' $publishDir = Join-Path $desktop 'publish' $outDir = Join-Path $PSScriptRoot 'out' function Read-Utf8([string]$p) { return [System.IO.File]::ReadAllText($p, (New-Object System.Text.UTF8Encoding($false))) } function Write-Utf8NoBom([string]$p, [string]$text) { [System.IO.File]::WriteAllText($p, $text, (New-Object System.Text.UTF8Encoding($false))) } # Inno Setup 编译器(EXE 安装包用):PATH → 默认安装位置(winget 装到用户目录) function Resolve-Iscc { $candidates = @( (Get-Command ISCC.exe -ErrorAction SilentlyContinue).Source, (Join-Path $env:LOCALAPPDATA 'Programs\Inno Setup 6\ISCC.exe'), (Join-Path ${env:ProgramFiles(x86)} 'Inno Setup 6\ISCC.exe'), (Join-Path $env:ProgramFiles 'Inno Setup 6\ISCC.exe') ) foreach ($candidate in $candidates) { if ($candidate -and (Test-Path $candidate)) { return $candidate } } return $null } # ---------------- 1. 配置 ---------------- $configPath = Join-Path $PSScriptRoot 'package.config.json' $cfg = Read-Utf8 $configPath | ConvertFrom-Json $envProp = $cfg.environments.PSObject.Properties[$Environment] if (-not $envProp) { throw "package.config.json 里没有环境:$Environment" } $envCfg = $envProp.Value if (-not $ServerBaseUrl) { $ServerBaseUrl = $envCfg.serverBaseUrl } if (-not $ServerBaseUrl) { throw "环境 $Environment 没配 serverBaseUrl" } $ServerBaseUrl = $ServerBaseUrl.TrimEnd('/') if ($Keep -le 0) { $Keep = [int]$cfg.keepVersions } if ($Keep -le 0) { $Keep = 5 } # ---------------- 2. 版本 ---------------- $projXml = [xml](Read-Utf8 $project) $projVersion = ($projXml.Project.PropertyGroup.Version | Where-Object { $_ } | Select-Object -First 1) if (-not $Version) { $Version = $projVersion } if (-not $Version) { throw "csproj 里没找到 ,也没传 -Version" } $Version = $Version.Trim().TrimStart('v', 'V') $null = [version]$Version # 提前校验格式(不合法直接报错) Write-Host "环境:$Environment" Write-Host "版本:$Version" Write-Host "后端:$ServerBaseUrl" # 打哪种安装包:Both = MSI + EXE(默认)。 # EXE 版存在的意义:现场有电脑装不了 MSI(Windows Installer 服务被禁用 / 安全软件拦截 / 组策略), # EXE 走 Inno Setup,不依赖 Windows Installer。 $buildMsi = $Target -ne 'Exe' $buildExe = $Target -ne 'Msi' Write-Host "安装包:$Target" # ---------------- 3. 插件打包(压缩源码 → extension\dist,csproj 打的是 dist) ---------------- # 用 backend 的 node_modules 里的 esbuild;没装就明确报错,别静默打到上一次的旧 dist $bundleScript = Join-Path $repoRoot 'backend\scripts\deploy\build-extension.cjs' if (-not (Test-Path $bundleScript)) { throw "找不到插件打包脚本:$bundleScript" } $nodeExe = (Get-Command node -ErrorAction SilentlyContinue).Source if (-not $nodeExe) { throw "找不到 node,无法打包谷歌插件(先装 Node.js,再 cd backend; npm install)" } & $nodeExe $bundleScript if ($LASTEXITCODE -ne 0) { throw "插件打包失败($LASTEXITCODE):先 cd backend; npm install" } # ---------------- 4. 发布 ---------------- Push-Location $desktop try { & $dotnetExe publish $project -c Release -r win-x64 ` --self-contained true ` -p:PublishSingleFile=true ` -p:IncludeNativeLibrariesForSelfExtract=true ` -p:Version=$Version ` -p:AppEnvironment=$Environment ` -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" } # ffmpeg 必须随包安装:回放校验 / 多路合并 / 主码流转 MJPEG 都依赖它。 # 客户机(尤其非设备服务器)普遍没有 ffmpeg,缺了它就只能拿到「合并失败,保留各路段」。 $ffmpeg = Join-Path $publishDir 'native\ffmpeg\ffmpeg.exe' if (-not (Test-Path $ffmpeg)) { throw "缺少 ffmpeg:$ffmpeg`n请把 ffmpeg.exe 放到 desktop\native\ffmpeg\ffmpeg.exe 后再打包(详见该目录的 README.md)。" } Write-Host "ffmpeg:$([math]::Round((Get-Item $ffmpeg).Length / 1MB, 1)) MB" # ---------------- 5. 打安装包 ---------------- New-Item -ItemType Directory -Force -Path $outDir | Out-Null # 4a. MSI(WiX) $msi = $null $msiName = $null if ($buildMsi) { $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" } $msiName = "YuncangTraceSetup-$Version.msi" $msi = Join-Path $outDir $msiName 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 } } # 4b. EXE(Inno Setup,不依赖 Windows Installer) $setupExe = $null $exeName = $null if ($buildExe) { $iscc = Resolve-Iscc if (-not $iscc) { if ($Target -eq 'Exe') { throw "找不到 ISCC.exe(Inno Setup 6)。先执行:winget install --id JRSoftware.InnoSetup" } # Both 模式下没有 Inno 也别把 MSI 流程一起搞挂:只提示,继续出 MSI Write-Warning "找不到 ISCC.exe(Inno Setup 6),本次跳过 EXE(只出 MSI)。要 EXE 请先:winget install --id JRSoftware.InnoSetup" $buildExe = $false } } if ($buildExe) { $exeName = "YuncangTraceSetup-$Version.exe" $setupExe = Join-Path $outDir $exeName Push-Location $PSScriptRoot try { & $iscc "/DVersion=$Version" "/O$outDir" "YuncangTrace.iss" if ($LASTEXITCODE -ne 0) { throw "ISCC 编译失败($LASTEXITCODE)" } } finally { Pop-Location } if (-not (Test-Path $setupExe)) { throw "EXE 安装包没有生成:$setupExe" } Write-Host "EXE:$([math]::Round((Get-Item $setupExe).Length / 1MB, 1)) MB" } # ---------------- 6. version.json ---------------- $now = Get-Date $msiHash = $null $msiSize = 0 if ($msi) { $msiHash = (Get-FileHash -Algorithm MD5 -Path $msi).Hash.ToLowerInvariant() $msiSize = (Get-Item $msi).Length Write-Host "MSI:$msiHash($([math]::Round($msiSize / 1MB, 1)) MB)" } $exeHash = $null $exeSize = 0 if ($setupExe) { $exeHash = (Get-FileHash -Algorithm MD5 -Path $setupExe).Hash.ToLowerInvariant() $exeSize = (Get-Item $setupExe).Length Write-Host "EXE:$exeHash($([math]::Round($exeSize / 1MB, 1)) MB)" } # downloadUrl/md5/size 是**老客户端唯一认识的字段**,必须继续指向 MSI(老客户端只会 msiexec); # downloadUrlExe/md5Exe/sizeExe 给新客户端,新客户端优先下载 EXE(不依赖 Windows Installer)。 $primaryName = if ($msi) { $msiName } else { $exeName } $primaryHash = if ($msi) { $msiHash } else { $exeHash } $primarySize = if ($msi) { $msiSize } else { $exeSize } $downloadUrl = "$ServerBaseUrl/download/desktop/$primaryName" if (-not $msi) { Write-Warning "本次没有打 MSI:downloadUrl 只能指向 EXE,老客户端(按 .msi 校验 / msiexec 安装)无法自动更新。" } $versionJson = [ordered]@{ version = $Version downloadUrl = $downloadUrl md5 = $primaryHash size = $primarySize } if ($setupExe) { $versionJson.downloadUrlExe = "$ServerBaseUrl/download/desktop/$exeName" $versionJson.md5Exe = $exeHash $versionJson.sizeExe = $exeSize } $versionJson.publishedAt = $now.ToString('yyyy-MM-ddTHH:mm:sszzz') $versionJson.buildId = $now.ToString('yyyyMMddHHmmss') $versionJson.environment = $Environment $versionJson.releaseNotes = $ReleaseNotes $versionJson = $versionJson | ConvertTo-Json $versionFile = Join-Path $outDir 'version.json' Write-Utf8NoBom $versionFile $versionJson # ---------------- 7. 复制到后端 ---------------- # MSI → app/public/download/desktop/(/download/desktop/xxx.msi,长缓存) # version.json → app/public/version/desktop/(/download/version.json 由路由提供,绝不能缓存) $skipVersionJson = $false $downloadDir = $null $versionDir = $null $existingVj = $null if ($SkipDeploy) { Write-Host "(-SkipDeploy:不复制到后端目录)" } elseif ($envCfg.copyToServer -ne $false) { $downloadDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.downloadFolder)) $versionDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.versionFolder)) New-Item -ItemType Directory -Force -Path $downloadDir | Out-Null New-Item -ItemType Directory -Force -Path $versionDir | Out-Null # 后端已经有更新版本:说明这次是在补打旧版本,不能把 version.json 降回去 # (旧版本打包脚本把 version.json 放在 app/public/version/ 根目录,这里两个位置都看) $existingVj = Join-Path $versionDir 'version.json' $legacyVj = Join-Path (Split-Path -Parent $versionDir) 'version.json' foreach ($candidate in @($existingVj, $legacyVj)) { if (-not (Test-Path $candidate)) { continue } try { $old = (Read-Utf8 $candidate) | ConvertFrom-Json if ($old.version -and ([version]$old.version -gt [version]$Version)) { $skipVersionJson = $true Write-Warning "后端 version.json 已是更新版本($($old.version)),本次不覆盖它" break } } catch { /* 旧文件坏了就当没有 */ } } foreach ($package in @($msi, $setupExe)) { if (-not $package) { continue } $dstPackage = Join-Path $downloadDir (Split-Path -Leaf $package) Copy-Item -Path $package -Destination $dstPackage -Force # 刷新 mtime:即使上游还有一层协商缓存,也能认出文件变了 (Get-Item $dstPackage).LastWriteTime = $now } if (-not $skipVersionJson) { Write-Utf8NoBom $existingVj $versionJson (Get-Item $existingVj).LastWriteTime = $now # 旧版本打包时把 version.json 放在 app/public/version/ 根目录,顺手清掉(避免两份不一致) if (Test-Path $legacyVj) { Remove-Item $legacyVj -Force } } # 更早的版本把 version.json 放在静态目录里,顺手清掉(避免两份不一致) $staleVj = Join-Path $downloadDir 'version.json' if (Test-Path $staleVj) { Remove-Item $staleVj -Force } Write-Host "已复制到:$downloadDir(安装包)、$versionDir(version.json)" } else { Write-Host "(该环境 copyToServer=false,跳过复制到后端)" } # ---------------- 8. 只保留最近 N 个安装包 ---------------- function Remove-OldPackages([string]$dir, [int]$keep, [string]$pattern) { if (-not (Test-Path $dir)) { return } $items = @( Get-ChildItem -Path $dir -Filter $pattern -File | ForEach-Object { if ($_.BaseName -match 'YuncangTraceSetup-(\d+(\.\d+){1,3})$') { [pscustomobject]@{ File = $_; Ver = [version]$Matches[1] } } } | Sort-Object Ver -Descending ) $removed = 0 $items | Select-Object -Skip $keep | ForEach-Object { Remove-Item $_.File.FullName -Force $removed++ Write-Host " 清理旧包:$($_.File.Name)" } if ($removed -eq 0) { Write-Host " $dir($pattern):共 $($items.Count) 个,未超过 $keep 个" } } Write-Host "保留最近 $Keep 个安装包(.msi / .exe 分别计算):" foreach ($pattern in @('YuncangTraceSetup-*.msi', 'YuncangTraceSetup-*.exe')) { Remove-OldPackages $outDir $Keep $pattern if ($downloadDir) { Remove-OldPackages $downloadDir $Keep $pattern } } # ---------------- 9. 摘要 ---------------- Write-Host "" if ($msi) { Write-Host "MSI:$msi" -ForegroundColor Green } if ($setupExe) { Write-Host "EXE:$setupExe" -ForegroundColor Green } Write-Host "版本文件:$versionFile" Write-Host "下载地址:$downloadUrl" if ($setupExe) { Write-Host "静默安装(EXE):`"$setupExe`" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" } if ($msi) { Write-Host "静默安装(MSI):msiexec /i `"$msi`" /passive /norestart" } Write-Host "卸载:设置 → 应用 → 云仓视频溯源软件(或运行安装目录下的 unins000.exe)"