param( [string]$EvidenceDirectory = '', [string]$Tag = (Get-Date -Format 'yyyyMMdd-HHmmss'), [switch]$NativeOnly ) $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $PSScriptRoot if ($Tag -notmatch '^[A-Za-z0-9-]+$') { throw '构建标签只能包含字母、数字和短横线' } $task = Get-Content -LiteralPath (Join-Path $projectRoot 'docs/00-tracking/active-task.json') -Raw -Encoding UTF8 | ConvertFrom-Json if (!$EvidenceDirectory) { $EvidenceDirectory = Split-Path -Parent $task.ledger } $evidenceRoot = [System.IO.Path]::GetFullPath((Join-Path $projectRoot $EvidenceDirectory)) if (!$evidenceRoot.StartsWith($projectRoot + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase)) { throw '构建证据目录必须位于当前项目内' } if (!(Test-Path -LiteralPath $evidenceRoot -PathType Container)) { throw '先建立本轮验收账本和证据目录' } $sdk = $task.environment.androidSdk if (!$sdk -or !(Test-Path -LiteralPath (Join-Path $sdk 'platforms/android-36/android.jar'))) { throw 'Android SDK 不可用;先只读核对 active-task.json 的 environment.androidSdk,禁止把旧 APK 当新产物' } $nativeLog = Join-Path $evidenceRoot "android-gradle-$Tag.log" $webLog = Join-Path $evidenceRoot "android-cap-sync-$Tag.log" $receiptFile = Join-Path $evidenceRoot "apk-$Tag-receipt.json" foreach ($file in @($nativeLog, $receiptFile) + $(if (!$NativeOnly) { @($webLog) } else { @() })) { if (Test-Path -LiteralPath $file) { throw "构建证据已存在,先核实结果,不覆盖:$file" } } $oldAndroidHome = $env:ANDROID_HOME $oldAndroidSdkRoot = $env:ANDROID_SDK_ROOT function Invoke-LoggedCommand([string]$Executable, [string[]]$CommandArguments, [string]$LogFile) { $nativeCommand = Get-Command -Name $Executable -CommandType Application -ErrorAction Stop $previousPreference = $ErrorActionPreference try { # Windows PowerShell 把原生命令 stderr 包装为错误记录;仍以真实退出码判断结果。 $ErrorActionPreference = 'Continue' $global:LASTEXITCODE = $null & $nativeCommand.Source @CommandArguments *> $LogFile $commandExit = $LASTEXITCODE } finally { $ErrorActionPreference = $previousPreference } if ($null -eq $commandExit) { throw "构建命令未能启动,不能使用旧产物:$Executable" } if ($commandExit -ne 0) { throw "构建命令失败(exit=$commandExit):$LogFile" } } Push-Location $projectRoot try { $env:ANDROID_HOME = $sdk $env:ANDROID_SDK_ROOT = $sdk if (!$NativeOnly) { Invoke-LoggedCommand -Executable 'npm.cmd' -CommandArguments @('run', 'cap:sync') -LogFile $webLog } Invoke-LoggedCommand -Executable '.\android\gradlew.bat' -CommandArguments @('-p', 'android', 'assembleDebug', '--console=plain') -LogFile $nativeLog $apk = Get-Item -LiteralPath 'android/app/build/outputs/apk/debug/app-debug.apk' $sha = [System.Security.Cryptography.SHA256]::Create() try { $apkHash = [BitConverter]::ToString($sha.ComputeHash([System.IO.File]::ReadAllBytes($apk.FullName))).Replace('-', '') } finally { $sha.Dispose() } $receipt = [ordered]@{ at = [DateTimeOffset]::UtcNow.ToString('o') apk = $apk.FullName sha256 = $apkHash bytes = $apk.Length sdk = $sdk nativeLog = $nativeLog webBuildAndSync = !$NativeOnly.IsPresent webLog = $(if (!$NativeOnly) { $webLog } else { $null }) } [System.IO.File]::WriteAllText($receiptFile, ($receipt | ConvertTo-Json), [System.Text.UTF8Encoding]::new($false)) $receipt | ConvertTo-Json } finally { Pop-Location $env:ANDROID_HOME = $oldAndroidHome $env:ANDROID_SDK_ROOT = $oldAndroidSdkRoot }