← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Scripts/deploy-device.sh (3133B)

1#!/bin/bash
2# Builds Reportage in Debug and installs it on a paired iPhone, over the local
3# network if the iPhone is on the same Wi-Fi (or over USB if it is plugged in).
4#
5# Usage: bash Scripts/deploy-device.sh [--skip-build] [--no-launch] <device>
6#
7# The device is a name, UDID or identifier as `xcrun devicectl list devices`
8# shows it, and is required so a build never lands on the wrong iPhone. The
9# iPhone must have been paired with Xcode, with Developer Mode on.
10#
11# Signing uses a temporary keychain holding .asc/dev.p12, not the login
12# keychain, so a locked login keychain or a missed prompt can't fail it.
13set -euo pipefail
14
15cd "$(dirname "${BASH_SOURCE[0]}")/.."
16source Scripts/signing-keychain.sh
17
18SKIP_BUILD=false
19LAUNCH=true
20DEVICE=""
21for argument in "$@"; do
22  case "$argument" in
23    --skip-build) SKIP_BUILD=true ;;
24    --no-launch) LAUNCH=false ;;
25    *) DEVICE="$argument" ;;
26  esac
27done
28
29if [[ -z "$DEVICE" ]]; then
30  echo "Usage: bash Scripts/deploy-device.sh [--skip-build] [--no-launch] <device>" >&2
31  echo "Name the device as 'xcrun devicectl list devices' shows it." >&2
32  exit 1
33fi
34
35BUNDLE_ID="net.inqk.reportage"
36DERIVED_DATA="$(pwd)/.cache/device"
37APP="${DERIVED_DATA}/Build/Products/Debug-iphoneos/Reportage.app"
38
39# Resolves the device to its UDID, which both xcodebuild and devicectl accept.
40DEVICES_JSON="$(mktemp)"
41cleanup() {
42  rm -f "$DEVICES_JSON"
43  restore_signing_keychain
44}
45trap cleanup EXIT
46xcrun devicectl list devices --json-output "$DEVICES_JSON" >/dev/null
47read -r UDID NAME < <(python3 - "$DEVICES_JSON" "$DEVICE" <<'EOF'
48import json, sys
49
50devices = json.load(open(sys.argv[1]))["result"]["devices"]
51wanted = sys.argv[2]
52for device in devices:
53    hardware = device["hardwareProperties"]
54    name = device["deviceProperties"]["name"]
55    if hardware.get("reality") != "physical":
56        continue
57    if wanted not in (name, hardware.get("udid"), device["identifier"]):
58        continue
59    print(hardware["udid"], name)
60    break
61EOF
62)
63
64if [[ -z "${UDID:-}" ]]; then
65  echo "No device matching '${DEVICE}'; see 'xcrun devicectl list devices'." >&2
66  exit 1
67fi
68
69if [[ "$SKIP_BUILD" == false ]]; then
70  SECRETS=".asc/secrets.sh"
71  if [[ ! -f "$SECRETS" ]]; then
72    echo "Error: $SECRETS not found. Copy .asc/ from another app; it holds team-wide signing files." >&2
73    exit 1
74  fi
75  source "$SECRETS"
76  TMP_KEYCHAIN="$(pwd)/.asc/build.keychain-db"
77  signing_keychain_begin
78  signing_keychain_import ".asc/dev.p12" "$DEV_P12_PASS"
79  signing_keychain_activate
80
81  echo "==> Building for ${NAME}"
82  xcodebuild build \
83    -scheme "Reportage" \
84    -project "Reportage.xcodeproj" \
85    -configuration Debug \
86    -destination "id=${UDID}" \
87    -derivedDataPath "$DERIVED_DATA" \
88    -allowProvisioningUpdates \
89    -quiet
90fi
91
92if [[ ! -d "$APP" ]]; then
93  echo "No app at ${APP}; run without --skip-build first." >&2
94  exit 1
95fi
96
97echo "==> Installing on ${NAME}"
98xcrun devicectl device install app --device "$UDID" "$APP" --quiet
99
100if [[ "$LAUNCH" == true ]]; then
101  echo "==> Launching"
102  xcrun devicectl device process launch --device "$UDID" --terminate-existing "$BUNDLE_ID" --quiet
103fi