Files
cmroubao/init.ps1
T

121 lines
3.8 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"
$backendRoot = Join-Path $PSScriptRoot "backend-api"
$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"
}
if (-not (Test-Path -LiteralPath (Join-Path $backendRoot "go.mod"))) {
throw "缺少 Go 后端工程: $backendRoot"
}
if (-not (Get-Command go -ErrorAction SilentlyContinue)) {
throw "缺少 Go 1.23.0,go 不在 PATH 中。"
}
$goVersion = (& go version) -join "`n"
if ($goVersion -notmatch '\bgo1\.23\.0\b') {
throw "需要 Go 1.23.0,当前 go version 输出: $goVersion"
}
if (-not (Get-Command gcc -ErrorAction SilentlyContinue)) {
throw "SQLite CGO 构建需要 GCC,gcc 不在 PATH 中。"
}
$env:ANDROID_HOME = $sdkRoot
$env:ANDROID_SDK_ROOT = $sdkRoot
$env:GOTOOLCHAIN = "local"
if ((& go env CGO_ENABLED) -ne "1") {
throw "SQLite 构建需要 CGO_ENABLED=1。"
}
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"
Write-Host "==> 验证 Go 后端"
Push-Location $backendRoot
try {
& go test ./...
if ($LASTEXITCODE -ne 0) {
throw "Go 后端测试失败,退出码: $LASTEXITCODE"
}
& go vet ./...
if ($LASTEXITCODE -ne 0) {
throw "Go 后端 vet 失败,退出码: $LASTEXITCODE"
}
$unformatted = @(& gofmt -l cmd internal migrations)
if ($LASTEXITCODE -ne 0 -or $unformatted.Count -ne 0) {
throw "Go 后端存在未格式化文件: $($unformatted -join ', ')"
}
New-Item -ItemType Directory -Force "bin" | Out-Null
& go build -o "bin/cmroubao-api.exe" ./cmd/api
if ($LASTEXITCODE -ne 0) {
throw "Go API 构建失败,退出码: $LASTEXITCODE"
}
& go build -o "bin/cmroubao-migrate.exe" ./cmd/migrate
if ($LASTEXITCODE -ne 0) {
throw "Go migration 构建失败,退出码: $LASTEXITCODE"
}
& go build -o "bin/cmroubao-authctl.exe" ./cmd/authctl
if ($LASTEXITCODE -ne 0) {
throw "Go authctl 构建失败,退出码: $LASTEXITCODE"
}
} finally {
Pop-Location
}
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"
}
}