liufumingren 43dae198db chore: Android 扫码台设置页、发布脚本健壮性与文档同步
- Android 扫码台:设置页新增「设为默认桌面 / 解除默认桌面 / 回到盒子桌面 / 打开系统设置」等现场免 adb 操作
- android_client/build.ps1:gradle 的 javac 提示走 stderr,改成 2>&1 逐行 Write-Host,避免被当成构建失败
- deploy-lib.cjs:ssh 加 ServerAliveInterval/CountMax 与 -n,避免发布脚本卡在远端命令结束前不退出
- set-password.js:重置密码时补写 password_changed_at(否则旧 JWT 不失效)
- .gitignore:忽略 video_split/(调试用的视频拆帧产物)
- 文档同步:README / backend README(微信网关、成员扫码绑定、平台管理员、顶栏下载入口、已下线的自建微信表)/
  database README / desktop README

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:21:41 +08:00

254 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
打「AndroidStation 设备端」的正式签名 APK,并生成下载用的 version.json。
.DESCRIPTION
1. 按 -Environment 从 package.config.json 取该环境的下载域名 / 目标目录;
2. gradlew assembleRelease:版本号用 -PversionName/-PversionCode 覆盖,
签名读 AndroidStation\keystore.properties(正式 keystore,不入库);
3. 算 MD5 → 写 version.json(version / versionCode / downloadUrl / md5 / size / publishedAt / buildId);
4. APK 复制到 out\;copyToServer=true 时再复制到后端:
APK → app\public\download\android_client\(nginx 长缓存)
version.json → app\public\version\android_client\(由 controller/download.js 关缓存后提供)
5. out\ 与后端目录各只保留最近 keepVersions(默认 5)个安装包。
.PARAMETER Environment
Development(连本机 7300)/ Production(正式域名)。
.PARAMETER Version
版本号 x.y.z(必填;发布脚本会按已有包自动 +1 补丁号)。
.PARAMETER VersionCode
Android versionCode(默认由 x.y.z 换算:major*10000 + minor*100 + patch)。
.PARAMETER ServerBaseUrl
覆盖该环境配置里的域名(生成下载地址用,APK 本体不写死域名)。
.PARAMETER Keep
保留多少个历史安装包(默认读 package.config.json 的 keepVersions = 5)。
.PARAMETER ReleaseNotes
写进 version.json 的更新说明。
.EXAMPLE
.\build.ps1 -Environment Production -Version 1.0.1 -ReleaseNotes "扫码上报更稳"
.NOTES
前置:JDK 17、Android SDK(AndroidStation\local.properties 的 sdk.dir 或 ANDROID_HOME)、
AndroidStation\keystore.properties + keystore\*.jks(正式签名)。
本文件必须保存为 UTF-8 with BOM —— PowerShell 5.1 读无 BOM 脚本会按 GBK 解析,中文注释直接语法报错。
#>
param(
[ValidateSet('Development', 'Production')]
[string]$Environment = 'Development',
[string]$Version,
[int]$VersionCode = 0,
[string]$ServerBaseUrl,
[int]$Keep = 0,
[string]$ReleaseNotes = ''
)
$ErrorActionPreference = 'Stop'
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 }
$projectDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.projectDir))
$outDir = Join-Path $PSScriptRoot 'out'
$gradlew = Join-Path $projectDir 'gradlew.bat'
if (-not (Test-Path $gradlew)) { throw "找不到 gradlew.bat:$gradlew(检查 package.config.json 的 projectDir)" }
# ---------------- 2. 版本 ----------------
if (-not $Version) { throw "请用 -Version x.y.z 指定版本号(npm run android:publish 会自动带上)" }
$Version = $Version.Trim().TrimStart('v', 'V')
$null = [version]$Version # 提前校验格式(不合法直接报错)
$parts = $Version.Split('.')
$major = [int]$parts[0]
$minor = 0
if ($parts.Length -gt 1) { $minor = [int]$parts[1] }
$patch = 0
if ($parts.Length -gt 2) { $patch = [int]$parts[2] }
if ($VersionCode -le 0) { $VersionCode = $major * 10000 + $minor * 100 + $patch }
Write-Host "环境:$Environment"
Write-Host "版本:$Version(versionCode $VersionCode)"
Write-Host "工程:$projectDir"
# ---------------- 3. 环境检查 ----------------
$keystoreProps = Join-Path $projectDir 'keystore.properties'
if (-not (Test-Path $keystoreProps)) {
throw "缺少正式签名配置:$keystoreProps`n(keystore.properties 与 keystore/*.jks 不入库,发布机需自行备份)"
}
$localProps = Join-Path $projectDir 'local.properties'
if (-not (Test-Path $localProps) -and -not $env:ANDROID_HOME -and -not $env:ANDROID_SDK_ROOT) {
throw "找不到 Android SDK:请写 $localProps 的 sdk.dir 或设置 ANDROID_HOME"
}
$javaHome = $env:JAVA_HOME
if (-not $javaHome -or -not (Test-Path (Join-Path $javaHome 'bin\java.exe'))) {
$roots = @(
'C:\Program Files\Android\Android Studio\jbr',
(Join-Path $env:LOCALAPPDATA 'Programs\EclipseAdoptium'),
'C:\Program Files\Eclipse Adoptium',
'C:\Program Files\Java'
)
$javaHome = $null
foreach ($root in $roots) {
if (-not (Test-Path $root)) { continue }
if (Test-Path (Join-Path $root 'bin\java.exe')) { $javaHome = $root; break }
$child = Get-ChildItem -Path $root -Directory -ErrorAction SilentlyContinue |
Where-Object { Test-Path (Join-Path $_.FullName 'bin\java.exe') } |
Sort-Object Name -Descending | Select-Object -First 1
if ($child) { $javaHome = $child.FullName; break }
}
}
if (-not $javaHome) { throw '找不到 JDK:请安装 JDK 17 并设置 JAVA_HOME。' }
$env:JAVA_HOME = $javaHome
Write-Host "JAVA_HOME=$javaHome"
# ---------------- 4. 构建 APK ----------------
Push-Location $projectDir
try {
# 2>&1 | Write-Host:gradle 会把 javac 的提示写到 stderr(例如「使用了已过时的 API」),
# 直接调用时 PowerShell 会把它当错误记录,配合 $ErrorActionPreference='Stop' 会误判构建失败。
& $gradlew --no-daemon assembleRelease "-PversionName=$Version" "-PversionCode=$VersionCode" 2>&1 |
ForEach-Object { Write-Host $_ }
if ($LASTEXITCODE -ne 0) { throw "gradlew assembleRelease 失败($LASTEXITCODE)" }
}
finally {
Pop-Location
}
$builtApk = Join-Path $projectDir 'app\build\outputs\apk\release\app-release.apk'
if (-not (Test-Path $builtApk)) { throw "构建产物不存在:$builtApk" }
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$apkName = [string]$envCfg.apkName
if (-not $apkName) { $apkName = 'YuncangScan-{version}.apk' }
$apkName = $apkName.Replace('{version}', $Version)
$apk = Join-Path $outDir $apkName
Copy-Item -Path $builtApk -Destination $apk -Force
# ---------------- 5. version.json ----------------
$hash = (Get-FileHash -Algorithm MD5 -Path $apk).Hash.ToLowerInvariant()
$size = (Get-Item $apk).Length
$downloadUrl = "$ServerBaseUrl/download/android_client/$apkName"
$now = Get-Date
$versionJson = [ordered]@{
version = $Version
versionCode = $VersionCode
package = 'com.gztbyc.scancenter'
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 "APK:$apk($([math]::Round($size / 1MB, 1)) MB)"
Write-Host "MD5:$hash"
Write-Host "下载地址:$downloadUrl"
# ---------------- 6. 复制到后端 ----------------
$skipVersionJson = $false
$downloadDir = $null
$versionDir = $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 降回去
$existingVj = Join-Path $versionDir 'version.json'
if (Test-Path $existingVj) {
try {
$old = (Read-Utf8 $existingVj) | ConvertFrom-Json
if ($old.version -and ([version]$old.version -gt [version]$Version)) {
$skipVersionJson = $true
Write-Warning "后端 version.json 已是更新版本($($old.version)),本次不覆盖它"
}
}
catch { /* 旧文件坏了就当没有 */ }
}
$dstApk = Join-Path $downloadDir $apkName
Copy-Item -Path $apk -Destination $dstApk -Force
# 刷新 mtime:即使上游还有一层协商缓存,也能认出文件变了
(Get-Item $dstApk).LastWriteTime = $now
if (-not $skipVersionJson) {
Write-Utf8NoBom $existingVj $versionJson
(Get-Item $existingVj).LastWriteTime = $now
}
Write-Host "已复制到:$downloadDir(APK)、$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 'YuncangScan-*.apk' -File | ForEach-Object {
if ($_.BaseName -match 'YuncangScan-(\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 "完成:$apk" -ForegroundColor Green
Write-Host "版本文件:$versionFile"
Write-Host "下载地址:$downloadUrl"
Write-Host "安装:adb install -r `"$apk`"(设备上无需任何特殊权限)"