From 107f263fb159761a77d452068e6248238d5f5355 Mon Sep 17 00:00:00 2001 From: Siddarth Kumar Date: Mon, 19 Feb 2024 21:11:36 +0530 Subject: [PATCH] chore: improve android & iOS build step (#18900) fixes #18888 ## Summary `make run-android` was sometimes flaky because we used to send metro to background via `nohup` and then bring it back to foreground after we read metro logs. Now we do not send metro to background. We first wait for a successful build. we then install the app on the simulator. After this is done we give command to sleep until metro server has started, Once metro server has started we open the installed app. In this workflow the command to open the installed app goes in background and metro stays in foreground. The new workflow should now be less flaky. ## Review notes `make run-clojure` `make run-android` OR `make run-ios` should just work #### Platforms - Android - iOS --- .gitignore | 3 ++ scripts/run-android.sh | 53 +++++++++------------------------- scripts/run-ios.sh | 51 +++++++++----------------------- scripts/wait-for-metro-port.sh | 14 +++++++++ 4 files changed, 45 insertions(+), 76 deletions(-) create mode 100755 scripts/wait-for-metro-port.sh diff --git a/.gitignore b/.gitignore index e4c7e47f89..d0f80477e9 100644 --- a/.gitignore +++ b/.gitignore @@ -197,3 +197,6 @@ lefthook.yml ## metro server logs metro-server-logs.log + +## debug build time logs +logs/ diff --git a/scripts/run-android.sh b/scripts/run-android.sh index 8f4c11aa84..97c3b0c81d 100755 --- a/scripts/run-android.sh +++ b/scripts/run-android.sh @@ -1,37 +1,9 @@ #!/usr/bin/env bash set -euo pipefail -set -m # needed to access jobs GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) - -# We run Metro in background while calling adb. -cleanupMetro() { - pkill -f run-metro.sh - rm -f metro-server-logs.log -} - -# Using function gives a neater jobspec name. -runMetro() { - nohup "${GIT_ROOT}/scripts/run-metro.sh" 2>&1 \ - | tee metro-server-logs.log -} - -waitForMetro() { - set +e # Allow grep command to fail in the loop. - TIMEOUT=5 - echo "Waiting for Metro server..." >&2 - while ! grep -q "Welcome to Metro" metro-server-logs.log; do - echo -n "." >&2 - sleep 1 - if ((TIMEOUT == 0)); then - echo -e "\nMetro server timed out, exiting" >&2 - set -e # Restore errexit for rest of script. - return 1 - fi - ((TIMEOUT--)) - done - set -e # Restore errexit for rest of script. -} +ADB_INSTALL_LOG_FILE="${GIT_ROOT}/logs/adb_install.log" +ADB_SHELL_MONKEY_LOG_FILE="${GIT_ROOT}/logs/adb_shell_monkey.log" # Generate android debug build. export ANDROID_ABI_INCLUDE=$("${GIT_ROOT}/scripts/adb_devices_abis.sh") @@ -40,14 +12,17 @@ export BUILD_TYPE=debug "${GIT_ROOT}/scripts/build-android.sh" # Install the APK on running emulator or android device. -adb install ./result/app-debug.apk +installAndLaunchApp() { + adb install ./result/app-debug.apk > "${ADB_INSTALL_LOG_FILE}" 2>&1 + "${GIT_ROOT}/scripts/wait-for-metro-port.sh" 2>&1 + adb shell monkey -p im.status.ethereum.debug 1 > "${ADB_SHELL_MONKEY_LOG_FILE}" 2>&1 +} -trap cleanupMetro EXIT ERR INT QUIT -runMetro & -waitForMetro +showAdbLogs() { + cat "${ADB_INSTALL_LOG_FILE}" >&2; + cat "${ADB_SHELL_MONKEY_LOG_FILE}" >&2; +} -# Start the installed app. -adb shell monkey -p im.status.ethereum.debug 1 - -# bring metro job to foreground -fg 'runMetro' +trap showAdbLogs EXIT ERR INT QUIT +installAndLaunchApp & +exec "${GIT_ROOT}/scripts/run-metro.sh" 2>&1 diff --git a/scripts/run-ios.sh b/scripts/run-ios.sh index f9f94c661b..d3a530bfe0 100755 --- a/scripts/run-ios.sh +++ b/scripts/run-ios.sh @@ -3,34 +3,20 @@ set -euo pipefail set -m # needed to access jobs GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel) +XCRUN_INSTALL_LOG_FILE="${GIT_ROOT}/logs/xcrun_install.log" +XCRUN_LAUNCH_LOG_FILE="${GIT_ROOT}/logs/xcrun_launch.log" + +# Install on the simulator +installAndLaunchApp() { + xcrun simctl install "$UUID" "$APP_PATH" > "${XCRUN_INSTALL_LOG_FILE}" 2>&1 + "${GIT_ROOT}/scripts/wait-for-metro-port.sh" 2>&1 + xcrun simctl launch "$UUID" im.status.ethereum.debug > "${XCRUN_LAUNCH_LOG_FILE}" 2>&1 -# We run Metro in background while calling adb. -cleanupMetro() { - pkill -f run-metro.sh - rm -f metro-server-logs.log } -# Using function gives a neater jobspec name. -runMetro() { - nohup "${GIT_ROOT}/scripts/run-metro.sh" 2>&1 \ - | tee metro-server-logs.log -} - -waitForMetro() { - set +e # Allow grep command to fail in the loop. - TIMEOUT=5 - echo "Waiting for Metro server..." >&2 - while ! grep -q "Welcome to Metro" metro-server-logs.log; do - echo -n "." >&2 - sleep 1 - if ((TIMEOUT == 0)); then - echo -e "\nMetro server timed out, exiting" >&2 - set -e # Restore errexit for rest of script. - return 1 - fi - ((TIMEOUT--)) - done - set -e # Restore errexit for rest of script. +showXcrunLogs() { + cat "${XCRUN_INSTALL_LOG_FILE}" >&2; + cat "${XCRUN_LAUNCH_LOG_FILE}" >&2; } # Check if the first argument is provided @@ -65,15 +51,6 @@ xcodebuild -workspace "ios/StatusIm.xcworkspace" -configuration Debug -scheme St APP_PATH="${BUILD_DIR}/Build/Products/Debug-iphonesimulator/StatusIm.app" -# Install on the simulator -xcrun simctl install "$UUID" "$APP_PATH" - -trap cleanupMetro EXIT ERR INT QUIT -runMetro & -waitForMetro - -# launch the app when metro is ready -xcrun simctl launch "$UUID" im.status.ethereum.debug - -# bring metro job to foreground -fg 'runMetro' +trap showXcrunLogs EXIT ERR INT QUIT +installAndLaunchApp & +exec "${GIT_ROOT}/scripts/run-metro.sh" 2>&1 diff --git a/scripts/wait-for-metro-port.sh b/scripts/wait-for-metro-port.sh new file mode 100755 index 0000000000..67144cfe5a --- /dev/null +++ b/scripts/wait-for-metro-port.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -euo pipefail + +TIMEOUT=10 # Metro should not take this long to start. + +while [ "${TIMEOUT}" -gt 0 ]; do + if ! lsof -i:8081 &> /dev/null; then + echo "." + sleep 1 + ((TIMEOUT--)) + else + break + fi +done