liufumingren 228723e0c3 设备端扫码上报 + 桌面端/Android 正式包发布脚手架
AndroidStation(原中控改造为纯设备端)
- 只做三件事:UDP 48899 被桌面端发现、TCP 8087 推送 USB 扫码结果、离线语音播报
- 扫码不再经过 UI:去掉逐键重绘,50ms 自动提交,同一码重复扫码也一律上报
- 离线语音种子(数字/字母/“开始打包”/欢迎语)随包安装,MediaPlayer 播放,启动把音量拉满
- 启动播报「欢迎使用溯源系统,初始化已经完成,扫码即可开始打包」
- 移除 Device Owner / 设备管理员 / lock task / 网络与 IP 设置等非普通应用能力(仅保留开机自启)
- 正式签名 keystore + release 签名配置,versionName/versionCode 可由打包脚本覆盖

发布脚手架
- backend/android_client:Android 正式包构建(build.ps1 + package.config.json)
- backend/scripts/deploy/publish-android.cjs + npm run android:build / android:publish
- 桌面端安装包改为 download/desktop,Android 为 download/android_client;
  version.json 仍走 /download/version.json(老客户端写死),新增 /download/android_client/version.json
- 谷歌插件随桌面端 MSI 安装到 <安装目录>\extension,工具菜单新增「复制谷歌插件地址」
- git-safety-check 增加仓库级禁区(keystore.properties / *.jks / local.properties)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 05:53:27 +08:00

270 lines
10 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 安装包,并生成本机自动更新用的 version.json。
.DESCRIPTION
1. 按 -Environment 从 package.config.json 取该环境的域名(开发 / 生产);
2. dotnet publish(自包含单文件,不依赖 .NET 运行时);
3. wix build 生成 MSI;
4. 计算 MD5 → 写 version.json(version / downloadUrl / md5 / size / publishedAt / buildId);
5. 把 version.json + MSI 复制到后端目录(MSI → backend/app/public/download/desktop/、
version.json → backend/app/public/version/desktop/),供软件启动时
GET /download/version.json 检测更新;
6. 本地 out/ 与后端目录各只保留最近 keepVersions(默认 5)个安装包。
.PARAMETER Environment
Development(默认,连本机 7300)/ Production(用正式域名)。
.PARAMETER Version
覆盖版本号(默认取 csproj 的 <Version>)。例:-Version 1.0.1
.PARAMETER ServerBaseUrl
覆盖该环境配置里的域名(临时打测试包用)。
.PARAMETER Keep
保留多少个历史安装包(默认读 package.config.json 的 keepVersions = 5)。
.PARAMETER ReleaseNotes
写进 version.json 的更新说明(软件弹更新提示时展示)。
.EXAMPLE
.\build.ps1 -Environment Production -Version 1.0.1 -ReleaseNotes "修复若干问题"
.EXAMPLE
.\build.ps1 # 开发环境、版本取 csproj
.NOTES
前置:dotnet tool install --global wix --version 5.0.2
(WiX v6+ 需要 OSMF 付费协议,v5 不需要)
本文件必须保存为 UTF-8 with BOM —— PowerShell 5.1 读无 BOM 脚本会按 GBK 解析,中文注释直接语法报错。
#>
param(
[ValidateSet('Development', 'Production')]
[string]$Environment = 'Development',
[string]$Version,
[string]$ServerBaseUrl,
[int]$Keep = 0,
[string]$ReleaseNotes = ''
)
$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\
$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)))
}
# ---------------- 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 = $Version.Trim().TrimStart('v', 'V')
$null = [version]$Version # 提前校验格式(不合法直接报错)
Write-Host "环境:$Environment"
Write-Host "版本:$Version"
Write-Host "后端:$ServerBaseUrl"
# ---------------- 3. 发布 ----------------
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"
# ---------------- 4. 打 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
$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
}
# ---------------- 5. version.json ----------------
$hash = (Get-FileHash -Algorithm MD5 -Path $msi).Hash.ToLowerInvariant()
$size = (Get-Item $msi).Length
$downloadUrl = "$ServerBaseUrl/download/desktop/$msiName"
$now = Get-Date
$versionJson = [ordered]@{
version = $Version
downloadUrl = $downloadUrl
md5 = $hash
size = $size
publishedAt = $now.ToString('yyyy-MM-ddTHH:mm:sszzz')
buildId = $now.ToString('yyyyMMddHHmmss')
environment = $Environment
releaseNotes = $ReleaseNotes
} | ConvertTo-Json
$versionFile = Join-Path $outDir 'version.json'
Write-Utf8NoBom $versionFile $versionJson
Write-Host "MD5:$hash($([math]::Round($size / 1MB, 1)) MB)"
# ---------------- 6. 复制到后端 ----------------
# 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 ($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 { /* 旧文件坏了就当没有 */ }
}
$dstMsi = Join-Path $downloadDir $msiName
Copy-Item -Path $msi -Destination $dstMsi -Force
# 刷新 mtime:即使上游还有一层协商缓存,也能认出文件变了
(Get-Item $dstMsi).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(MSI)、$versionDir(version.json)"
}
else {
Write-Host "(该环境 copyToServer=false,跳过复制到后端)"
}
# ---------------- 7. 只保留最近 N 个安装包 ----------------
function Remove-OldPackages([string]$dir, [int]$keep) {
if (-not (Test-Path $dir)) { return }
$items = @(
Get-ChildItem -Path $dir -Filter 'YuncangTraceSetup-*.msi' -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:共 $($items.Count) 个,未超过 $keep 个" }
}
Write-Host "保留最近 $Keep 个安装包:"
Remove-OldPackages $outDir $Keep
if ($downloadDir) { Remove-OldPackages $downloadDir $Keep }
# ---------------- 8. 摘要 ----------------
Write-Host ""
Write-Host "完成:$msi" -ForegroundColor Green
Write-Host "版本文件:$versionFile"
Write-Host "下载地址:$downloadUrl"
Write-Host "安装:msiexec /i `"$msi`""
Write-Host "卸载:msiexec /x `"$msi`""