[embedder] Add script to update Fuchsia SDK.

Simplifies the workflow for updating the Fuchsia SDK to a newer version.

I moved common shell script helpers into a separate helper file.

Change-Id: Iff4a9fcd852bf96ac767eecd7ad3a5790520b116
diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel
index 4827e23..5f80cc6 100644
--- a/WORKSPACE.bazel
+++ b/WORKSPACE.bazel
@@ -36,14 +36,12 @@
 
 rules_fuchsia_deps()
 
-# To upgrade the Fuchsia SDK:
-# 1. `cd $FUCHSIA_EMBEDDER_DIR/third_party/sdk-integration && git fetch && git pull origin main`
-# 2. Run `bazel build --config=fuchsia_x64 //src/embedder` or any other Bazel command to
-#    fetch the latest SDK, which will upgrade `tools/ffx`.
+# To update the Fuchsia SDK: `$FUCHSIA_EMBEDDER_DIR/scripts/update_fuchsia_sdk.sh`
 #
-# After upgrading the SDK, you should restart any running emulators and package servers
-# and update your product bundles (`ffx product-bundle get workstation_eng.qemu-x64`), as
-# old images may be incompatible with the upgraded SDK.
+# After updating the SDK, you should restart any running emulators and package servers
+# and update your product bundles (`ffx product-bundle get workstation_eng.qemu-x64`) as
+# old images may be incompatible with the updated SDK. To do this automatically, run:
+# `$FUCHSIA_EMBEDDER_DIR/scripts/update_fuchsia_sdk.sh --cleanup`
 fuchsia_sdk_repository(
     name = "fuchsia_sdk",
 )
diff --git a/scripts/build_and_copy_engine_artifacts.sh b/scripts/build_and_copy_engine_artifacts.sh
index e99f074..1157a54 100755
--- a/scripts/build_and_copy_engine_artifacts.sh
+++ b/scripts/build_and_copy_engine_artifacts.sh
@@ -10,26 +10,13 @@
 # Usage:
 #   build_and_copy_engine_artifacts.sh
 #
-# Prerequisites:
+# Requirements:
 #   1. $ENGINE_DIR is set to the src/ directory of your flutter/engine checkout.
 #   2. $FUCHSIA_EMBEDDER_DIR is set to your flutter-embedder.git checkout.
 #   3. $DEPOT_TOOLS is set to your depot_tools directory.
 
 set -e # Fail on any error.
-
-# Returns true if colors are supported.
-function is-stderr-tty {
-  [[ -t 2 ]]
-}
-
-# Prints a line to stderr with a green INFO: prefix.
-function echo-info {
-  if is-stderr-tty; then
-    echo -e >&2 "\033[1;32mINFO:\033[0m $*"
-  else
-    echo -e >&2 "INFO: $*"
-  fi
-}
+source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/echo_helpers.sh || exit $?
 
 echo-info "Building the debug Flutter Engine embedding for Fuchsia (libflutter_engine.so)..."
 "${ENGINE_DIR}"/flutter/tools/gn --fuchsia --embedder-for-target --unopt
diff --git a/scripts/build_and_run_example.sh b/scripts/build_and_run_example.sh
index 4270816..fd16c43 100755
--- a/scripts/build_and_run_example.sh
+++ b/scripts/build_and_run_example.sh
@@ -3,13 +3,18 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 #
-# Prerequisites:
+# Usage:
+#   build_and_run_example.sh <name_of_dir_in_examples>
+#
+# Requirements:
 #   1. $FUCHSIA_EMBEDDER_DIR is set to your flutter-embedder.git checkout directory.
 #   2. You are currently running Workstation (`ffx emu start --headless workstation_eng.qemu-x64`).
+#   3. The example folder must contain a package target named `<example>_pkg`.
 #
 # TODO(akbiggs): Port this workflow to Bazel.
 
-set -e # Exit if any program returns an error.
+set -e # Fail on any error.
+source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/echo_helpers.sh || exit $?
 
 far_debug_dir=/tmp/"${app_name}"_far_contents
 far_tool="${FUCHSIA_EMBEDDER_DIR}"/bazel-flutter-embedder/external/fuchsia_sdk/tools/x64/far
@@ -39,19 +44,6 @@
   release_args="--release"
 fi
 
-function is-stderr-tty {
-  [[ -t 2 ]]
-}
-
-# echo-info prints a line to stderr with a green INFO: prefix.
-function echo-info {
-  if is-stderr-tty; then
-    echo -e >&2 "\033[1;32mINFO:\033[0m $*"
-  else
-    echo -e >&2 "INFO: $*"
-  fi
-}
-
 pushd "${FUCHSIA_EMBEDDER_DIR}"
 
 echo-info "Building Flutter sample app bundle."
diff --git a/scripts/lib/echo_helpers.sh b/scripts/lib/echo_helpers.sh
new file mode 100644
index 0000000..7955f6a
--- /dev/null
+++ b/scripts/lib/echo_helpers.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+#
+# Copyright 2022 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Returns true if colors are supported.
+function is-stderr-tty {
+  [[ -t 2 ]]
+}
+
+# echo-info prints a line to stderr with a green INFO: prefix.
+function echo-info {
+  if is-stderr-tty; then
+    echo -e >&2 "\033[1;32mINFO:\033[0m $*"
+  else
+    echo -e >&2 "INFO: $*"
+  fi
+}
+
+# echo-warning prints a line to stderr with a yellow WARNING: prefix.
+function echo-warning {
+  if is-stderr-tty; then
+    echo -e >&2 "\033[1;33mWARNING:\033[0m $*"
+  else
+    echo -e >&2 "WARNING: $*"
+  fi
+}
diff --git a/scripts/sync_engine_artifacts_to_revision.sh b/scripts/sync_engine_artifacts_to_revision.sh
index 9a0e4cc..903bc36 100755
--- a/scripts/sync_engine_artifacts_to_revision.sh
+++ b/scripts/sync_engine_artifacts_to_revision.sh
@@ -10,27 +10,14 @@
 # Usage:
 #   sync_engine_artifacts_to_commit.sh <engine_commit>
 #
-# Prerequisites:
+# Requirements:
 #   1. $ENGINE_DIR is set to the src/ directory of your flutter/engine checkout.
 #   2. $FUCHSIA_EMBEDDER_DIR is set to your flutter-embedder.git checkout.
 #   3. $DEPOT_TOOLS is set to your depot_tools directory.
 #   4. You don't have any uncommited changes to your Engine code.
 
 set -e # Fail on any error.
-
-# Returns true if colors are supported.
-function is-stderr-tty {
-  [[ -t 2 ]]
-}
-
-# Prints a line to stderr with a green INFO: prefix.
-function echo-info {
-  if is-stderr-tty; then
-    echo -e >&2 "\033[1;32mINFO:\033[0m $*"
-  else
-    echo -e >&2 "INFO: $*"
-  fi
-}
+source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/echo_helpers.sh || exit $?
 
 engine_revision=$1
 
diff --git a/scripts/update_fuchsia_sdk.sh b/scripts/update_fuchsia_sdk.sh
new file mode 100755
index 0000000..3eb8880
--- /dev/null
+++ b/scripts/update_fuchsia_sdk.sh
@@ -0,0 +1,67 @@
+#!/bin/bash
+#
+# Copyright 2022 The Fuchsia Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Updates the version of ffx used by this repository to the latest
+# version.
+#
+# Usage:
+#   update_fuchsia_sdk.sh
+#
+# Requirements:
+#   1. $FUCHSIA_EMBEDDER_DIR is set to your flutter-embedder.git checkout.
+
+set -e # Fail on any error.
+source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/echo_helpers.sh || exit $?
+
+# Parse arguments.
+cleanup=0
+while [[ $# -gt 0 ]]; do
+  case $1 in
+    --cleanup)
+      cleanup=1
+      shift # past argument
+      ;;
+    *)
+      shift # past value
+      ;;
+  esac
+done
+
+echo-info "Updating the Fuchsia SDK checkout..."
+git -C $FUCHSIA_EMBEDDER_DIR/third_party/sdk-integration pull origin main
+
+echo-info "Building the embedder to fetch the Fuchsia SDK tools..."
+pushd $FUCHSIA_EMBEDDER_DIR
+bazel build --config=fuchsia_x64 //src/embedder
+popd
+
+if [[ "${cleanup}" -eq 0 ]]
+then
+  echo-info "Your Fuchsia SDK has been updated to version `$FUCHSIA_EMBEDDER_DIR/tools/ffx version`."
+  echo-warning 'You should restart any running emulators (`ffx emu stop`)'
+  echo-warning 'and update your product bundles (`ffx product-bundle get workstation_eng.qemu-x64`) as'
+  echo-warning 'old images may be incompatible with the updated SDK.'
+  echo-warning 'To do this automatically, run: $FUCHSIA_EMBEDDER_DIR/scripts/update_fuchsia_sdk.sh --cleanup'
+  exit 0
+fi
+
+echo-info "Cleaning up old things that are incompatible with the new SDK."
+echo-info "If you don't want this, don't run the script with --cleanup."
+echo-info "Killing any running emulators."
+ffx emu stop
+
+# TODO(https://fxbug.dev/106963): Replace with an ffx product-bundle clean command once
+# one exists.
+echo-info "Deleting old product bundles."
+rm -rf ~/.local/share/Fuchsia/ffx/pbms
+
+echo-info "Refetching the workstation_eng.qemu-x64 product bundle."
+ffx product-bundle get workstation_eng.qemu-x64
+
+echo
+echo-info "Your Fuchsia SDK has been updated to version `$FUCHSIA_EMBEDDER_DIR/tools/ffx version`."
+echo-info "You will need to restart your emulator by running:"
+echo '$FUCHSIA_EMBEDDER_DIR/tools/ffx emu start workstation_eng.qemu-x64'