build-acceptance-apk.ps1 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. param(
  2. [string]$EvidenceDirectory = '',
  3. [string]$Tag = (Get-Date -Format 'yyyyMMdd-HHmmss'),
  4. [switch]$NativeOnly
  5. )
  6. $ErrorActionPreference = 'Stop'
  7. $projectRoot = Split-Path -Parent $PSScriptRoot
  8. if ($Tag -notmatch '^[A-Za-z0-9-]+$') { throw '构建标签只能包含字母、数字和短横线' }
  9. $task = Get-Content -LiteralPath (Join-Path $projectRoot 'docs/00-tracking/active-task.json') -Raw -Encoding UTF8 | ConvertFrom-Json
  10. if (!$EvidenceDirectory) { $EvidenceDirectory = Split-Path -Parent $task.ledger }
  11. $evidenceRoot = [System.IO.Path]::GetFullPath((Join-Path $projectRoot $EvidenceDirectory))
  12. if (!$evidenceRoot.StartsWith($projectRoot + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) {
  13. throw '构建证据目录必须位于当前项目内'
  14. }
  15. if (!(Test-Path -LiteralPath $evidenceRoot -PathType Container)) { throw '先建立本轮验收账本和证据目录' }
  16. $sdk = $task.environment.androidSdk
  17. if (!$sdk -or !(Test-Path -LiteralPath (Join-Path $sdk 'platforms/android-36/android.jar'))) {
  18. throw 'Android SDK 不可用;先只读核对 active-task.json 的 environment.androidSdk,禁止把旧 APK 当新产物'
  19. }
  20. $nativeLog = Join-Path $evidenceRoot "android-gradle-$Tag.log"
  21. $webLog = Join-Path $evidenceRoot "android-cap-sync-$Tag.log"
  22. $receiptFile = Join-Path $evidenceRoot "apk-$Tag-receipt.json"
  23. foreach ($file in @($nativeLog, $receiptFile) + $(if (!$NativeOnly) { @($webLog) } else { @() })) {
  24. if (Test-Path -LiteralPath $file) { throw "构建证据已存在,先核实结果,不覆盖:$file" }
  25. }
  26. $oldAndroidHome = $env:ANDROID_HOME
  27. $oldAndroidSdkRoot = $env:ANDROID_SDK_ROOT
  28. function Invoke-LoggedCommand([string]$Executable, [string[]]$CommandArguments, [string]$LogFile) {
  29. $nativeCommand = Get-Command -Name $Executable -CommandType Application -ErrorAction Stop
  30. $previousPreference = $ErrorActionPreference
  31. try {
  32. # Windows PowerShell 把原生命令 stderr 包装为错误记录;仍以真实退出码判断结果。
  33. $ErrorActionPreference = 'Continue'
  34. $global:LASTEXITCODE = $null
  35. & $nativeCommand.Source @CommandArguments *> $LogFile
  36. $commandExit = $LASTEXITCODE
  37. } finally { $ErrorActionPreference = $previousPreference }
  38. if ($null -eq $commandExit) { throw "构建命令未能启动,不能使用旧产物:$Executable" }
  39. if ($commandExit -ne 0) { throw "构建命令失败(exit=$commandExit):$LogFile" }
  40. }
  41. Push-Location $projectRoot
  42. try {
  43. $env:ANDROID_HOME = $sdk
  44. $env:ANDROID_SDK_ROOT = $sdk
  45. if (!$NativeOnly) {
  46. Invoke-LoggedCommand -Executable 'npm.cmd' -CommandArguments @('run', 'cap:sync') -LogFile $webLog
  47. }
  48. Invoke-LoggedCommand -Executable '.\android\gradlew.bat' -CommandArguments @('-p', 'android', 'assembleDebug', '--console=plain') -LogFile $nativeLog
  49. $apk = Get-Item -LiteralPath 'android/app/build/outputs/apk/debug/app-debug.apk'
  50. $sha = [System.Security.Cryptography.SHA256]::Create()
  51. try { $apkHash = [BitConverter]::ToString($sha.ComputeHash([System.IO.File]::ReadAllBytes($apk.FullName))).Replace('-', '') }
  52. finally { $sha.Dispose() }
  53. $receipt = [ordered]@{
  54. at = [DateTimeOffset]::UtcNow.ToString('o')
  55. apk = $apk.FullName
  56. sha256 = $apkHash
  57. bytes = $apk.Length
  58. sdk = $sdk
  59. nativeLog = $nativeLog
  60. webBuildAndSync = !$NativeOnly.IsPresent
  61. webLog = $(if (!$NativeOnly) { $webLog } else { $null })
  62. }
  63. [System.IO.File]::WriteAllText($receiptFile, ($receipt | ConvertTo-Json), [System.Text.UTF8Encoding]::new($false))
  64. $receipt | ConvertTo-Json
  65. } finally {
  66. Pop-Location
  67. $env:ANDROID_HOME = $oldAndroidHome
  68. $env:ANDROID_SDK_ROOT = $oldAndroidSdkRoot
  69. }