run_firestore_emulator.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. # Copyright 2019 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # USAGE: run_firestore_emulator.sh { run | start | stop }
  16. #
  17. # Downloads and runs the Firestore emulator
  18. set -euo pipefail
  19. # Use Java 11 if it is available on the runner image
  20. if [[ ! -z "${JAVA_HOME_11_X64:-}" ]]; then
  21. export JAVA_HOME=$JAVA_HOME_11_X64
  22. fi
  23. VERSION='1.19.7'
  24. FILENAME="cloud-firestore-emulator-v${VERSION}.jar"
  25. URL="https://storage.googleapis.com/firebase-preview-drop/emulator/${FILENAME}"
  26. cache_dir="${HOME}/.cache/firebase/emulators"
  27. jar="${cache_dir}/${FILENAME}"
  28. cd $(git rev-parse --show-toplevel)
  29. pid_file="cloud-firestore-emulator.pid"
  30. log_file="cloud-firestore-emulator.log"
  31. function help() {
  32. cat 1>&2 <<EOF
  33. run_firestore_emulator.sh {run|start|stop}
  34. EOF
  35. }
  36. # Downloads the emulator jar if it doesn't already exist
  37. function ensure_exists() {
  38. if [[ ! -f "$jar" ]]; then
  39. echo "Downloading Firestore emulator" 1>&2
  40. mkdir -p "${cache_dir}"
  41. curl -s -o "${jar}" "${URL}"
  42. fi
  43. }
  44. # Runs the emulator synchronously
  45. function run() {
  46. exec java -jar "$jar" "$@"
  47. }
  48. # Verifies the emulator isn't already running at the PID in the pid_file
  49. function check_not_running() {
  50. if [[ -f "${pid_file}" ]]; then
  51. pid=$(cat "${pid_file}")
  52. if kill -0 "${pid}" >& /dev/null; then
  53. echo "Firestore emulator already running as PID ${pid}" 1>&2
  54. return 1
  55. fi
  56. echo "Removing stale PID file" 1>&2
  57. rm "${pid_file}"
  58. fi
  59. }
  60. # Starts the emulator in the background
  61. function start() {
  62. check_not_running
  63. run "$@" >& "${log_file}" &
  64. pid="$!"
  65. echo "$pid" > "${pid_file}"
  66. echo "Firestore emulator running as PID ${pid}" 1>&2
  67. }
  68. # Stops the emulator if it's running
  69. function stop() {
  70. if [[ -f "${pid_file}" ]]; then
  71. pid=$(cat "${pid_file}")
  72. kill "${pid}" || true
  73. rm "${pid_file}"
  74. fi
  75. }
  76. command=run
  77. if [[ $# -gt 0 ]]; then
  78. command="$1"
  79. shift
  80. fi
  81. case "${command}" in
  82. run)
  83. ensure_exists
  84. run "$@"
  85. ;;
  86. start)
  87. ensure_exists
  88. start
  89. ;;
  90. stop)
  91. stop
  92. ;;
  93. download)
  94. ensure_exists
  95. ;;
  96. *)
  97. help
  98. exit 1
  99. ;;
  100. esac