#!/bin/bash set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load .env file if [ -f "$PROJECT_ROOT/.env.local" ]; then set -a source "$PROJECT_ROOT/.env.local" set +a elif [ -f "$PROJECT_ROOT/.env" ]; then set -a source "$PROJECT_ROOT/.env" set +a fi PROFILE="${1:-debug}" WEAR_DIR="$PROJECT_ROOT/targets/wearos" FIREBASE_APP_ID="${FIREBASE_WEAR_APP_ID:-}" # Read version from app.json (single source of truth) VERSION=$(node -p "require('./app.json').expo.version") WEAR_OFFSET=50000000 CODE=$(echo "$VERSION" | awk -F. '{printf "%d%02d%02d", $1, $2, $3}') VERSION_CODE=$((CODE + WEAR_OFFSET)) cd "$WEAR_DIR" echo "Building WearOS ($PROFILE) v$VERSION ($VERSION_CODE)..." if [ "$PROFILE" = "release" ]; then if [ ! -f "$WEAR_DIR/../credentials.json" ]; then echo "credentials.json not found. Downloading from EAS..." cd "$PROJECT_ROOT" eas credentials --platform android --non-interactive 2>/dev/null || { echo "Auto-download failed. Run manually:" echo " eas credentials --platform android" echo " → Choose 'credentials.json: Upload/Download credentials'" echo " → Choose 'Download credentials from EAS to credentials.json'" echo " Then: cp credentials.json targets/wearos/credentials.json" exit 1 } cp credentials.json "$WEAR_DIR/credentials.json" echo "credentials.json copied to targets/wearos/" cd "$WEAR_DIR" fi ./gradlew assembleRelease -PversionName="$VERSION" -PversionCode="$VERSION_CODE" APK_PATH="app/build/outputs/apk/release/app-release.apk" else ./gradlew assembleDebug -PversionName="$VERSION" -PversionCode="$VERSION_CODE" APK_PATH="app/build/outputs/apk/debug/app-debug.apk" fi if [ ! -f "$APK_PATH" ]; then echo "Error: APK not found at $APK_PATH" exit 1 fi APK_SIZE=$(du -h "$APK_PATH" | cut -f1) echo "Build complete: $APK_PATH ($APK_SIZE)" if [ -n "$FIREBASE_APP_ID" ]; then if ! command -v firebase &> /dev/null; then echo "Error: firebase CLI not found. Install with: npm install -g firebase-tools" exit 1 fi RELEASE_NOTES="${2:-}" CMD=(firebase appdistribution:distribute "$APK_PATH" --app "$FIREBASE_APP_ID" --groups testers) if [ -n "$RELEASE_NOTES" ]; then CMD+=(--release-notes "$RELEASE_NOTES") fi echo "Uploading to Firebase App Distribution..." "${CMD[@]}" echo "Upload complete" else echo "Set FIREBASE_WEAR_APP_ID to upload to Firebase App Distribution" echo " export FIREBASE_WEAR_APP_ID=1:123456789:android:abc123" fi