Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

prefer booted simulator in darwin scripts

Prateek Sunal1368c40bda28
diff --git a/mobile/native/darwin/Apps/ensu/README.md b/mobile/native/darwin/Apps/ensu/README.md
index 4fd761f6d4..761ee3aa94 100644
--- a/mobile/native/darwin/Apps/ensu/README.md
+++ b/mobile/native/darwin/Apps/ensu/README.md
@@ -7,10 +7,10 @@ All commands below assume you run them from `darwin/Apps/ensu`.
## Quick scripts
```bash
-./build.sh # Debug build for iOS simulator
+./build.sh # Debug build for iOS simulator (prefers booted iPhone)
./build.sh device # Debug build for connected iOS device
./build.sh archive # Release archive
-./run.sh # Build + install + launch on iOS simulator
+./run.sh # Build + install + launch on iOS simulator (prefers booted iPhone)
```
Helpful flags:
diff --git a/mobile/native/darwin/Apps/ensu/build.sh b/mobile/native/darwin/Apps/ensu/build.sh
index 9806eadf91..bb2cc7829f 100755
--- a/mobile/native/darwin/Apps/ensu/build.sh
+++ b/mobile/native/darwin/Apps/ensu/build.sh
@@ -18,7 +18,7 @@ Usage:
./build.sh [sim|device|archive] [options]
Modes:
- sim Debug build for iOS simulator (default)
+ sim Debug build for iOS simulator (default, prefers booted iPhone)
device Debug build for connected iOS device
archive Release archive for App Store/TestFlight flow
@@ -35,12 +35,74 @@ Examples:
EOF
}
+pick_booted_simulator_id() {
+ if ! command -v python3 >/dev/null 2>&1; then
+ return
+ fi
+
+ python3 - <<'PY' 2>/dev/null
+import json
+import subprocess
+import sys
+
+try:
+ out = subprocess.check_output(
+ ["xcrun", "simctl", "list", "devices", "booted", "--json"],
+ stderr=subprocess.DEVNULL,
+ text=True,
+ )
+except Exception:
+ sys.exit(0)
+
+try:
+ data = json.loads(out)
+except Exception:
+ sys.exit(0)
+
+booted = []
+for runtime_devices in data.get("devices", {}).values():
+ for device in runtime_devices:
+ if device.get("state") != "Booted":
+ continue
+ if device.get("isAvailable") is False:
+ continue
+ name = device.get("name", "")
+ udid = device.get("udid", "")
+ if not udid:
+ continue
+ booted.append((name, udid))
+
+if not booted:
+ sys.exit(0)
+
+booted.sort(key=lambda item: (0 if "iphone" in item[0].lower() else 1, item[0]))
+print(booted[0][1])
+PY
+}
+
pick_destination_id() {
local platform="$1"
- xcodebuild -project "$PROJECT" -scheme "$SCHEME" -showdestinations 2>/dev/null \
- | sed -n "s/.*platform:${platform},.*id:\([^,}]*\).*/\1/p" \
- | grep -v '^dvtdevice-' \
- | head -n 1
+ local prefer_iphone="${2:-0}"
+ local destinations
+
+ destinations="$(xcodebuild -project "$PROJECT" -scheme "$SCHEME" -showdestinations 2>/dev/null \
+ | sed -n "s/.*platform:${platform},.*id:\([^,}]*\).*, name:\([^}]*\).*/\1|\2/p" \
+ | grep -v '^dvtdevice-' || true)"
+
+ if [[ -z "$destinations" ]]; then
+ return
+ fi
+
+ if [[ "$prefer_iphone" == "1" ]]; then
+ local iphone_id
+ iphone_id="$(printf "%s\n" "$destinations" | awk -F'|' '/iPhone/ { print $1; exit }')"
+ if [[ -n "$iphone_id" ]]; then
+ echo "$iphone_id"
+ return
+ fi
+ fi
+
+ printf "%s\n" "$destinations" | head -n 1 | cut -d'|' -f1
}
while [[ $# -gt 0 ]]; do
@@ -86,7 +148,10 @@ fi
case "$MODE" in
sim)
if [[ -z "$DESTINATION_ID" ]]; then
- DESTINATION_ID="$(pick_destination_id 'iOS Simulator')"
+ DESTINATION_ID="$(pick_booted_simulator_id)"
+ fi
+ if [[ -z "$DESTINATION_ID" ]]; then
+ DESTINATION_ID="$(pick_destination_id 'iOS Simulator' 1)"
fi
if [[ -z "$DESTINATION_ID" ]]; then
echo "No iOS Simulator destination found. Open Simulator.app once and retry." >&2
@@ -104,7 +169,7 @@ case "$MODE" in
;;
device)
if [[ -z "$DESTINATION_ID" ]]; then
- DESTINATION_ID="$(pick_destination_id 'iOS')"
+ DESTINATION_ID="$(pick_destination_id 'iOS' 1)"
fi
if [[ -z "$DESTINATION_ID" ]]; then
echo "No connected iOS device found. Connect a device or pass --destination-id." >&2
diff --git a/mobile/native/darwin/Apps/ensu/run.sh b/mobile/native/darwin/Apps/ensu/run.sh
index 539b3c03a1..930de6aedb 100755
--- a/mobile/native/darwin/Apps/ensu/run.sh
+++ b/mobile/native/darwin/Apps/ensu/run.sh
@@ -23,16 +23,75 @@ Options:
-h, --help Show help
Examples:
- ./run.sh
+ ./run.sh # prefers currently booted iPhone simulator
./run.sh --destination-id <SIMULATOR_UUID>
EOF
}
+pick_booted_simulator_id() {
+ if ! command -v python3 >/dev/null 2>&1; then
+ return
+ fi
+
+ python3 - <<'PY' 2>/dev/null
+import json
+import subprocess
+import sys
+
+try:
+ out = subprocess.check_output(
+ ["xcrun", "simctl", "list", "devices", "booted", "--json"],
+ stderr=subprocess.DEVNULL,
+ text=True,
+ )
+except Exception:
+ sys.exit(0)
+
+try:
+ data = json.loads(out)
+except Exception:
+ sys.exit(0)
+
+booted = []
+for runtime_devices in data.get("devices", {}).values():
+ for device in runtime_devices:
+ if device.get("state") != "Booted":
+ continue
+ if device.get("isAvailable") is False:
+ continue
+ name = device.get("name", "")
+ udid = device.get("udid", "")
+ if not udid:
+ continue
+ booted.append((name, udid))
+
+if not booted:
+ sys.exit(0)
+
+booted.sort(key=lambda item: (0 if "iphone" in item[0].lower() else 1, item[0]))
+print(booted[0][1])
+PY
+}
+
pick_simulator_id() {
- xcodebuild -project ensu.xcodeproj -scheme ensu -showdestinations 2>/dev/null \
- | sed -n 's/.*platform:iOS Simulator,.*id:\([^,}]*\).*/\1/p' \
- | grep -v '^dvtdevice-' \
- | head -n 1
+ local destinations
+
+ destinations="$(xcodebuild -project ensu.xcodeproj -scheme ensu -showdestinations 2>/dev/null \
+ | sed -n 's/.*platform:iOS Simulator,.*id:\([^,}]*\).*, name:\([^}]*\).*/\1|\2/p' \
+ | grep -v '^dvtdevice-' || true)"
+
+ if [[ -z "$destinations" ]]; then
+ return
+ fi
+
+ local iphone_id
+ iphone_id="$(printf "%s\n" "$destinations" | awk -F'|' '/iPhone/ { print $1; exit }')"
+ if [[ -n "$iphone_id" ]]; then
+ echo "$iphone_id"
+ return
+ fi
+
+ printf "%s\n" "$destinations" | head -n 1 | cut -d'|' -f1
}
while [[ $# -gt 0 ]]; do
@@ -71,6 +130,9 @@ done
cd "$ROOT"
+if [[ -z "$DESTINATION_ID" ]]; then
+ DESTINATION_ID="$(pick_booted_simulator_id)"
+fi
if [[ -z "$DESTINATION_ID" ]]; then
DESTINATION_ID="$(pick_simulator_id)"
fi