#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ANDROID_DIR="$ROOT_DIR/android-buyer" BACKEND_DIR="$ROOT_DIR/backend-api" SDK_ROOT="${ANDROID_SDK_ROOT:-${ANDROID_HOME:-$HOME/Android/Sdk}}" GRADLE="$ANDROID_DIR/gradlew" ADB="$SDK_ROOT/platform-tools/adb" APK="$ANDROID_DIR/app/build/outputs/apk/debug/app-debug.apk" if [[ ! -x "$GRADLE" ]]; then echo "ERROR: Android Gradle Wrapper is missing or not executable: $GRADLE" >&2 exit 2 fi if [[ ! -d "$SDK_ROOT/platforms/android-34" ]]; then echo "ERROR: Android SDK 34 is missing; set ANDROID_SDK_ROOT or ANDROID_HOME." >&2 exit 2 fi if [[ ! -d "$SDK_ROOT/build-tools/34.0.0" ]]; then echo "ERROR: Android SDK Build Tools 34.0.0 is missing." >&2 exit 2 fi if ! command -v java >/dev/null 2>&1; then echo "ERROR: JDK 17 is missing from PATH." >&2 exit 2 fi JAVA_VERSION="$(java -version 2>&1 | head -n 1)" if [[ "$JAVA_VERSION" != *'version "17.'* ]]; then echo "ERROR: JDK 17 is required; found: $JAVA_VERSION" >&2 exit 2 fi if [[ ! -f "$BACKEND_DIR/go.mod" ]]; then echo "ERROR: Go backend project is missing: $BACKEND_DIR" >&2 exit 2 fi if ! command -v go >/dev/null 2>&1; then echo "ERROR: Go 1.23.0 is missing from PATH." >&2 exit 2 fi GO_VERSION="$(go version)" if [[ ! "$GO_VERSION" =~ ^go\ version\ go1\.23\.0[[:space:]] ]]; then echo "ERROR: Go 1.23.0 is required; found: $GO_VERSION" >&2 exit 2 fi if ! command -v gcc >/dev/null 2>&1; then echo "ERROR: GCC is required for the SQLite CGO build." >&2 exit 2 fi export ANDROID_HOME="$SDK_ROOT" export ANDROID_SDK_ROOT="$SDK_ROOT" export GOTOOLCHAIN=local if [[ "$(go env CGO_ENABLED)" != "1" ]]; then echo "ERROR: CGO_ENABLED=1 is required for SQLite." >&2 exit 2 fi echo "==> Verifying Android project" ( cd "$ANDROID_DIR" "$GRADLE" test assembleDebug --no-daemon ) echo "==> Debug APK: $APK" echo "==> Verifying Go backend" ( cd "$BACKEND_DIR" go test ./... go vet ./... UNFORMATTED="$(gofmt -l cmd internal migrations)" if [[ -n "$UNFORMATTED" ]]; then echo "ERROR: Go files need gofmt:" >&2 echo "$UNFORMATTED" >&2 exit 2 fi mkdir -p bin go build -o bin/cmroubao-api ./cmd/api go build -o bin/cmroubao-migrate ./cmd/migrate ) if [[ "${RUN_START_COMMAND:-0}" == "1" ]]; then if [[ ! -x "$ADB" ]]; then echo "ERROR: Android Platform Tools are missing: $ADB" >&2 exit 2 fi if ! "$ADB" devices | grep -qE $'\tdevice$'; then echo "ERROR: No authorized Android device is connected." >&2 exit 2 fi echo "==> Installing and starting Debug APK" "$ADB" install -r -t "$APK" "$ADB" shell am start -W -n "com.roubao.autopilot/.MainActivity" fi