From b7ea86b5396dc04df7a0d03bf0334b084114cd7c Mon Sep 17 00:00:00 2001 From: Sergei Kotl Date: Fri, 6 Mar 2026 22:36:30 +0000 Subject: [PATCH] Add .sh scripts to configure, run and deploy in CLI environments --- firebase_ai_logic_showcase/.gitignore | 1 - firebase_ai_logic_showcase/configure.sh | 159 +++++++++++++++++++++++ firebase_ai_logic_showcase/deploy.sh | 28 ++++ firebase_ai_logic_showcase/firebase.json | 5 + firebase_ai_logic_showcase/run.sh | 31 +++++ 5 files changed, 223 insertions(+), 1 deletion(-) create mode 100755 firebase_ai_logic_showcase/configure.sh create mode 100755 firebase_ai_logic_showcase/deploy.sh create mode 100644 firebase_ai_logic_showcase/firebase.json create mode 100755 firebase_ai_logic_showcase/run.sh diff --git a/firebase_ai_logic_showcase/.gitignore b/firebase_ai_logic_showcase/.gitignore index 3948605..df0e787 100644 --- a/firebase_ai_logic_showcase/.gitignore +++ b/firebase_ai_logic_showcase/.gitignore @@ -51,5 +51,4 @@ ephemeral/ #firebase google-services.json GoogleService-Info.plist -firebase.json .firebaserc diff --git a/firebase_ai_logic_showcase/configure.sh b/firebase_ai_logic_showcase/configure.sh new file mode 100755 index 0000000..5bab29a --- /dev/null +++ b/firebase_ai_logic_showcase/configure.sh @@ -0,0 +1,159 @@ +#!/bin/bash + +# Check if GOOGLE_CLOUD_PROJECT is defined +if [ -z "${GOOGLE_CLOUD_PROJECT}" ]; then + echo "Error: GOOGLE_CLOUD_PROJECT environment variable is not set." + exit 1 +else + echo "GOOGLE_CLOUD_PROJECT is set to: ${GOOGLE_CLOUD_PROJECT}" +fi + +# Check if CLOUD_SHELL is set to true +if [ "${CLOUD_SHELL}" = "true" ]; then + echo "CLOUD_SHELL is set to true." +else + echo "Error: CLOUD_SHELL environment variable is not set to true." + exit 1 +fi + +# Set the predefined relative application path +APP_PATH="." +echo "APP_PATH is set to: ${APP_PATH}" + +# Define the bootstrap.js path +BOOTSTRAP_JS_PATH="${APP_PATH}/web/bootstrap.js" +echo "BOOTSTRAP_JS_PATH is set to: ${BOOTSTRAP_JS_PATH}" + +# Check if firebase CLI is installed +if ! command -v firebase &> /dev/null +then + echo "Error: firebase command not found. Please install the Firebase CLI." + echo "Installation instructions: https://firebase.google.com/docs/cli" + exit 1 +fi + +# Check if jq is installed +if ! command -v jq &> /dev/null +then + echo "Error: jq command not found. Please install jq to parse JSON." + echo "Installation instructions: https://stedolan.github.io/jq/download/" + exit 1 +fi + +# Enable Web Frameworks experiment +echo "Enabling Firebase Web Frameworks experiment..." +firebase experiments:enable webframeworks + +# Login to Firebase +echo "Logging into Firebase..." +firebase login + +# Check if the firebase login command was successful +if [ $? -ne 0 ]; then + echo "Error: Failed to login to Firebase." + exit 1 +fi + +# Set the default Firebase project +echo "Setting default Firebase project to: ${GOOGLE_CLOUD_PROJECT}" +firebase use ${GOOGLE_CLOUD_PROJECT} --alias default + +# Check if the firebase use command was successful +if [ $? -ne 0 ]; then + echo "Error: Failed to set Firebase project." + exit 1 +fi + +# List Firebase web apps and parse the output +echo "Fetching Firebase web apps list..." +FIREBASE_APPS_JSON=$(firebase --json apps:list WEB) + +# Check if the apps:list command was successful +if [ $? -ne 0 ]; then + echo "Error: Failed to list Firebase apps." + exit 1 +fi + +# Count the number of apps +APP_COUNT=$(echo "${FIREBASE_APPS_JSON}" | jq '.result | length') + +if [ "${APP_COUNT}" -eq 0 ]; then + echo "Error: No WEB apps found in Firebase project ${GOOGLE_CLOUD_PROJECT}." + exit 1 +elif [ "${APP_COUNT}" -gt 1 ]; then + echo "Error: Multiple WEB apps found in Firebase project ${GOOGLE_CLOUD_PROJECT}. This script expects only one." + echo "${FIREBASE_APPS_JSON}" + exit 1 +fi + +# Extract the appId +APP_ID=$(echo "${FIREBASE_APPS_JSON}" | jq -r '.result[0].appId') + +if [ -z "${APP_ID}" ] || [ "${APP_ID}" = "null" ]; then + echo "Error: Could not extract appId from Firebase app list." + echo "${FIREBASE_APPS_JSON}" + exit 1 +fi + +echo "Found single Firebase WEB app with appId: ${APP_ID}" + +# Get the Firebase app config +echo "Fetching Firebase app config for appId: ${APP_ID}" +FIREBASE_CONFIG_JSON=$(firebase --json apps:sdkconfig WEB ${APP_ID}) + +# Check if the apps:sdkconfig command was successful +if [ $? -ne 0 ]; then + echo "Error: Failed to fetch Firebase app config." + exit 1 +fi + +# Extract config values using jq +API_KEY=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.apiKey') +AUTH_DOMAIN=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.authDomain') +DATABASE_URL=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.databaseURL') +PROJECT_ID=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.projectId') +STORAGE_BUCKET=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.storageBucket') +MESSAGING_SENDER_ID=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.messagingSenderId') +MEASUREMENT_ID=$(echo "${FIREBASE_CONFIG_JSON}" | jq -r '.result.sdkConfig.measurementId') + +# Create the directory for bootstrap.js if it doesn't exist +mkdir -p "$(dirname "${BOOTSTRAP_JS_PATH}")" + +# Generate the bootstrap.js file +echo "Generating ${BOOTSTRAP_JS_PATH}..." +cat << EOF > "${BOOTSTRAP_JS_PATH}" +window["APP_TEMPLATE_BOOTSTRAP"] = { + firebase: { + apiKey: "${API_KEY}", + authDomain: "${AUTH_DOMAIN}", + databaseURL: "${DATABASE_URL}", + projectId: "${PROJECT_ID}", + storageBucket: "${STORAGE_BUCKET}", + messagingSenderId: "${MESSAGING_SENDER_ID}", + appId: "${APP_ID}", + measurementId: "${MEASUREMENT_ID}", + }, +}; +EOF + +if [ $? -ne 0 ]; then + echo "Error: Failed to generate ${BOOTSTRAP_JS_PATH}." + exit 1 +fi + +echo "Successfully generated ${BOOTSTRAP_JS_PATH}." +echo "Configuration checks and setup passed." + +echo "Making sure we can run flutter" +git config --global --add safe.directory /google/flutter + +(cd "${APP_PATH}" && flutter) + +if [ $? -ne 0 ]; then + echo "Error: Failed to run flutter CLI." + exit 1 +fi + +echo "Successfully ran flutter CLI." +exit 0 + diff --git a/firebase_ai_logic_showcase/deploy.sh b/firebase_ai_logic_showcase/deploy.sh new file mode 100755 index 0000000..81013c6 --- /dev/null +++ b/firebase_ai_logic_showcase/deploy.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Set the predefined relative application path +APP_PATH="." +echo "APP_PATH is set to: ${APP_PATH}" + +# Define the bootstrap.js path +BOOTSTRAP_JS_PATH="${APP_PATH}/web/bootstrap.js" +echo "BOOTSTRAP_JS_PATH is set to: ${BOOTSTRAP_JS_PATH}" + +# Check if bootstrap.js exists +if [ ! -f "${BOOTSTRAP_JS_PATH}" ]; then + echo "Error: ${BOOTSTRAP_JS_PATH} not found. Please run configure.sh first." + exit 1 +fi + +echo "${BOOTSTRAP_JS_PATH} found." + +echo "Cleanup the build..." +(cd "${APP_PATH}" && flutter clean) + +# Build the Flutter application +echo "Building the application..." +(cd "${APP_PATH}" && flutter build web --release) + +# Deploy to Firebase Hosting +echo "Deploying to Firebase Hosting..." +(cd "${APP_PATH}" && firebase deploy --only=hosting) diff --git a/firebase_ai_logic_showcase/firebase.json b/firebase_ai_logic_showcase/firebase.json new file mode 100644 index 0000000..2b2992c --- /dev/null +++ b/firebase_ai_logic_showcase/firebase.json @@ -0,0 +1,5 @@ +{ + "hosting": { + "public": "build/web" + } +} diff --git a/firebase_ai_logic_showcase/run.sh b/firebase_ai_logic_showcase/run.sh new file mode 100755 index 0000000..e59dfdd --- /dev/null +++ b/firebase_ai_logic_showcase/run.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Set the predefined relative application path +APP_PATH="." +echo "APP_PATH is set to: ${APP_PATH}" + +# Define the bootstrap.js path +BOOTSTRAP_JS_PATH="${APP_PATH}/web/bootstrap.js" +echo "BOOTSTRAP_JS_PATH is set to: ${BOOTSTRAP_JS_PATH}" + +# Check if bootstrap.js exists +if [ ! -f "${BOOTSTRAP_JS_PATH}" ]; then + echo "Error: ${BOOTSTRAP_JS_PATH} not found. Please run configure.sh first." + exit 1 +fi + +echo "${BOOTSTRAP_JS_PATH} found." + +PID=$(sudo netstat -ltnp | grep :8080 | awk '{print $7}' | cut -d'/' -f1) + if [ -n "$PID" ]; then + echo "Killing process $PID on port 8080" + sudo kill -9 $PID +fi + +# Build the Flutter application +echo "Building the application..." +(cd "${APP_PATH}" && flutter build web --release) + +# Run Python web server +echo "Serving web application..." +(cd "${APP_PATH}/build/web" && python3 -m http.server 8080)