Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a bash script needed to configure simulators for App development #29061

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"private": true,
"scripts": {
"configure-mapbox": "scripts/setup-mapbox-sdk-walkthrough.sh",
"setupNewDotWebForEmulators": "scripts/setup-newdot-web-emulators.sh",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these supposed to be a part of prebuild? or atleast added to the README? @hayata-suenaga ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think README.md will be updated here
#28422

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1206agra can you check the current README change in that PR and put a comment in that PR on additional instruction we need to add because of these new scripts if there are any?

"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",
Expand Down Expand Up @@ -114,8 +116,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",
Expand Down
133 changes: 133 additions & 0 deletions scripts/select-device.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/bin/bash

shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
# 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()
{
# shellcheck disable=SC2124
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"
}

kill_all_emulators_ios() {
# kill all the emulators
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
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_identifier" -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 &
}

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
}
2 changes: 1 addition & 1 deletion scripts/setup-mapbox-sdk-walkthrough.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/setup-mapbox-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
113 changes: 113 additions & 0 deletions scripts/setup-newdot-web-emulators.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/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 variables 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 30
info "Installing certificates on iOS simulator"
xcrun simctl keychain booted add-root-cert "$(mkcert -CAROOT)/rootCA.pem"
kill_all_emulators_ios
}

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
kill_all_emulators_android
}

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we come up with these numbers like 2 seconds, 30 seconds? Is there a way to avoid so many sleep commands?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope
You can't reduce the number of sleep command unfortunately

ensure_device_available
adb root
sleep 2
adb remount
adb push /etc/hosts /system/etc/hosts
kill_all_emulators_android
}

setup_android()
{
select_device_android
sleep 30
ensure_device_available
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why exit if ios fails? especially if $platform == 'all' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that the user knows immediately that the flow failed. Instead of thinking that the flow passed.

}
fi

if [ "$platform" = "android" ] || [ "$platform" = "all" ]; then
setup_android || {
error "Failed to setup Android emulator"
exit 1
}
fi

success "Done!"
29 changes: 24 additions & 5 deletions scripts/shellUtils.sh
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause this is causing errors
scripts/shellUtils.sh: line 3: declare: GREEN: readonly variable

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"
Expand Down
11 changes: 11 additions & 0 deletions scripts/start-android.sh
Original file line number Diff line number Diff line change
@@ -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
Loading