|
| 1 | +#!/usr/bin/env zsh |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +setopt errexit nounset pipefail |
| 18 | + |
| 19 | +source "${0:A:h:h}/scripts/util/say.zsh" |
| 20 | + |
| 21 | +typeset -r self_executable="$0" |
| 22 | +typeset -r log_prefix="[$0]" |
| 23 | +typeset -r default_postgresql_string='postgresql://postgres:postgres@localhost:5432?sslmode=disable' |
| 24 | + |
| 25 | +main() { |
| 26 | + cd "${0:A:h}" |
| 27 | + parse_args "$@" |
| 28 | + log "FIREBASE_DATACONNECT_POSTGRESQL_STRING=${FIREBASE_DATACONNECT_POSTGRESQL_STRING}" |
| 29 | + log "DATACONNECT_EMULATOR_BINARY_PATH=${DATACONNECT_EMULATOR_BINARY_PATH}" |
| 30 | + log "DATA_CONNECT_PREVIEW=${DATA_CONNECT_PREVIEW}" |
| 31 | + run_command firebase --debug emulators:start --only auth,dataconnect |
| 32 | +} |
| 33 | + |
| 34 | +parse_args() { |
| 35 | + zmodload zsh/zutil |
| 36 | + local -A opts |
| 37 | + zparseopts -D -E -A opts c: g p: v: w h || { |
| 38 | + sayp "Run with %F{cyan}-h%f for help" >&2 |
| 39 | + exit 2 |
| 40 | + } |
| 41 | + |
| 42 | + if (( ${+opts[-h]} )); then |
| 43 | + print_help |
| 44 | + exit 0 |
| 45 | + fi |
| 46 | + |
| 47 | + if (( # > 0 )); then |
| 48 | + say_error "unrecognized option or argument: $1" >&2 |
| 49 | + sayp "Run with %F{cyan}-h%f for help" >&2 |
| 50 | + exit 2 |
| 51 | + fi |
| 52 | + |
| 53 | + typeset emulator_binary="${opts[-c]:-${opts[-g]+gradle}}" |
| 54 | + typeset postgresql_string="${opts[-p]:-$default_postgresql_string}" |
| 55 | + typeset preview_flags="${opts[-v]:-}" |
| 56 | + typeset wipe_and_restart_postgres_pod=${+opts[-w]} |
| 57 | + |
| 58 | + if [[ ${emulator_binary} != "gradle" ]] ; then |
| 59 | + export DATACONNECT_EMULATOR_BINARY_PATH="${emulator_binary}" |
| 60 | + else |
| 61 | + run_command "../../gradlew" -p ../.. --configure-on-demand :firebase-dataconnect:connectors:downloadDebugDataConnectExecutable |
| 62 | + typeset gradle_emulator_binaries=(../connectors/build/intermediates/dataconnect/debug/executable/*(N)) |
| 63 | + if (( ${#gradle_emulator_binaries} != 1 )); then |
| 64 | + log_error_and_exit "expected exactly 1 emulator binary from gradle, but got ${#gradle_emulator_binaries}: ${gradle_emulator_binaries[*]}" |
| 65 | + fi |
| 66 | + typeset gradle_emulator_binary="${gradle_emulator_binaries[1]}" |
| 67 | + if [[ ! -e ${gradle_emulator_binary} ]] ; then |
| 68 | + log_error_and_exit "emulator binary from gradle does not exist: ${gradle_emulator_binary}" |
| 69 | + fi |
| 70 | + export DATACONNECT_EMULATOR_BINARY_PATH="${gradle_emulator_binary}" |
| 71 | + fi |
| 72 | + |
| 73 | + export FIREBASE_DATACONNECT_POSTGRESQL_STRING="${postgresql_string}" |
| 74 | + export DATA_CONNECT_PREVIEW="${preview_flags}" |
| 75 | + |
| 76 | + if [[ ${wipe_and_restart_postgres_pod} == "1" ]] ; then |
| 77 | + run_command podman compose down -v |
| 78 | + run_command podman compose up -d |
| 79 | + |
| 80 | + say "Waiting for Postgres service to appear to be healthy..." |
| 81 | + typeset postgres_health_check_number=0 |
| 82 | + while : ; do |
| 83 | + (( postgres_health_check_number++ )) || true |
| 84 | + typeset postgres_service_status="$(print_postgres_status)" |
| 85 | + say "Postgres service health check ${postgres_health_check_number}: ${postgres_service_status}" |
| 86 | + |
| 87 | + if [[ ${postgres_service_status} == *"(healthy)"* ]] ; then |
| 88 | + say "Postgres service appears to be healthy after ${postgres_health_check_number} seconds" |
| 89 | + break |
| 90 | + elif (( ${postgres_health_check_number} == 30 )) ; then |
| 91 | + print_podman_compose_status |
| 92 | + say "ERROR: Postgres service does not appear to be healthy after ${postgres_health_check_number} seconds" >&2 |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + |
| 96 | + sleep 1s |
| 97 | + done |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +run_command() { |
| 102 | + logn "Running command: " |
| 103 | + say_args "$@" |
| 104 | +} |
| 105 | + |
| 106 | +print_podman_compose_status() { |
| 107 | + podman compose ps --format=json |
| 108 | +} |
| 109 | + |
| 110 | +print_postgres_status() { |
| 111 | + print_podman_compose_status | jq -re '.[] | select(.Labels["com.docker.compose.service"] == "postgres") | .Status' |
| 112 | +} |
| 113 | + |
| 114 | +print_help() { |
| 115 | + sayp "%BFirebase Data Connect Emulator Launcher Helper%b" |
| 116 | + say |
| 117 | + saypn "%BSyntax: " |
| 118 | + sayn "${self_executable}" |
| 119 | + sayp " [options]%b" |
| 120 | + say |
| 121 | + say "This script provides a convenient way to launch the Firebase Data Connect" |
| 122 | + say "and Firebase Authentication emulators in a way that is amenable for running" |
| 123 | + say "the integration tests." |
| 124 | + say |
| 125 | + sayp "%UOptions:%u" |
| 126 | + say |
| 127 | + sayp "%F{cyan} -c <data_connect_emulator_binary_path>%f" |
| 128 | + say " Uses the Data Connect Emulator binary at the given path. A value of \"gradle\" " |
| 129 | + say " will use the same binary as the Gradle build. If not specified, or if specified " |
| 130 | + say " as the empty string, then the emulator binary is downloaded." |
| 131 | + say |
| 132 | + sayp "%F{cyan} -g%f" |
| 133 | + say " Shorthand for: -c gradle" |
| 134 | + say |
| 135 | + sayp "%F{cyan} -p <postgresql_connection_string>%f" |
| 136 | + say " Uses the given string to connect to the PostgreSQL server. If not specified " |
| 137 | + say " the the default value of \"${default_postgresql_string}\" is used." |
| 138 | + say " If specified as the empty string then an ephemeral PGLite server is used." |
| 139 | + say |
| 140 | + sayp "%F{cyan} -v <data_connect_preview_flags>%f" |
| 141 | + say " Uses the given Data Connect preview flags when launching the emulator." |
| 142 | + say " If not specified then an empty string is used, meaning that no preview flags" |
| 143 | + say " are in effect." |
| 144 | + say |
| 145 | + sayp "%F{cyan} -w%f" |
| 146 | + say " If specified, then a local PostgreSQL container is wiped and restarted" |
| 147 | + say " before launching the emulators. This is accomplished by running:" |
| 148 | + say " podman compose down -v && podman compose up -d" |
| 149 | + say |
| 150 | + sayp "%F{cyan} -h%f" |
| 151 | + say " Print this help screen and exit, as if successful." |
| 152 | +} |
| 153 | + |
| 154 | +log() { |
| 155 | + logn "$@" |
| 156 | + say |
| 157 | +} |
| 158 | + |
| 159 | +logn() { |
| 160 | + saypn "%F{magenta}" |
| 161 | + sayn "${log_prefix} " |
| 162 | + saypn "%f" |
| 163 | + sayn "$@" |
| 164 | +} |
| 165 | + |
| 166 | +log_error_and_exit() { |
| 167 | + say_error "$@" >&2 |
| 168 | + return 1 |
| 169 | +} |
| 170 | + |
| 171 | +main "$@" |
0 commit comments