-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimulator-selection.mjs
More file actions
187 lines (169 loc) · 4.9 KB
/
simulator-selection.mjs
File metadata and controls
187 lines (169 loc) · 4.9 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
const defaultDeviceNames = [
"iPhone 17",
"iPhone 16",
"iPhone 15",
"iPhone 14",
"iPhone 13",
"iPhone SE (3rd generation)",
];
export function selectIntegrationSimulator({
runJson,
runText,
timeoutMs,
env = process.env,
}) {
const sdkVersion = activeSimulatorSdkVersion(runText, timeoutMs);
const runtimes = availableIosRuntimes(runJson, timeoutMs);
const runtime = selectRuntime(runtimes, sdkVersion, env);
const deviceType = selectDeviceType(
runtime,
availableDeviceTypes(runJson, timeoutMs),
env.SIMDECK_INTEGRATION_DEVICE_TYPE,
);
return {
runtime,
deviceType,
sdkVersion,
};
}
function activeSimulatorSdkVersion(runText, timeoutMs) {
return runText("xcrun", ["--sdk", "iphonesimulator", "--show-sdk-version"], {
timeoutMs,
}).trim();
}
function availableIosRuntimes(runJson, timeoutMs) {
const payload = runJson("xcrun", ["simctl", "list", "runtimes", "-j"], {
timeoutMs,
});
return payload.runtimes
.filter(
(runtime) => runtime.isAvailable && runtime.identifier?.includes("iOS"),
)
.sort(compareRuntimeVersions);
}
function availableDeviceTypes(runJson, timeoutMs) {
return (
runJson("xcrun", ["simctl", "list", "devicetypes", "-j"], {
timeoutMs,
}).devicetypes ?? []
);
}
function selectRuntime(runtimes, sdkVersion, env) {
if (runtimes.length === 0) {
throw new Error("No available iOS simulator runtime found.");
}
const requestedRuntime = env.SIMDECK_INTEGRATION_IOS_RUNTIME;
if (requestedRuntime) {
const match = runtimes.find((runtime) =>
runtimeMatches(runtime, requestedRuntime),
);
if (!match) {
throw new Error(
`No available iOS simulator runtime matched ${JSON.stringify(requestedRuntime)}.`,
);
}
return match;
}
const sdkMajor = versionParts(sdkVersion)[0];
const sameMajor = runtimes.filter(
(runtime) => versionParts(runtime.version)[0] === sdkMajor,
);
const notNewerThanSdk = sameMajor.filter(
(runtime) => compareVersionStrings(runtime.version, sdkVersion) <= 0,
);
return notNewerThanSdk.at(-1) ?? sameMajor.at(-1) ?? runtimes.at(-1);
}
function runtimeMatches(runtime, requestedRuntime) {
const requested = requestedRuntime.trim();
const normalizedRequested = normalizeIdentifier(requested);
return [
runtime.identifier,
runtime.name,
runtime.version,
`iOS ${runtime.version}`,
`com.apple.CoreSimulator.SimRuntime.iOS-${String(runtime.version).replaceAll(".", "-")}`,
].some((candidate) => normalizeIdentifier(candidate) === normalizedRequested);
}
function selectDeviceType(runtime, allDeviceTypes, requestedDeviceType) {
const supported = supportedDeviceTypes(runtime, allDeviceTypes);
const iphones = supported.filter(
(device) =>
device.productFamily === "iPhone" ||
device.identifier?.includes("iPhone"),
);
if (requestedDeviceType) {
const match = iphones.find((device) =>
deviceTypeMatches(device, requestedDeviceType),
);
if (!match) {
throw new Error(
`Runtime ${runtime.identifier} does not support requested device ${JSON.stringify(requestedDeviceType)}.`,
);
}
return match;
}
for (const name of defaultDeviceNames) {
const match = iphones.find((device) => device.name === name);
if (match) {
return match;
}
}
const fallback = iphones[0];
if (!fallback) {
throw new Error(
`Runtime ${runtime.identifier} does not support an iPhone device.`,
);
}
return fallback;
}
function supportedDeviceTypes(runtime, allDeviceTypes) {
const runtimeSupported = Array.isArray(runtime.supportedDeviceTypes)
? runtime.supportedDeviceTypes
: [];
if (runtimeSupported.length > 0) {
return runtimeSupported;
}
return allDeviceTypes.filter(
(device) =>
device.productFamily === "iPhone" ||
device.identifier?.includes("iPhone"),
);
}
function deviceTypeMatches(device, requestedDeviceType) {
const requested = normalizeIdentifier(requestedDeviceType);
return [device.identifier, device.name].some(
(candidate) => normalizeIdentifier(candidate) === requested,
);
}
function normalizeIdentifier(value) {
return String(value ?? "")
.trim()
.toLowerCase();
}
function compareRuntimeVersions(left, right) {
const delta = compareVersionStrings(left.version, right.version);
if (delta !== 0) {
return delta;
}
return String(left.identifier).localeCompare(String(right.identifier));
}
function compareVersionStrings(left, right) {
const leftParts = versionParts(left);
const rightParts = versionParts(right);
for (
let index = 0;
index < Math.max(leftParts.length, rightParts.length);
index += 1
) {
const delta = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
if (delta !== 0) {
return delta;
}
}
return 0;
}
function versionParts(version) {
return String(version ?? "0")
.split(".")
.map((part) => Number(part) || 0);
}