Repository navigation
Expand file tree
/
Copy pathbuild-xcuitest-apple.sh
More file actions
200 lines (184 loc) · 6.64 KB
/
Copy pathbuild-xcuitest-apple.sh
File metadata and controls
200 lines (184 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/sh
set -eu
PLATFORM="${AGENT_DEVICE_XCUITEST_PLATFORM:-}"
PROJECT_PATH="apple/runner/AgentDeviceRunner/AgentDeviceRunner.xcodeproj"
SCHEME="AgentDeviceRunner"
DEFAULT_IOS_RUNNER_APP_BUNDLE_ID="com.callstack.agentdevice.runner"
if [ -z "$PLATFORM" ]; then
echo "AGENT_DEVICE_XCUITEST_PLATFORM is required (ios, macos, tvos, visionos)" >&2
exit 1
fi
is_truthy() {
case "${1:-}" in
1|true|TRUE|yes|YES|on|ON)
return 0
;;
*)
return 1
;;
esac
}
resolve_default_destination() {
case "$PLATFORM" in
ios)
resolve_simulator_destination 'iOS' 'iPhone' || printf '%s\n' 'generic/platform=iOS Simulator'
;;
macos)
printf 'platform=macOS,arch=%s\n' "$(uname -m)"
;;
tvos)
resolve_simulator_destination 'tvOS' 'Apple TV' || printf '%s\n' 'generic/platform=tvOS Simulator'
;;
visionos)
resolve_simulator_destination 'visionOS' 'Apple Vision' || printf '%s\n' 'generic/platform=visionOS Simulator'
;;
*)
echo "Unsupported AGENT_DEVICE_XCUITEST_PLATFORM: $PLATFORM" >&2
exit 1
;;
esac
}
resolve_simulator_destination() {
command -v node >/dev/null 2>&1 || return 1
node -e '
const { execFileSync } = require("node:child_process");
const platformName = process.argv[1];
const deviceNamePattern = new RegExp(process.argv[2]);
const platformNameLower = platformName.toLowerCase();
try {
const output = execFileSync("xcrun", ["simctl", "list", "devices", "available", "-j"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 3000,
});
const parsed = JSON.parse(output);
const devices = Object.entries(parsed.devices ?? {})
.filter(([runtime]) => runtime.toLowerCase().includes(platformNameLower))
.flatMap(([, runtimeDevices]) => Array.isArray(runtimeDevices) ? runtimeDevices : [])
.filter(
(device) =>
device &&
device.isAvailable !== false &&
typeof device.udid === "string" &&
typeof device.name === "string" &&
deviceNamePattern.test(device.name),
);
const selected = devices.find((device) => device.state === "Booted") ?? devices[0];
if (!selected) process.exit(1);
console.log(`platform=${platformName} Simulator,id=${selected.udid}`);
} catch {
process.exit(1);
}
' "$1" "$2"
}
resolve_default_derived_path() {
case "$PLATFORM" in
ios)
printf '%s\n' "$HOME/.agent-device/apple-runner/derived"
;;
macos)
printf '%s\n' "$HOME/.agent-device/apple-runner/derived/macos"
;;
tvos)
printf '%s\n' "$HOME/.agent-device/apple-runner/derived/tvos"
;;
visionos)
printf '%s\n' "$HOME/.agent-device/apple-runner/derived/visionos"
;;
*)
echo "Unsupported AGENT_DEVICE_XCUITEST_PLATFORM: $PLATFORM" >&2
exit 1
;;
esac
}
resolve_clean_path() {
if [ -n "${AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH:-}" ]; then
printf '%s\n' "$DERIVED_PATH"
return
fi
case "$PLATFORM" in
ios)
printf '%s\n' "$DERIVED_PATH/device"
;;
macos|tvos|visionos)
printf '%s\n' "$DERIVED_PATH"
;;
*)
echo "Unsupported AGENT_DEVICE_XCUITEST_PLATFORM: $PLATFORM" >&2
exit 1
;;
esac
}
DESTINATION="${AGENT_DEVICE_XCUITEST_DESTINATION:-$(resolve_default_destination)}"
DERIVED_PATH="${AGENT_DEVICE_IOS_RUNNER_DERIVED_PATH:-$(resolve_default_derived_path)}"
CLEAN_PATH="$(resolve_clean_path)"
RUNNER_APP_BUNDLE_ID="${AGENT_DEVICE_IOS_BUNDLE_ID:-${AGENT_DEVICE_IOS_RUNNER_APP_BUNDLE_ID:-$DEFAULT_IOS_RUNNER_APP_BUNDLE_ID}}"
RUNNER_TEST_BUNDLE_ID="${AGENT_DEVICE_IOS_RUNNER_TEST_BUNDLE_ID:-$RUNNER_APP_BUNDLE_ID.uitests}"
SIGNING_BUILD_SETTINGS=""
if [ "$PLATFORM" = "macos" ]; then
SIGNING_BUILD_SETTINGS="CODE_SIGNING_ALLOWED=NO CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= DEVELOPMENT_TEAM="
fi
if is_truthy "${AGENT_DEVICE_IOS_CLEAN_DERIVED:-}"; then
rm -rf "$CLEAN_PATH"
fi
SWIFT_FLAGS='$(inherited) -disable-sandbox -D AGENT_DEVICE_RUNNER_ISOLATION_CANARY'
if is_truthy "${AGENT_DEVICE_XCUITEST_INCLUDE_UNIT_TESTS:-}"; then
SWIFT_FLAGS="$SWIFT_FLAGS -D AGENT_DEVICE_RUNNER_UNIT_TESTS"
fi
# Optional arch override. A generic simulator destination leaves the active arch
# undefined; Xcode versions differ on the default (26.6 picks x86_64, which runs
# under Rosetta on arm64 hosts). Set AGENT_DEVICE_XCUITEST_ARCHS=arm64 to pin it.
ARCH_BUILD_SETTINGS=""
if [ -n "${AGENT_DEVICE_XCUITEST_ARCHS:-}" ]; then
ARCH_BUILD_SETTINGS="ARCHS=$AGENT_DEVICE_XCUITEST_ARCHS"
fi
build_for_testing() {
node --experimental-strip-types scripts/swift-toolchain-tmpdir.ts xcodebuild build-for-testing \
-project "$PROJECT_PATH" \
-scheme "$SCHEME" \
-destination "$DESTINATION" \
-derivedDataPath "$DERIVED_PATH" \
AGENT_DEVICE_IOS_RUNNER_APP_BUNDLE_ID="$RUNNER_APP_BUNDLE_ID" \
AGENT_DEVICE_IOS_RUNNER_TEST_BUNDLE_ID="$RUNNER_TEST_BUNDLE_ID" \
COMPILER_INDEX_STORE_ENABLE=NO \
ENABLE_CODE_COVERAGE=NO \
ONLY_ACTIVE_ARCH=YES \
ENABLE_PREVIEWS=NO \
ENABLE_DEBUG_DYLIB=NO \
-IDEPackageSupportDisableManifestSandbox=1 \
-IDEPackageSupportDisablePluginExecutionSandbox=1 \
ENABLE_USER_SCRIPT_SANDBOXING=NO \
OTHER_SWIFT_FLAGS="$SWIFT_FLAGS" \
$ARCH_BUILD_SETTINGS \
$SIGNING_BUILD_SETTINGS
}
# The isolation scan reads the compiler diagnostics in the build log, and an incremental build
# prints them only for the files it recompiles. The scan's positive control must print on every
# build, so its source is always stale.
REUSED_DERIVED_DATA=0
if [ -d "$DERIVED_PATH/Build/Intermediates.noindex" ]; then
REUSED_DERIVED_DATA=1
fi
touch apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerIsolationCanary.swift
mkdir -p "$DERIVED_PATH/Logs"
BUILD_LOG="$DERIVED_PATH/Logs/agent-device-build-for-testing.log"
BUILD_STATUS_FILE="$DERIVED_PATH/Logs/agent-device-build-for-testing.status"
{
BUILD_STATUS=0
build_for_testing 2>&1 || BUILD_STATUS=$?
printf '%s\n' "$BUILD_STATUS" > "$BUILD_STATUS_FILE"
} | tee "$BUILD_LOG"
BUILD_STATUS="$(cat "$BUILD_STATUS_FILE")"
if [ "$BUILD_STATUS" -ne 0 ]; then
exit "$BUILD_STATUS"
fi
if [ "$REUSED_DERIVED_DATA" = 1 ]; then
echo "Isolation scan covers only the files this build recompiled: it reused DerivedData at $DERIVED_PATH. Run pnpm build:xcuitest:$PLATFORM:clean to scan every file." >&2
fi
if ! node --experimental-strip-types scripts/runner-isolation-diagnostics.ts "$BUILD_LOG"; then
# Unchanged files print no diagnostics on the next incremental build, so a rerun would pass.
rm -rf "$DERIVED_PATH/Build/Intermediates.noindex"
exit 1
fi
node --experimental-strip-types scripts/patch-xcuitest-runner-icon.ts "$DERIVED_PATH"
node scripts/write-xcuitest-cache-metadata.mjs "$PLATFORM" "$DERIVED_PATH" "$DESTINATION"