Skip to content

Commit

Permalink
chore: move metro functions to lib/metro.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
siddarthkay committed Feb 17, 2024
1 parent f9c4935 commit 1d91919
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 87 deletions.
33 changes: 33 additions & 0 deletions scripts/lib/metro.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail

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.
}
31 changes: 2 additions & 29 deletions scripts/run-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,6 @@ 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.
}

# Generate android debug build.
export ANDROID_ABI_INCLUDE=$("${GIT_ROOT}/scripts/adb_devices_abis.sh")
export BUILD_ENV=debug
Expand All @@ -42,6 +13,8 @@ export BUILD_TYPE=debug
# Install the APK on running emulator or android device.
adb install ./result/app-debug.apk

source "${GIT_ROOT}/scripts/lib/metro.sh"

trap cleanupMetro EXIT ERR INT QUIT
runMetro &
waitForMetro
Expand Down
31 changes: 2 additions & 29 deletions scripts/run-ios-device.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,6 @@ 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.
}

# find the first connected iPhone's UUID
DEVICE_UUID=$(idevice_id -l)

Expand Down Expand Up @@ -64,6 +35,8 @@ xcrun devicectl device process launch --no-activate --verbose --device "$DEVICE_
# Extract background PID of status app
STATUS_PID=$(jq -r '.result.process.processIdentifier' "$XCRUN_LOG_DIR")

source "${GIT_ROOT}/scripts/lib/metro.sh"

trap cleanupMetro EXIT ERR INT QUIT
runMetro &
waitForMetro
Expand Down
31 changes: 2 additions & 29 deletions scripts/run-ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,6 @@ 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.
}

# Check if the first argument is provided
if [ -z "${1-}" ]; then
echo "Error: No simulator name provided." >&2
Expand Down Expand Up @@ -68,6 +39,8 @@ APP_PATH="${BUILD_DIR}/Build/Products/Debug-iphonesimulator/StatusIm.app"
# Install on the simulator
xcrun simctl install "$UUID" "$APP_PATH"

source "${GIT_ROOT}/scripts/lib/metro.sh"

trap cleanupMetro EXIT ERR INT QUIT
runMetro &
waitForMetro
Expand Down

0 comments on commit 1d91919

Please sign in to comment.