#!/bin/bash set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" if ! command -v xcodebuild >/dev/null 2>&1; then echo "Xcode is required. Install it from the Mac App Store and open it once." >&2 exit 1 fi simulator_device="${OLDFELLA_SIMULATOR_DEVICE:-iPhone 18 Pro}" simulator_os="${OLDFELLA_SIMULATOR_OS:-27.2}" app_bundle_id=industries.geesawra.oldfella first_simulator() { xcrun simctl list devices available -j | OLDFELLA_SIMULATOR_DEVICE="$simulator_device" OLDFELLA_SIMULATOR_OS="$simulator_os" python3 -c ' import json, os, sys name = os.environ["OLDFELLA_SIMULATOR_DEVICE"] os_version = os.environ["OLDFELLA_SIMULATOR_OS"].replace(".", "-") devices = json.load(sys.stdin)["devices"] expected_runtime = "iOS-" + os_version for runtime, group in devices.items(): if not runtime.endswith(expected_runtime): continue for device in group: if device["isAvailable"] and device["name"] == name: print(device["udid"]) raise SystemExit(0) version = os.environ["OLDFELLA_SIMULATOR_OS"] raise SystemExit("No available " + name + " simulator with iOS " + version + " was found. Install that simulator runtime in Xcode.") ' } connected_devices() { xcrun devicectl list devices --quiet --json-output - | python3 -c ' import json, sys mode, requested = sys.argv[1:] devices = [] for device in json.load(sys.stdin)["result"]["devices"]: properties = device.get("properties", {}) hardware = properties.get("hardware", device.get("hardwareProperties", {})) connection = properties.get("connection", {}) if (hardware.get("platform") == "iOS" and hardware.get("reality") == "physical" and (connection.get("state") == "connected" or connection.get("pairingState") == "paired") and hardware.get("udid")): name = properties.get("state", device.get("deviceProperties", {})).get("name", "Unknown device") devices.append((name, hardware["udid"])) if mode == "list": if not devices: print("No connected physical iOS devices were found.") for name, uid in devices: print(f"{name}\t{uid}") elif mode == "all": if not devices: raise SystemExit("No connected physical iOS device was found. Connect and unlock a device, then retry.") for _, uid in devices: print(uid) elif requested: if not any(uid.lower() == requested.lower() for _, uid in devices): raise SystemExit("Device UID not found among connected physical iOS devices. Run: just devices") print(next(uid for _, uid in devices if uid.lower() == requested.lower())) elif len(devices) == 1: print(devices[0][1]) elif not devices: raise SystemExit("No connected physical iOS device was found. Connect and unlock a device, then retry.") else: raise SystemExit("Multiple devices are connected. Run: just devices\nThen pass a UID to just device or just device-release.") ' "$1" "${2:-}" } if [ "${1:-}" = devices ]; then connected_devices list exit fi if ! command -v xcodegen >/dev/null 2>&1; then echo "XcodeGen is required. Run: brew bundle install --file Brewfile" >&2 exit 1 fi xcodegen generate ./scripts/write-version.sh app_path() { local destination="$1" local configuration="${2:-Debug}" xcodebuild -scheme OldFella -target OldFella -configuration "$configuration" -destination "$destination" -showBuildSettings 2>/dev/null | awk -F ' = ' '/ TARGET_BUILD_DIR = / {dir=$2} / FULL_PRODUCT_NAME = / {product=$2} END {print dir "/" product}' } install_release() { local device_id="$1" local destination="platform=iOS,id=$device_id" local configuration=DeviceRelease local app installed_id xcodebuild -scheme OldFella -configuration "$configuration" -destination "$destination" -allowProvisioningUpdates build || return 1 app="$(app_path "$destination" "$configuration")" || return 1 installed_id="$(plutil -extract CFBundleIdentifier raw "$app/Info.plist")" || return 1 if [ "$installed_id" != "$app_bundle_id.dev" ]; then echo "Expected the Old Fella Dev bundle ID. Found $installed_id." >&2 return 1 fi xcrun devicectl device install app --device "$device_id" "$app" || return 1 echo "Installed Old Fella Dev on $device_id." } case "${1:-}" in build) xcodebuild -scheme OldFella -destination 'generic/platform=iOS Simulator' build ;; sim) DEVICE_ID="$(first_simulator)" DESTINATION="platform=iOS Simulator,id=$DEVICE_ID" DEVICE_HUB_APP="$(xcode-select -p)/../Applications/DeviceHub.app" if [ -x "$DEVICE_HUB_APP/Contents/MacOS/DeviceHub" ]; then open "$DEVICE_HUB_APP" >/dev/null 2>&1 || true fi xcodebuild -scheme OldFella -destination "$DESTINATION" build xcrun simctl boot "$DEVICE_ID" 2>/dev/null || true xcrun simctl bootstatus "$DEVICE_ID" -b APP="$(app_path "$DESTINATION")" xcrun simctl install "$DEVICE_ID" "$APP" xcrun simctl launch --terminate-running-process "$DEVICE_ID" "$app_bundle_id" echo "Old Fella is running on $simulator_device with iOS $simulator_os in Device Hub." ;; device) DEVICE_ID="$(connected_devices select "${2:-}")" DESTINATION="platform=iOS,id=$DEVICE_ID" xcodebuild -scheme OldFella -destination "$DESTINATION" -allowProvisioningUpdates build APP="$(app_path "$DESTINATION")" xcrun devicectl device install app --device "$DEVICE_ID" "$APP" xcrun devicectl device process launch --device "$DEVICE_ID" "$app_bundle_id" ;; device-release|profile) duration="${3:-40}" profile_template="${OLDFELLA_PROFILE_TEMPLATE:-Time Profiler}" if [ "$1" = profile ]; then if [[ ! "$duration" =~ ^[0-9]{1,3}$ ]] || [ "$duration" -lt 5 ] || [ "$duration" -gt 300 ]; then echo "The recording duration must be a whole number from 5 to 300 seconds." >&2 exit 2 fi if [ "$profile_template" != "Animation Hitches" ] && [ "$profile_template" != "Time Profiler" ] && [ "$profile_template" != SwiftUI ]; then echo "OLDFELLA_PROFILE_TEMPLATE must be Animation Hitches, Time Profiler, or SwiftUI." >&2 exit 2 fi if [ "${OLDFELLA_PROFILE_FEED:-live}" != live ] && [ "${OLDFELLA_PROFILE_FEED:-live}" != rich ]; then echo "OLDFELLA_PROFILE_FEED must be live or rich." >&2 exit 2 fi xcrun --find xctrace >/dev/null 2>&1 || { echo "Instruments is unavailable. Select the full Xcode installation with xcode-select, then retry." >&2 exit 1 } fi if [ "$1" = profile ]; then DEVICE_ID="$(connected_devices select "${2:-}")" DESTINATION="platform=iOS,id=$DEVICE_ID" xcodebuild -scheme OldFellaScrollProfile -configuration Release -destination "$DESTINATION" \ -allowProvisioningUpdates -only-testing:OldFellaUITests/DeviceScrollProfile build-for-testing python3 scripts/profile-scroll.py "$DEVICE_ID" "$duration" "$profile_template" exit fi if [ "${2:-}" = all ]; then DEVICE_IDS="$(connected_devices all)" failed=0 while IFS= read -r DEVICE_ID; do if ! install_release "$DEVICE_ID"; then echo "Failed to install Old Fella Dev on $DEVICE_ID." >&2 failed=1 fi done <<< "$DEVICE_IDS" exit "$failed" fi DEVICE_ID="$(connected_devices select "${2:-}")" install_release "$DEVICE_ID" ;; test|test-agent) if [ -n "${OLDFELLA_TEST_DEVICE_UID:-}" ]; then DEVICE_ID="$(connected_devices select "$OLDFELLA_TEST_DEVICE_UID")" test_destination="platform=iOS,id=$DEVICE_ID" else DEVICE_ID="$(first_simulator)" test_destination="platform=iOS Simulator,id=$DEVICE_ID" fi selector="${2:-}" test_configuration="${OLDFELLA_TEST_CONFIGURATION:-Debug}" if [ "$test_configuration" != Debug ] && [ "$test_configuration" != Release ]; then echo "OLDFELLA_TEST_CONFIGURATION must be Debug or Release." >&2 exit 2 fi test_arguments=(-scheme OldFella -configuration "$test_configuration" ENABLE_TESTABILITY=YES -destination "$test_destination") if [ -n "${OLDFELLA_TEST_DEVICE_UID:-}" ]; then test_arguments+=(-allowProvisioningUpdates) fi if [ -n "$selector" ]; then test_arguments+=("-only-testing:$selector") fi xcodebuild "${test_arguments[@]}" test ;; *) echo "Usage: $0 {build|sim|devices|device [uid]|device-release [uid|all]|profile [uid] [seconds]|test [selector]|test-agent [selector]}" >&2 exit 2 ;; esac