From b0afc326530380553cabc23e98ebb7cccfddcf47 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Sun, 8 Oct 2023 19:35:21 +0530 Subject: [PATCH 1/8] made setupNewDotWebForEmulators script --- package.json | 3 +- scripts/select-device.sh | 87 +++++++++++++++++ scripts/setup-newdot-web-emulators.sh | 130 ++++++++++++++++++++++++++ scripts/shellUtils.sh | 29 +++++- 4 files changed, 243 insertions(+), 6 deletions(-) create mode 100755 scripts/select-device.sh create mode 100755 scripts/setup-newdot-web-emulators.sh diff --git a/package.json b/package.json index 9a3b9ed3af86..26937ea33741 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "private": true, "scripts": { "configure-mapbox": "scripts/setup-mapbox-sdk-walkthrough.sh", + "setupNewDotWebForEmulators": "scripts/setup-newdot-web-emulators.sh", "postinstall": "scripts/postInstall.sh", "clean": "npx react-native clean-project-auto", "android": "scripts/set-pusher-suffix.sh && npx react-native run-android --variant=developmentDebug --appId=com.expensify.chat.dev", @@ -114,8 +115,8 @@ "react-collapse": "^5.1.0", "react-content-loader": "^6.1.0", "react-dom": "18.1.0", - "react-map-gl": "^7.1.3", "react-error-boundary": "^4.0.11", + "react-map-gl": "^7.1.3", "react-native": "0.72.4", "react-native-android-location-enabler": "^1.2.2", "react-native-blob-util": "^0.17.3", diff --git a/scripts/select-device.sh b/scripts/select-device.sh new file mode 100755 index 000000000000..47e7cfca227c --- /dev/null +++ b/scripts/select-device.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +# Use functions and varaibles from the utils script +source scripts/shellUtils.sh + +select_device_ios() +{ + IFS="$@" arr=$(xcrun xctrace list devices | grep -E "iPhone|iPad") + + # Create arrays to store device names and identifiers + device_names=() + device_identifiers=() + + # Parse the device list and populate the arrays + while IFS= read -r line; do + if [[ $line =~ ^(.*)\ \((.*)\)\ \((.*)\)$ ]]; then + device="${BASH_REMATCH[1]}" + version="${BASH_REMATCH[2]}" + identifier="${BASH_REMATCH[3]}" + device_identifiers+=("$identifier") + device_names+=("$device Version: $version") + else + info "Input does not match the expected pattern." + echo $line + fi + done <<< $arr + if [ ${#device_names[@]} -eq 0 ]; then + error "No devices detected, please create one." + exit 1 + fi + if [ ${#device_names[@]} -eq 1 ]; then + device_identifier="${device_identifiers[0]}" + success "Single device detected, launching ${device_names[0]}" + open -a Simulator --args -CurrentDeviceUDID $device_identifier + return + fi + info "Multiple devices detected, please select one from the list." + PS3='Please enter your choice: ' + select device_name in "${device_names[@]}"; do + if [ -n "$device_name" ]; then + selected_index=$((REPLY - 1)) + device_name_for_display="${device_names[$selected_index]}" + device_identifier="${device_identifiers[$selected_index]}" + break + else + echo "Invalid selection. Please choose a device." + fi + done + success "Launching $device_name_for_display" + open -a Simulator --args -CurrentDeviceUDID $device_identifier +} + +select_device_android() +{ + IFS="$@" arr=$(emulator -list-avds) + + # Create arrays to store device names + device_names=() + + # Parse the device list and populate the arrays + while IFS= read -r line; do + device_names+=("$line") + done <<< $arr + if [ ${#device_names[@]} -eq 0 ]; then + error "No devices detected, please create one." + exit 1 + fi + if [ ${#device_names[@]} -eq 1 ]; then + device_identifier="${device_names[0]}" + success "Single device detected, launching $device_identifier" + emulator -avd $device_names -writable-system > /dev/null 2>&1 & + return + fi + info "Multiple devices detected, please select one from the list." + PS3='Please enter your choice: ' + select device_name in "${device_names[@]}"; do + if [ -n "$device_name" ]; then + selected_index=$((REPLY - 1)) + device_identifier="${device_names[$selected_index]}" + break + else + echo "Invalid selection. Please choose a device." + fi + done + success "Launching $device_identifier" + emulator -avd $device_identifier -writable-system > /dev/null 2>&1 & +} \ No newline at end of file diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh new file mode 100755 index 000000000000..14d99e550d05 --- /dev/null +++ b/scripts/setup-newdot-web-emulators.sh @@ -0,0 +1,130 @@ +#!/bin/bash + +# NewDot Web Configuration Script for iOS and Android Emulators +# ============================================================= +# +# Purpose: +# -------- +# This script configures Configure iOS simulators and Android emulators to connect to +# new.expensify.com.dev over https for local development. +# +# Background: +# ----------- +# We plan to change the URL to serve the App on the development environment from +# localhost:8082 to new.expensify.com.dev. This can be accomplished by adding a new entry +# to the laptop's hosts file along with changes made in the PR. +# However, we're not sure how we can access the App on Safari or Chrome on iOS or Android +# simulators. +# +# How this script helps: +# ---------------------- +# This script streamlines the process of adding the certificates to both android and +# ios platforms. +# +# Usage: +# ------ +# To run this script, pass the platform on which you want to run as a command-line +# argument which can be the following: +# 1. ios +# 2. android +# 3. all (default) +# ./setup-newdot-web-emulators.sh platform + +# Use functions and varaibles from the utils script +source scripts/shellUtils.sh + +# Use functions to select and open the emulator for iOS and Android +source scripts/select-device.sh + +if [ $# -eq 0 ]; then + platform="all" +else + platform=$1 +fi + +setup_ios() +{ + select_device_ios + sleep 5 + info "Installing certificates on iOS simulator" + xcrun simctl keychain booted add-cert ./config/webpack/certificate.pem +} + +restart_adb_server() { + info "Restarting adb ..." + adb kill-server + sleep 2 + adb start-server + sleep 2 + info "Restarting adb done" +} + +ensure_device_available() { + # Must turn off exit on error temporarily + set +e + adb devices | grep -q offline + if [ $? -eq 0 ]; then + restart_adb_server + adb devices | grep -q offline + if [ $? -eq 0 ]; then + error "Device remains 'offline'. Please investigate!" + exit 1 + fi + fi + set -e +} + +setup_android_path_1() +{ + adb root || { + error "Failed to restart adb as root" + info "You may want to check https://stackoverflow.com/a/45668555" + exit 1 + } + sleep 2 + adb remount + adb push /etc/hosts /system/etc/hosts +} + +setup_android_path_2() +{ + adb root || { + error "Failed to restart adb as root" + info "You may want to check https://stackoverflow.com/a/45668555" + exit 1 + } + sleep 2 + adb disable-verity + adb reboot + sleep 30 + ensure_device_available + adb root + sleep 2 + adb remount + adb push /etc/hosts /system/etc/hosts +} + +setup_android() +{ + select_device_android + sleep 5 + info "Installing certificates on Android emulator" + setup_android_path_1 || { + info "Looks like the system partition is read-only" + info "Trying another method to install the certificates" + setup_android_path_2 + } +} + +if [ "$platform" = "ios" ] || [ "$platform" = "all" ]; then + setup_ios || { + error "Failed to setup iOS simulator" + exit 1 + } +fi + +if [ "$platform" = "android" ] || [ "$platform" = "all" ]; then + setup_android +fi + +success "Done!" \ No newline at end of file diff --git a/scripts/shellUtils.sh b/scripts/shellUtils.sh index 4c9e2febc34d..848e6d238254 100644 --- a/scripts/shellUtils.sh +++ b/scripts/shellUtils.sh @@ -1,10 +1,29 @@ #!/bin/bash -declare -r GREEN=$'\e[1;32m' -declare -r RED=$'\e[1;31m' -declare -r BLUE=$'\e[1;34m' -declare -r TITLE=$'\e[1;4;34m' -declare -r RESET=$'\e[0m' +# Check if GREEN has already been defined +if [ -z "${GREEN+x}" ]; then + declare -r GREEN=$'\e[1;32m' +fi + +# Check if RED has already been defined +if [ -z "${RED+x}" ]; then + declare -r RED=$'\e[1;31m' +fi + +# Check if BLUE has already been defined +if [ -z "${BLUE+x}" ]; then + declare -r BLUE=$'\e[1;34m' +fi + +# Check if TITLE has already been defined +if [ -z "${TITLE+x}" ]; then + declare -r TITLE=$'\e[1;4;34m' +fi + +# Check if RESET has already been defined +if [ -z "${RESET+x}" ]; then + declare -r RESET=$'\e[0m' +fi function success { echo "🎉 $GREEN$1$RESET" From b02536a2f2f19349275b80e730e1d57e9a69f7e0 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Sun, 8 Oct 2023 20:18:13 +0530 Subject: [PATCH 2/8] fix lint --- scripts/select-device.sh | 16 +++++++++------- scripts/setup-newdot-web-emulators.sh | 6 ++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/select-device.sh b/scripts/select-device.sh index 47e7cfca227c..3c318ede36b6 100755 --- a/scripts/select-device.sh +++ b/scripts/select-device.sh @@ -5,6 +5,7 @@ source scripts/shellUtils.sh select_device_ios() { + # shellcheck disable=SC2124 IFS="$@" arr=$(xcrun xctrace list devices | grep -E "iPhone|iPad") # Create arrays to store device names and identifiers @@ -21,9 +22,9 @@ select_device_ios() device_names+=("$device Version: $version") else info "Input does not match the expected pattern." - echo $line + echo "$line" fi - done <<< $arr + done <<< "$arr" if [ ${#device_names[@]} -eq 0 ]; then error "No devices detected, please create one." exit 1 @@ -31,7 +32,7 @@ select_device_ios() if [ ${#device_names[@]} -eq 1 ]; then device_identifier="${device_identifiers[0]}" success "Single device detected, launching ${device_names[0]}" - open -a Simulator --args -CurrentDeviceUDID $device_identifier + open -a Simulator --args -CurrentDeviceUDID "$device_identifier" return fi info "Multiple devices detected, please select one from the list." @@ -47,11 +48,12 @@ select_device_ios() fi done success "Launching $device_name_for_display" - open -a Simulator --args -CurrentDeviceUDID $device_identifier + open -a Simulator --args -CurrentDeviceUDID "$device_identifier" } select_device_android() { + # shellcheck disable=SC2124 IFS="$@" arr=$(emulator -list-avds) # Create arrays to store device names @@ -60,7 +62,7 @@ select_device_android() # Parse the device list and populate the arrays while IFS= read -r line; do device_names+=("$line") - done <<< $arr + done <<< "$arr" if [ ${#device_names[@]} -eq 0 ]; then error "No devices detected, please create one." exit 1 @@ -68,7 +70,7 @@ select_device_android() if [ ${#device_names[@]} -eq 1 ]; then device_identifier="${device_names[0]}" success "Single device detected, launching $device_identifier" - emulator -avd $device_names -writable-system > /dev/null 2>&1 & + emulator -avd "$device_identifier" -writable-system > /dev/null 2>&1 & return fi info "Multiple devices detected, please select one from the list." @@ -83,5 +85,5 @@ select_device_android() fi done success "Launching $device_identifier" - emulator -avd $device_identifier -writable-system > /dev/null 2>&1 & + emulator -avd "$device_identifier" -writable-system > /dev/null 2>&1 & } \ No newline at end of file diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 14d99e550d05..558d5ae99900 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -62,11 +62,9 @@ restart_adb_server() { ensure_device_available() { # Must turn off exit on error temporarily set +e - adb devices | grep -q offline - if [ $? -eq 0 ]; then + if adb devices | grep -q offline; then restart_adb_server - adb devices | grep -q offline - if [ $? -eq 0 ]; then + if adb devices | grep -q offline; then error "Device remains 'offline'. Please investigate!" exit 1 fi From 7c7c6cfb095f39aa4ac04ba95e58fef5b30ebe82 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Mon, 9 Oct 2023 11:40:53 +0530 Subject: [PATCH 3/8] fix flow --- scripts/select-device.sh | 10 ++++++++++ scripts/setup-newdot-web-emulators.sh | 3 +++ 2 files changed, 13 insertions(+) diff --git a/scripts/select-device.sh b/scripts/select-device.sh index 3c318ede36b6..f1ad18bf8d96 100755 --- a/scripts/select-device.sh +++ b/scripts/select-device.sh @@ -51,6 +51,11 @@ select_device_ios() open -a Simulator --args -CurrentDeviceUDID "$device_identifier" } +kill_all_emulators_ios() { + # kill all the emulators + killall Simulator +} + select_device_android() { # shellcheck disable=SC2124 @@ -86,4 +91,9 @@ select_device_android() done success "Launching $device_identifier" emulator -avd "$device_identifier" -writable-system > /dev/null 2>&1 & +} + +kill_all_emulators_android() { + # kill all the emulators + adb devices | grep emulator | cut -f1 | while read -r line; do adb -s "$line" emu kill; done } \ No newline at end of file diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 558d5ae99900..29c52f7a57e7 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -48,6 +48,7 @@ setup_ios() sleep 5 info "Installing certificates on iOS simulator" xcrun simctl keychain booted add-cert ./config/webpack/certificate.pem + kill_all_emulators_ios } restart_adb_server() { @@ -82,6 +83,7 @@ setup_android_path_1() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts + kill_all_emulators_android } setup_android_path_2() @@ -100,6 +102,7 @@ setup_android_path_2() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts + kill_all_emulators_android } setup_android() From fc5cecb16c8767c6e8a0e018fae3cf36be71237b Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Mon, 9 Oct 2023 19:04:55 +0530 Subject: [PATCH 4/8] fix somethings --- scripts/select-device.sh | 16 ++++++++++++++-- scripts/setup-mapbox-sdk-walkthrough.sh | 2 +- scripts/setup-mapbox-sdk.sh | 2 +- scripts/setup-newdot-web-emulators.sh | 9 ++++++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/scripts/select-device.sh b/scripts/select-device.sh index f1ad18bf8d96..d83709d927ab 100755 --- a/scripts/select-device.sh +++ b/scripts/select-device.sh @@ -1,6 +1,18 @@ #!/bin/bash -# Use functions and varaibles from the utils script +# Utility script for iOS and Android Emulators +# ============================================ +# +# Purpose: +# -------- +# This script helps to start and kill iOS simulators and Android emulators instances. +# +# How this script helps: +# ---------------------- +# This script streamlines the process of starting and killing on both android and ios +# platforms. + +# Use functions and variables from the utils script source scripts/shellUtils.sh select_device_ios() @@ -96,4 +108,4 @@ select_device_android() kill_all_emulators_android() { # kill all the emulators adb devices | grep emulator | cut -f1 | while read -r line; do adb -s "$line" emu kill; done -} \ No newline at end of file +} diff --git a/scripts/setup-mapbox-sdk-walkthrough.sh b/scripts/setup-mapbox-sdk-walkthrough.sh index 20b79641fc42..46b970f2d462 100755 --- a/scripts/setup-mapbox-sdk-walkthrough.sh +++ b/scripts/setup-mapbox-sdk-walkthrough.sh @@ -21,7 +21,7 @@ # To configure Mapbox, invoke this script by running the following command from the project's root directory: # npm run configure-mapbox -# Use functions and varaibles from the utils script +# Use functions and variables from the utils script source scripts/shellUtils.sh # Intro message diff --git a/scripts/setup-mapbox-sdk.sh b/scripts/setup-mapbox-sdk.sh index 06fd75fba299..fa37cc2fbbad 100755 --- a/scripts/setup-mapbox-sdk.sh +++ b/scripts/setup-mapbox-sdk.sh @@ -36,7 +36,7 @@ # To run this script, pass the secret Mapbox access token as a command-line argument: # ./scriptname.sh YOUR_MAPBOX_ACCESS_TOKEN -# Use functions and varaibles from the utils script +# Use functions and variables from the utils script source scripts/shellUtils.sh NETRC_PATH="$HOME/.netrc" diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 29c52f7a57e7..1d886796c64d 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -30,7 +30,7 @@ # 3. all (default) # ./setup-newdot-web-emulators.sh platform -# Use functions and varaibles from the utils script +# Use functions and variables from the utils script source scripts/shellUtils.sh # Use functions to select and open the emulator for iOS and Android @@ -125,7 +125,10 @@ if [ "$platform" = "ios" ] || [ "$platform" = "all" ]; then fi if [ "$platform" = "android" ] || [ "$platform" = "all" ]; then - setup_android + setup_android || { + error "Failed to setup Android emulator" + exit 1 + } fi -success "Done!" \ No newline at end of file +success "Done!" From 0069124124a79f2ec5b072be2ec3000b4d90187d Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Mon, 9 Oct 2023 21:24:35 +0530 Subject: [PATCH 5/8] fix timing --- scripts/setup-newdot-web-emulators.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 1d886796c64d..1cea4aaeab25 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -83,7 +83,7 @@ setup_android_path_1() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts - kill_all_emulators_android + # kill_all_emulators_android } setup_android_path_2() @@ -102,13 +102,14 @@ setup_android_path_2() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts - kill_all_emulators_android + # kill_all_emulators_android } setup_android() { select_device_android - sleep 5 + sleep 30 + ensure_device_available info "Installing certificates on Android emulator" setup_android_path_1 || { info "Looks like the system partition is read-only" From 51deb9de8a4b6e05720ded0b18e430860c389c6f Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Tue, 10 Oct 2023 20:13:47 +0530 Subject: [PATCH 6/8] fix ios flow --- scripts/setup-newdot-web-emulators.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 1cea4aaeab25..85f3a2686cc7 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -45,9 +45,9 @@ fi setup_ios() { select_device_ios - sleep 5 + sleep 30 info "Installing certificates on iOS simulator" - xcrun simctl keychain booted add-cert ./config/webpack/certificate.pem + xcrun simctl keychain booted add-root-cert "$(mkcert -CAROOT)/rootCA.pem" kill_all_emulators_ios } From 75854f578f1765b0499e7e3aa4a86763825a8724 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 12 Oct 2023 07:57:35 +0530 Subject: [PATCH 7/8] adding android script --- package.json | 1 + scripts/select-device.sh | 22 ++++++++++++++++++++++ scripts/setup-newdot-web-emulators.sh | 22 ---------------------- scripts/start-android.sh | 11 +++++++++++ 4 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 scripts/start-android.sh diff --git a/package.json b/package.json index 26937ea33741..3edc5c7e8321 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "scripts": { "configure-mapbox": "scripts/setup-mapbox-sdk-walkthrough.sh", "setupNewDotWebForEmulators": "scripts/setup-newdot-web-emulators.sh", + "startAndroidEmulator": "scripts/start-android.sh", "postinstall": "scripts/postInstall.sh", "clean": "npx react-native clean-project-auto", "android": "scripts/set-pusher-suffix.sh && npx react-native run-android --variant=developmentDebug --appId=com.expensify.chat.dev", diff --git a/scripts/select-device.sh b/scripts/select-device.sh index d83709d927ab..a53a6034d3da 100755 --- a/scripts/select-device.sh +++ b/scripts/select-device.sh @@ -68,6 +68,28 @@ kill_all_emulators_ios() { killall Simulator } +restart_adb_server() { + info "Restarting adb ..." + adb kill-server + sleep 2 + adb start-server + sleep 2 + info "Restarting adb done" +} + +ensure_device_available() { + # Must turn off exit on error temporarily + set +e + if adb devices | grep -q offline; then + restart_adb_server + if adb devices | grep -q offline; then + error "Device remains 'offline'. Please investigate!" + exit 1 + fi + fi + set -e +} + select_device_android() { # shellcheck disable=SC2124 diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 85f3a2686cc7..88598dd2e692 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -51,28 +51,6 @@ setup_ios() kill_all_emulators_ios } -restart_adb_server() { - info "Restarting adb ..." - adb kill-server - sleep 2 - adb start-server - sleep 2 - info "Restarting adb done" -} - -ensure_device_available() { - # Must turn off exit on error temporarily - set +e - if adb devices | grep -q offline; then - restart_adb_server - if adb devices | grep -q offline; then - error "Device remains 'offline'. Please investigate!" - exit 1 - fi - fi - set -e -} - setup_android_path_1() { adb root || { diff --git a/scripts/start-android.sh b/scripts/start-android.sh new file mode 100644 index 000000000000..b9a4e08a07a2 --- /dev/null +++ b/scripts/start-android.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Script for starting android emulator correctly + +# Use functions to select and open the emulator for iOS and Android +source scripts/select-device.sh + +select_device_android +sleep 30 +ensure_device_available +adb reverse tcp:8082 tcp:8082 \ No newline at end of file From b2d504ef33ce13a4130c854874551ac54bdf9488 Mon Sep 17 00:00:00 2001 From: Shubham Agrawal Date: Thu, 12 Oct 2023 08:00:14 +0530 Subject: [PATCH 8/8] fix android script --- scripts/setup-newdot-web-emulators.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup-newdot-web-emulators.sh b/scripts/setup-newdot-web-emulators.sh index 88598dd2e692..a0ed1422d2a9 100755 --- a/scripts/setup-newdot-web-emulators.sh +++ b/scripts/setup-newdot-web-emulators.sh @@ -61,7 +61,7 @@ setup_android_path_1() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts - # kill_all_emulators_android + kill_all_emulators_android } setup_android_path_2() @@ -80,7 +80,7 @@ setup_android_path_2() sleep 2 adb remount adb push /etc/hosts /system/etc/hosts - # kill_all_emulators_android + kill_all_emulators_android } setup_android()