Open History to see the last commit
1#!/usr/bin/env bash
2
3set -euo pipefail
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
7DEVICE_ID="${T3_SWIFT_DEVICE_ID:-${1:-}}"
8DEVELOPMENT_TEAM="${T3_SWIFT_DEVELOPMENT_TEAM:-${2:-}}"
9CONFIGURATION="${T3_SWIFT_CONFIGURATION:-Debug}"
10DERIVED_DATA_PATH="${T3_SWIFT_DERIVED_DATA_PATH:-${APP_DIR}/.derivedData/device}"
11
12die() {
13 printf '[swift-ios-device] error: %s\n' "$*" >&2
14 exit 1
15}
16
17require_cmd() {
18 command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
19}
20
21require_cmd awk
22require_cmd mktemp
23require_cmd plutil
24require_cmd xcodebuild
25require_cmd xcrun
26
27[[ "${CONFIGURATION}" == "Debug" || "${CONFIGURATION}" == "Release" ]] || die \
28 "T3_SWIFT_CONFIGURATION must be Debug or Release"
29
30if [[ "${CONFIGURATION}" == "Debug" ]]; then
31 BUNDLE_IDENTIFIER="com.t3tools.t3code.swiftui.dev"
32else
33 BUNDLE_IDENTIFIER="com.t3tools.t3code.swiftui"
34fi
35WIDGET_BUNDLE_IDENTIFIER="${BUNDLE_IDENTIFIER}.widgets"
36SHARE_BUNDLE_IDENTIFIER="${BUNDLE_IDENTIFIER}.sharing"
37
38[[ -z "${T3_SWIFT_BUNDLE_IDENTIFIER:-}" || "${T3_SWIFT_BUNDLE_IDENTIFIER}" == "${BUNDLE_IDENTIFIER}" ]] || die \
39 "custom bundle identifiers are unsupported; select the Debug or Release identity with T3_SWIFT_CONFIGURATION"
40
41bundle_identifier_for_target() {
42 local target="$1"
43 xcodebuild -showBuildSettings \
44 -project "${APP_DIR}/T3Code.xcodeproj" \
45 -target "${target}" \
46 -configuration "${CONFIGURATION}" \
47 | awk '$1 == "PRODUCT_BUNDLE_IDENTIFIER" && $2 == "=" { print $3; exit }'
48}
49
50verify_bundle_identifiers() {
51 local host widgets share
52 host="$(bundle_identifier_for_target T3Code)"
53 widgets="$(bundle_identifier_for_target T3CodeWidgets)"
54 share="$(bundle_identifier_for_target T3CodeShare)"
55 [[ "${host}" == "${BUNDLE_IDENTIFIER}" ]] || die \
56 "host bundle identifier resolved to '${host}'"
57 [[ "${widgets}" == "${WIDGET_BUNDLE_IDENTIFIER}" ]] || die \
58 "widget bundle identifier resolved to '${widgets}'"
59 [[ "${share}" == "${SHARE_BUNDLE_IDENTIFIER}" ]] || die \
60 "share bundle identifier resolved to '${share}'"
61 printf '[swift-ios-device] bundle identifiers: %s, %s, %s\n' \
62 "${host}" "${widgets}" "${share}"
63}
64
65if [[ "${T3_SWIFT_VERIFY_BUNDLE_IDENTIFIERS_ONLY:-0}" == "1" ]]; then
66 verify_bundle_identifiers
67 exit 0
68fi
69
70[[ -n "${DEVICE_ID}" ]] || die \
71 "set T3_SWIFT_DEVICE_ID to a CoreDevice identifier or UDID from 'xcrun devicectl list devices --columns UDID'"
72[[ -n "${DEVELOPMENT_TEAM}" ]] || die \
73 "set T3_SWIFT_DEVELOPMENT_TEAM to your Apple Developer team ID"
74
75build_settings=(
76 "DEVELOPMENT_TEAM=${DEVELOPMENT_TEAM}"
77)
78
79DEVICE_JSON="$(mktemp -t t3-swift-devices.XXXXXX)"
80trap 'unlink "${DEVICE_JSON}" 2>/dev/null || true' EXIT
81xcrun devicectl list devices --json-output "${DEVICE_JSON}" --quiet >/dev/null
82DESTINATION_ID="$(
83 xcrun swift "${SCRIPT_DIR}/resolve-device-udid.swift" "${DEVICE_JSON}" "${DEVICE_ID}"
84)" || die "could not resolve device '${DEVICE_ID}' to an Xcode destination UDID"
85
86for key in \
87 T3CODE_CLERK_PUBLISHABLE_KEY \
88 T3CODE_CLERK_JWT_TEMPLATE \
89 T3CODE_RELAY_URL; do
90 value="${!key:-}"
91 if [[ -n "${value}" ]]; then
92 build_settings+=("${key}=${value}")
93 fi
94done
95
96if [[ -n "${T3_SWIFT_VERSION:-}" ]]; then
97 build_settings+=("MARKETING_VERSION=${T3_SWIFT_VERSION}")
98fi
99if [[ -n "${T3_SWIFT_BUILD_NUMBER:-}" ]]; then
100 build_settings+=("CURRENT_PROJECT_VERSION=${T3_SWIFT_BUILD_NUMBER}")
101fi
102
103printf '[swift-ios-device] building %s for %s\n' "${BUNDLE_IDENTIFIER}" "${DESTINATION_ID}"
104xcodebuild build \
105 -project "${APP_DIR}/T3Code.xcodeproj" \
106 -scheme T3Code \
107 -configuration "${CONFIGURATION}" \
108 -destination "platform=iOS,id=${DESTINATION_ID}" \
109 -derivedDataPath "${DERIVED_DATA_PATH}" \
110 -allowProvisioningUpdates \
111 -allowProvisioningDeviceRegistration \
112 "${build_settings[@]}"
113
114APP_PATH="${DERIVED_DATA_PATH}/Build/Products/${CONFIGURATION}-iphoneos/T3Code.app"
115[[ -d "${APP_PATH}" ]] || die "built app was not found at ${APP_PATH}"
116
117actual_host="$(plutil -extract CFBundleIdentifier raw -o - "${APP_PATH}/Info.plist")"
118actual_widgets="$(
119 plutil -extract CFBundleIdentifier raw -o - \
120 "${APP_PATH}/PlugIns/T3CodeWidgets.appex/Info.plist"
121)"
122actual_share="$(
123 plutil -extract CFBundleIdentifier raw -o - \
124 "${APP_PATH}/PlugIns/T3CodeShare.appex/Info.plist"
125)"
126[[ "${actual_host}" == "${BUNDLE_IDENTIFIER}" ]] || die \
127 "built host bundle identifier is '${actual_host}'"
128[[ "${actual_widgets}" == "${WIDGET_BUNDLE_IDENTIFIER}" ]] || die \
129 "built widget bundle identifier is '${actual_widgets}'"
130[[ "${actual_share}" == "${SHARE_BUNDLE_IDENTIFIER}" ]] || die \
131 "built share bundle identifier is '${actual_share}'"
132
133printf '[swift-ios-device] installing %s\n' "${APP_PATH}"
134xcrun devicectl device install app --device "${DESTINATION_ID}" "${APP_PATH}"
135
136printf '[swift-ios-device] launching %s\n' "${BUNDLE_IDENTIFIER}"
137if ! launch_output="$(
138 xcrun devicectl device process launch \
139 --device "${DESTINATION_ID}" \
140 "${BUNDLE_IDENTIFIER}" 2>&1
141)"; then
142 printf '%s\n' "${launch_output}" >&2
143 if [[ "${launch_output}" == *"BSErrorCodeDescription = Locked"* ]]; then
144 printf '[swift-ios-device] installed; unlock the device to launch the app\n'
145 exit 0
146 fi
147 exit 1
148fi
149printf '%s\n' "${launch_output}"