200 lines
11 KiB
Batchfile
200 lines
11 KiB
Batchfile
@echo off
|
||
setlocal
|
||
set "NESLITE_PACKAGE_FILE=%~f0"
|
||
where pwsh.exe >nul 2>nul
|
||
if errorlevel 1 (
|
||
echo PowerShell 7 is required. Install it and run this file again.
|
||
pause
|
||
exit /b 1
|
||
)
|
||
pwsh.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$text = [IO.File]::ReadAllText($env:NESLITE_PACKAGE_FILE); $body = ($text -split '(?m)^# NESLITE_POWERSHELL\r?$', 2)[1]; & ([scriptblock]::Create($body)) %*"
|
||
exit /b %errorlevel%
|
||
# NESLITE_POWERSHELL
|
||
param(
|
||
[ValidateSet('debug', 'release')]
|
||
[string]$Variant = 'debug',
|
||
[switch]$NoPause
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$PSNativeCommandUseErrorActionPreference = $false
|
||
$root = Split-Path -Parent $env:NESLITE_PACKAGE_FILE
|
||
$buildRoot = Join-Path $root 'build'
|
||
$logPath = Join-Path $buildRoot "logs\package-$Variant.log"
|
||
$exitCode = 1
|
||
$locationPushed = $false
|
||
|
||
try {
|
||
foreach ($directory in @('logs', 'tmp', 'cache\gradle', 'releases', 'reports')) {
|
||
$null = New-Item -ItemType Directory -Path (Join-Path $buildRoot $directory) -Force
|
||
}
|
||
"NESLite $Variant packaging - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" |
|
||
Set-Content -LiteralPath $logPath -Encoding utf8NoBOM
|
||
Push-Location -LiteralPath $root
|
||
$locationPushed = $true
|
||
|
||
$bundledJdk = Join-Path $buildRoot 'toolchains\jdk-17.0.17+10'
|
||
if (Test-Path -LiteralPath (Join-Path $bundledJdk 'bin\java.exe')) { $env:JAVA_HOME = $bundledJdk }
|
||
if (-not $env:JAVA_HOME -or -not (Test-Path -LiteralPath (Join-Path $env:JAVA_HOME 'bin\java.exe'))) {
|
||
throw '未找到 JDK 17。请设置 JAVA_HOME,或将 JDK 放入 build\toolchains\jdk-17.0.17+10。'
|
||
}
|
||
$java = Join-Path $env:JAVA_HOME 'bin\java.exe'
|
||
$javaVersion = (& $java -version 2>&1 | Out-String)
|
||
if ($LASTEXITCODE -ne 0 -or $javaVersion -notmatch 'version "17\.') {
|
||
throw "构建需要 JDK 17,当前 JAVA_HOME 为 $env:JAVA_HOME。"
|
||
}
|
||
|
||
$sdkRoot = $null
|
||
$properties = Join-Path $root 'local.properties'
|
||
if (Test-Path -LiteralPath $properties) {
|
||
foreach ($line in Get-Content -LiteralPath $properties) {
|
||
if ($line -match '^\s*sdk\.dir\s*=\s*(.+?)\s*$') {
|
||
$sdkRoot = $Matches[1].Replace('\:', ':').Replace('\\', '\').Replace('\ ', ' ')
|
||
}
|
||
}
|
||
}
|
||
if (-not $sdkRoot) { $sdkRoot = $env:ANDROID_HOME }
|
||
if (-not $sdkRoot) { $sdkRoot = $env:ANDROID_SDK_ROOT }
|
||
if (-not $sdkRoot) { $sdkRoot = Join-Path $env:LOCALAPPDATA 'Android\Sdk' }
|
||
foreach ($component in @('platforms\android-36.1', 'build-tools\36.1.0',
|
||
'ndk\28.2.13676358', 'cmake\3.22.1')) {
|
||
if (-not (Test-Path -LiteralPath (Join-Path $sdkRoot $component))) {
|
||
throw "Android 构建组件缺失:$sdkRoot\$component。安装方法见 docs\toolchain.md。"
|
||
}
|
||
}
|
||
$env:ANDROID_HOME = $sdkRoot
|
||
$env:ANDROID_SDK_ROOT = $sdkRoot
|
||
$env:TEMP = Join-Path $buildRoot 'tmp'
|
||
$env:TMP = $env:TEMP
|
||
$env:JAVA_TOOL_OPTIONS = ($env:JAVA_TOOL_OPTIONS +
|
||
" -Djava.io.tmpdir=`"$env:TEMP`" -Djdk.net.unixdomain.tmpdir=`"$env:TEMP`"").Trim()
|
||
|
||
if ($Variant -eq 'release') {
|
||
$signingDirectory = if ($env:NESLITE_SIGNING_DIRECTORY) { $env:NESLITE_SIGNING_DIRECTORY } else {
|
||
Join-Path $env:USERPROFILE 'NESLite-private\signing'
|
||
}
|
||
$keystore = Join-Path $signingDirectory 'neslite-release.p12'
|
||
$passwordFile = Join-Path $signingDirectory 'password.dpapi'
|
||
if (-not (Test-Path -LiteralPath $keystore) -or -not (Test-Path -LiteralPath $passwordFile)) {
|
||
throw '未找到正式签名密钥及加密密码。请配置项目外的 NESLITE_SIGNING_DIRECTORY;不会自动更换已有签名。'
|
||
}
|
||
$sourceStatus = @(& git -C $root status --porcelain)
|
||
if ($LASTEXITCODE -ne 0 -or $sourceStatus.Count -ne 0) { throw '正式打包前请提交源码,确保 APK 和源码包对应同一提交。' }
|
||
$sourceCommit = (& git -C $root rev-parse HEAD).Trim()
|
||
if ($LASTEXITCODE -ne 0) { throw '无法取得源码提交。' }
|
||
}
|
||
|
||
Write-Host '正在检查固定依赖并编译 APK;本入口不自动运行测试。'
|
||
& (Join-Path $root 'tools\fetch-third-party.ps1') | Tee-Object -FilePath $logPath -Append
|
||
$task = if ($Variant -eq 'debug') { ':app:assembleDebug' } else { ':app:assembleRelease' }
|
||
& $java '-Xmx64m' '-Xms64m' '-classpath' (Join-Path $root 'gradle\wrapper\gradle-wrapper.jar') `
|
||
org.gradle.wrapper.GradleWrapperMain '--project-cache-dir' (Join-Path $buildRoot 'cache\gradle') `
|
||
'--console=plain' '--no-daemon' '-Pkotlin.compiler.execution.strategy=in-process' $task 2>&1 |
|
||
Tee-Object -FilePath $logPath -Append
|
||
if ($LASTEXITCODE -ne 0) { throw "APK 编译失败(退出码 $LASTEXITCODE)。" }
|
||
|
||
$apkDirectory = Join-Path $buildRoot "app\outputs\apk\$Variant"
|
||
$metadata = Get-Content -LiteralPath (Join-Path $apkDirectory 'output-metadata.json') -Raw | ConvertFrom-Json
|
||
if (@($metadata.elements).Count -ne 1) { throw '预期单个 arm64 APK,构建产物数量不符。' }
|
||
$artifact = $metadata.elements[0]
|
||
$builtApk = Join-Path $apkDirectory $artifact.outputFile
|
||
if (-not (Test-Path -LiteralPath $builtApk)) { throw "APK 产物不存在:$builtApk" }
|
||
$releaseFiles = @()
|
||
if ($Variant -eq 'release') {
|
||
$signer = Join-Path $sdkRoot 'build-tools\36.1.0\lib\apksigner.jar'
|
||
$signedApk = Join-Path $buildRoot "tmp\NESLite-$($artifact.versionName)-signed.apk"
|
||
$securePassword = Get-Content -LiteralPath $passwordFile -Raw | ConvertTo-SecureString
|
||
$env:NESLITE_KEYSTORE_PASSWORD = [Net.NetworkCredential]::new('', $securePassword).Password
|
||
try {
|
||
& $java '-jar' $signer sign --ks $keystore --ks-key-alias neslite-release `
|
||
--ks-pass env:NESLITE_KEYSTORE_PASSWORD --key-pass env:NESLITE_KEYSTORE_PASSWORD `
|
||
--v4-signing-enabled false --out $signedApk $builtApk 2>&1 |
|
||
Tee-Object -FilePath $logPath -Append
|
||
if ($LASTEXITCODE -ne 0) { throw '正式 APK 签名失败。' }
|
||
} finally {
|
||
Remove-Item Env:NESLITE_KEYSTORE_PASSWORD -ErrorAction SilentlyContinue
|
||
$securePassword = $null
|
||
}
|
||
$signatureReport = @(& $java '-jar' $signer verify --verbose --print-certs $signedApk 2>&1)
|
||
if ($LASTEXITCODE -ne 0) { throw '正式 APK 签名验证失败。' }
|
||
$signatureReport | Set-Content -LiteralPath (Join-Path $buildRoot 'reports\release-signature.txt') -Encoding utf8NoBOM
|
||
$builtApk = $signedApk
|
||
}
|
||
$auditPath = Join-Path $buildRoot "reports\package-$Variant-native.json"
|
||
& (Join-Path $root 'tools\check-rc-native.ps1') -ApkPath $builtApk -OutputPath $auditPath |
|
||
Tee-Object -FilePath $logPath -Append | Out-Null
|
||
|
||
$suffix = '-arm64-v8a'
|
||
$name = "NESLite-$($artifact.versionName)$suffix.apk"
|
||
$destination = Join-Path $buildRoot "releases\$name"
|
||
if ($Variant -eq 'release') {
|
||
$symbols = Join-Path $buildRoot 'app\outputs\native-debug-symbols\release\native-debug-symbols.zip'
|
||
if (-not (Test-Path -LiteralPath $symbols)) { throw 'Release 原生调试符号缺失。' }
|
||
Copy-Item -LiteralPath $symbols -Destination (Join-Path $buildRoot "releases\NESLite-$($artifact.versionName)-native-debug-symbols.zip") -Force
|
||
$releaseFiles += "NESLite-$($artifact.versionName)-native-debug-symbols.zip"
|
||
$sourceName = "NESLite-$($artifact.versionName)-source.zip"
|
||
& git -C $root archive --format=zip "--prefix=NESLite-$($artifact.versionName)/" `
|
||
"--output=$(Join-Path $buildRoot "releases\$sourceName")" $sourceCommit
|
||
if ($LASTEXITCODE -ne 0) { throw '应用源码归档失败。' }
|
||
$releaseFiles += $sourceName
|
||
$dependencies = Get-Content -LiteralPath (Join-Path $root 'third_party\manifest.json') -Raw | ConvertFrom-Json
|
||
foreach ($dependency in $dependencies.sources) {
|
||
$dependencyDirectory = Join-Path $root "third_party\sources\$($dependency.directory)"
|
||
$actualCommit = (& git -C $dependencyDirectory rev-parse HEAD).Trim()
|
||
$dependencyStatus = @(& git -C $dependencyDirectory status --porcelain)
|
||
if ($LASTEXITCODE -ne 0 -or $actualCommit -ne $dependency.commit -or $dependencyStatus.Count -ne 0) {
|
||
throw "依赖源码与锁定版本不一致:$($dependency.directory)"
|
||
}
|
||
$dependencyName = "$($dependency.directory)-$($dependency.commit.Substring(0, 12))-source.zip"
|
||
$archiveArguments = @('-C', $dependencyDirectory, 'archive', '--format=zip',
|
||
"--prefix=$($dependency.directory)/", "--output=$(Join-Path $buildRoot "releases\$dependencyName")", $dependency.commit)
|
||
if ($dependency.directory -eq 'libretro-headers') { $archiveArguments += 'libretro-common/include/libretro.h' }
|
||
& git @archiveArguments
|
||
if ($LASTEXITCODE -ne 0) { throw "依赖源码归档失败:$($dependency.directory)" }
|
||
$releaseFiles += $dependencyName
|
||
}
|
||
}
|
||
Copy-Item -LiteralPath $builtApk -Destination $destination -Force
|
||
$hash = (Get-FileHash -LiteralPath $destination -Algorithm SHA256).Hash
|
||
"$hash $name" | Set-Content -LiteralPath "$destination.sha256" -Encoding ascii
|
||
$releaseFiles += $name, "$name.sha256"
|
||
foreach ($material in @('LICENSE', 'THIRD_PARTY_NOTICES.md', 'third_party\manifest.json')) {
|
||
Copy-Item -LiteralPath (Join-Path $root $material) -Destination (Join-Path $buildRoot 'releases') -Force
|
||
$releaseFiles += Split-Path -Leaf $material
|
||
}
|
||
if ($Variant -eq 'release') {
|
||
$notesName = "NESLite-$($artifact.versionName)-release-notes.md"
|
||
Copy-Item -LiteralPath (Join-Path $root "docs\releases\$($artifact.versionName).md") `
|
||
-Destination (Join-Path $buildRoot "releases\$notesName") -Force
|
||
$releaseFiles += $notesName
|
||
$manifestName = "NESLite-$($artifact.versionName)-release.json"
|
||
[ordered]@{ versionName=$artifact.versionName; versionCode=$artifact.versionCode;
|
||
applicationId=$metadata.applicationId; sourceCommit=$sourceCommit; abi='arm64-v8a'; signed=$true;
|
||
apk=$name; apkSHA256=$hash; dependencies=$dependencies.sources; assets=$releaseFiles } |
|
||
ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $buildRoot "releases\$manifestName") -Encoding utf8NoBOM
|
||
$releaseFiles += $manifestName
|
||
$checksums = foreach ($releaseFile in $releaseFiles) {
|
||
"{0} {1}" -f (Get-FileHash -LiteralPath (Join-Path $buildRoot "releases\$releaseFile") -Algorithm SHA256).Hash, $releaseFile
|
||
}
|
||
$checksums | Set-Content -LiteralPath (Join-Path $buildRoot "releases\NESLite-$($artifact.versionName)-SHA256SUMS.txt") -Encoding ascii
|
||
}
|
||
"APK: $destination`nSHA256: $hash" | Add-Content -LiteralPath $logPath -Encoding utf8NoBOM
|
||
Write-Host "`n打包完成:$destination" -ForegroundColor Green
|
||
Write-Host "构建日志:$logPath"
|
||
if ($Variant -eq 'release') {
|
||
Write-Host '正式 APK 已签名并验证,源码、符号及发布材料已输出。'
|
||
} else {
|
||
Write-Host '这是可直接安装的调试版 APK。'
|
||
}
|
||
$exitCode = 0
|
||
} catch {
|
||
$message = $_.Exception.Message
|
||
Write-Host "`n打包失败:$message" -ForegroundColor Red
|
||
if (Test-Path -LiteralPath $logPath) { Add-Content -LiteralPath $logPath -Value "FAILED: $message" }
|
||
Write-Host "请检查日志:$logPath"
|
||
} finally {
|
||
if ($locationPushed) { Pop-Location }
|
||
}
|
||
if (-not $NoPause) { $null = Read-Host '按回车键关闭窗口' }
|
||
exit $exitCode
|