Files
cmroubao/init.ps1
T

71 lines
2.1 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.
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
Set-Location -Path $PSScriptRoot
$sdkRoot = if ($env:ANDROID_SDK_ROOT) {
$env:ANDROID_SDK_ROOT
} elseif ($env:ANDROID_HOME) {
$env:ANDROID_HOME
} else {
Join-Path $env:LOCALAPPDATA "Android\Sdk"
}
$gradle = Join-Path $PSScriptRoot "android-buyer\gradlew.bat"
$adb = Join-Path $sdkRoot "platform-tools\adb.exe"
$apk = Join-Path $PSScriptRoot "android-buyer\app\build\outputs\apk\debug\app-debug.apk"
if (-not (Test-Path -LiteralPath $gradle)) {
throw "缺少 Android Gradle Wrapper: $gradle"
}
if (-not (Test-Path -LiteralPath (Join-Path $sdkRoot "platforms\android-34"))) {
throw "缺少 Android SDK 34,请设置 ANDROID_SDK_ROOT 或 ANDROID_HOME。"
}
if (-not (Test-Path -LiteralPath (Join-Path $sdkRoot "build-tools\34.0.0"))) {
throw "缺少 Android SDK Build Tools 34.0.0。"
}
if (-not (Get-Command java -ErrorAction SilentlyContinue)) {
throw "缺少 JDK 17,java 不在 PATH 中。"
}
$javaVersion = (& java -version 2>&1) -join "`n"
if ($javaVersion -notmatch 'version "17(?:\.|")') {
throw "需要 JDK 17,当前 java -version 输出: $javaVersion"
}
$env:ANDROID_HOME = $sdkRoot
$env:ANDROID_SDK_ROOT = $sdkRoot
Write-Host "==> 验证 Android 工程"
Push-Location (Join-Path $PSScriptRoot "android-buyer")
try {
& $gradle test assembleDebug --no-daemon
if ($LASTEXITCODE -ne 0) {
throw "Android 构建失败,退出码: $LASTEXITCODE"
}
} finally {
Pop-Location
}
Write-Host "==> Debug APK: $apk"
if ($env:RUN_START_COMMAND -eq "1") {
if (-not (Test-Path -LiteralPath $adb)) {
throw "缺少 SDK Platform Tools: $adb"
}
$devices = & $adb devices
if (($devices -join "`n") -notmatch "\sdevice\s*$") {
throw "没有已授权的 Android 设备。"
}
Write-Host "==> 安装并启动 Debug APK"
& $adb install -r -t $apk
if ($LASTEXITCODE -ne 0) {
throw "APK 安装失败,退出码: $LASTEXITCODE"
}
& $adb shell am start -W -n "com.roubao.autopilot/.MainActivity"
if ($LASTEXITCODE -ne 0) {
throw "App 启动失败,退出码: $LASTEXITCODE"
}
}