peachsim 0.1.730 → 0.1.746

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/cli/bridge-flow-runner.ts +15 -2
  2. package/cli/commands/control.ts +13 -3
  3. package/cli/commands/desktop.ts +1 -0
  4. package/cli/commands/electron.ts +12 -8
  5. package/cli/commands/flow.ts +1 -3
  6. package/cli/commands/install-desktop.ts +105 -66
  7. package/cli/commands/logout.ts +5 -1
  8. package/cli/commands/no-bridge-hint.ts +2 -2
  9. package/cli/commands/swift.ts +26 -9
  10. package/cli/desktop-companion.ts +36 -9
  11. package/cli/drivers/desktop.ts +65 -0
  12. package/cli/drivers/electron.ts +4 -66
  13. package/cli/drivers/index.ts +1 -0
  14. package/cli/drivers/playwright-sim-host.ts +2 -0
  15. package/cli/drivers/playwright.ts +3 -3
  16. package/cli/drivers/registry.ts +7 -2
  17. package/cli/drivers/types.ts +4 -3
  18. package/cli/maestro-js.ts +10 -0
  19. package/cli/outbound-endpoints.ts +2 -2
  20. package/dist-lib/agent-daemon-client.cjs +1 -1
  21. package/dist-lib/agent-events.cjs +1 -1
  22. package/dist-lib/agent-identity.cjs +1 -1
  23. package/dist-lib/agent-sessions.cjs +1 -1
  24. package/dist-lib/attached-projects.cjs +1 -1
  25. package/dist-lib/auth/shared-session.cjs +39 -9
  26. package/dist-lib/backend-origin.cjs +1 -1
  27. package/dist-lib/beta.cjs +1 -1
  28. package/dist-lib/beta.mjs +1 -1
  29. package/dist-lib/bridge-constants.cjs +1 -1
  30. package/dist-lib/bridge-contract-input.cjs +1 -1
  31. package/dist-lib/bridge-contract-input.mjs +1 -1
  32. package/dist-lib/bridge-contract.cjs +1 -1
  33. package/dist-lib/bridge-contract.mjs +1 -1
  34. package/dist-lib/capture-contract.cjs +13 -19
  35. package/dist-lib/capture-contract.mjs +13 -19
  36. package/dist-lib/cli-constants.cjs +1 -1
  37. package/dist-lib/cloud-contract.cjs +1 -1
  38. package/dist-lib/cloud-contract.mjs +1 -1
  39. package/dist-lib/cloud-test-args.cjs +1 -1
  40. package/dist-lib/cloud-test-args.mjs +1 -1
  41. package/dist-lib/cloud.cjs +1 -1
  42. package/dist-lib/cloud.mjs +1 -1
  43. package/dist-lib/config.cjs +1 -1
  44. package/dist-lib/detox/index.cjs +117 -11
  45. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  46. package/dist-lib/home-paths.cjs +32 -1
  47. package/dist-lib/host/bridge-host.cjs +1741 -540
  48. package/dist-lib/host/fetch-proxy-handler.cjs +1 -1
  49. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  50. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  51. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  52. package/dist-lib/host/websocket-proxy.cjs +1 -1
  53. package/dist-lib/index.cjs +17 -22
  54. package/dist-lib/jump-to-source-babel.cjs +1 -1
  55. package/dist-lib/jump-to-source-native.cjs +1 -1
  56. package/dist-lib/kotlin-host.cjs +1 -1
  57. package/dist-lib/menu.cjs +1 -1
  58. package/dist-lib/menu.mjs +1 -1
  59. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  60. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  61. package/dist-lib/metro-production-bundle.cjs +1 -1
  62. package/dist-lib/metro-production-bundle.mjs +1 -1
  63. package/dist-lib/metro.cjs +1 -1
  64. package/dist-lib/native-app-identity.cjs +1 -1
  65. package/dist-lib/native-app-identity.mjs +1 -1
  66. package/dist-lib/native-firebase-metadata.cjs +1 -1
  67. package/dist-lib/native-firebase-metadata.mjs +1 -1
  68. package/dist-lib/plist.cjs +1 -1
  69. package/dist-lib/plist.mjs +1 -1
  70. package/dist-lib/profiles.cjs +1 -1
  71. package/dist-lib/public-brand.cjs +1 -1
  72. package/dist-lib/react-native-host-modules.cjs +1 -1
  73. package/dist-lib/react-native-host-modules.mjs +1 -1
  74. package/dist-lib/render-mode.cjs +1 -1
  75. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  76. package/dist-lib/sdk.cjs +2 -1
  77. package/dist-lib/sdk.mjs +2 -1
  78. package/dist-lib/skills.cjs +2431 -1075
  79. package/dist-lib/swift-compile.cjs +374 -65
  80. package/dist-lib/swift-compile.mjs +369 -66
  81. package/dist-lib/swift-package.cjs +146 -9
  82. package/dist-lib/swift-package.mjs +144 -9
  83. package/dist-lib/swift-submit.cjs +2 -2
  84. package/dist-lib/swift-submit.mjs +2 -2
  85. package/dist-lib/swift-toolchain.cjs +1 -1
  86. package/dist-lib/swift.cjs +1 -1
  87. package/dist-lib/vite-swift.cjs +1690 -233
  88. package/dist-lib/vite.cjs +4 -4
  89. package/package.json +1 -1
  90. package/skills/contrast/SKILL.md +1 -1
  91. package/src/auth/shared-session.ts +45 -9
  92. package/src/bridge-constants.ts +7 -7
  93. package/src/capture-contract.ts +18 -33
  94. package/src/desktop-host-delivery.ts +81 -0
  95. package/src/home-paths.ts +30 -0
  96. package/src/host/bridge-host.ts +11 -6
  97. package/src/native-seam-manifest.ts +0 -11
  98. package/src/sim-client.ts +1 -0
  99. package/src/swift-compile.ts +479 -76
  100. package/src/swift-distribution.lock.json +11 -11
  101. package/src/swift-host.ts +338 -45
  102. package/src/swift-package.ts +204 -19
  103. package/src/swift-remote-packages.ts +255 -0
  104. package/src/swift-submit.ts +4 -1
  105. package/src/vite-plugin-one.ts +2 -2
  106. package/src/vite-plugin.ts +29 -9
  107. package/src/xcode-project.ts +612 -0
@@ -0,0 +1,612 @@
1
+ // reads an xcode project's application target the way xcode builds it: the
2
+ // swift sources and resources that are members of the target, the local
3
+ // swift packages its products come from, the remote package products it links,
4
+ // and the build settings that change how its swift compiles. never runs xcode
5
+ // or evaluates a build phase; project.pbxproj is data.
6
+
7
+ import fs from 'node:fs'
8
+ import path from 'node:path'
9
+ import plist from 'plist'
10
+ import { swiftManifestGraph } from './swift-package'
11
+
12
+ type PlistValue = string | PlistValue[] | { [key: string]: PlistValue }
13
+ type PlistDict = { [key: string]: PlistValue }
14
+
15
+ // project.pbxproj is an old-style (openstep) property list: dictionaries,
16
+ // arrays and strings, with c comments. node's plist package reads only xml.
17
+ export function parseOpenStepPlist(text: string): PlistValue {
18
+ let i = 0
19
+ const fail = (message: string): never => {
20
+ const line = text.slice(0, i).split('\n').length
21
+ throw new Error(`project.pbxproj line ${line}: ${message}`)
22
+ }
23
+ const skip = () => {
24
+ for (;;) {
25
+ const ch = text[i]
26
+ if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') i++
27
+ else if (ch === '/' && text[i + 1] === '*') {
28
+ const end = text.indexOf('*/', i + 2)
29
+ if (end < 0) fail('unterminated comment')
30
+ i = end + 2
31
+ } else if (ch === '/' && text[i + 1] === '/') {
32
+ const end = text.indexOf('\n', i)
33
+ i = end < 0 ? text.length : end + 1
34
+ } else return
35
+ }
36
+ }
37
+ const quoted = (): string => {
38
+ i++
39
+ let out = ''
40
+ for (;;) {
41
+ const ch = text[i++]
42
+ if (ch === undefined) fail('unterminated string')
43
+ if (ch === '"') return out
44
+ if (ch !== '\\') {
45
+ out += ch
46
+ continue
47
+ }
48
+ const next = text[i++]
49
+ if (next === 'n') out += '\n'
50
+ else if (next === 't') out += '\t'
51
+ else if (next === 'r') out += '\r'
52
+ else if (next === 'U') {
53
+ out += String.fromCharCode(parseInt(text.slice(i, i + 4), 16))
54
+ i += 4
55
+ } else out += next
56
+ }
57
+ }
58
+ const value = (): PlistValue => {
59
+ skip()
60
+ const ch = text[i]
61
+ if (ch === '{') {
62
+ i++
63
+ const dict: PlistDict = {}
64
+ for (;;) {
65
+ skip()
66
+ if (text[i] === '}') {
67
+ i++
68
+ return dict
69
+ }
70
+ const key = value()
71
+ if (typeof key !== 'string') fail('dictionary key is not a string')
72
+ skip()
73
+ if (text[i++] !== '=') fail('expected =')
74
+ dict[key as string] = value()
75
+ skip()
76
+ if (text[i++] !== ';') fail('expected ;')
77
+ }
78
+ }
79
+ if (ch === '(') {
80
+ i++
81
+ const list: PlistValue[] = []
82
+ for (;;) {
83
+ skip()
84
+ if (text[i] === ')') {
85
+ i++
86
+ return list
87
+ }
88
+ list.push(value())
89
+ skip()
90
+ if (text[i] === ',') i++
91
+ else if (text[i] !== ')') fail('expected , or )')
92
+ }
93
+ }
94
+ if (ch === '"') return quoted()
95
+ const start = i
96
+ while (i < text.length && /[A-Za-z0-9_$+/:.\-<>]/.test(text[i]!)) i++
97
+ if (i === start) fail(`unexpected ${JSON.stringify(ch ?? 'end of file')}`)
98
+ return text.slice(start, i)
99
+ }
100
+ if (text.startsWith('// !$*UTF8*$!')) i = text.indexOf('\n') + 1
101
+ const root = value()
102
+ skip()
103
+ if (i !== text.length) fail('trailing content')
104
+ return root
105
+ }
106
+
107
+ const isDict = (value: PlistValue | undefined): value is PlistDict =>
108
+ typeof value === 'object' && value !== null && !Array.isArray(value)
109
+ const str = (value: PlistValue | undefined): string | undefined =>
110
+ typeof value === 'string' ? value : undefined
111
+ const list = (value: PlistValue | undefined): string[] =>
112
+ Array.isArray(value)
113
+ ? value.filter((item): item is string => typeof item === 'string')
114
+ : []
115
+
116
+ /** a remote package product the target links, as the project names it. */
117
+ export interface XcodeRemoteProduct {
118
+ product: string
119
+ repositoryURL: string
120
+ }
121
+
122
+ /** an application target, resolved to files on disk. */
123
+ export interface XcodeAppTarget {
124
+ /** the directory holding the .xcodeproj: every relative path starts here. */
125
+ projectDir: string
126
+ pbxproj: string
127
+ name: string
128
+ /** the module name the target's sources compile as. */
129
+ moduleName: string
130
+ /** absolute paths of the swift sources that are members of the target. */
131
+ sources: string[]
132
+ /** absolute paths of every resource file the target copies, bundles expanded. */
133
+ resources: string[]
134
+ /** the target's processed launch plist, including generated build settings. */
135
+ infoPlist: string
136
+ infoPlistFile: string | null
137
+ languageMode: number
138
+ mainActorIsolation: boolean
139
+ /** directories of the local swift packages whose products the target links. */
140
+ localPackages: string[]
141
+ remoteProducts: XcodeRemoteProduct[]
142
+ /** the Package.resolved pinning the remote packages' revisions, when resolved. */
143
+ packageResolved: string | null
144
+ /** linked product names no local or remote package reference supplies. */
145
+ unresolvedProducts: string[]
146
+ /** every application target in the project, for a chooser. */
147
+ appTargets: string[]
148
+ }
149
+
150
+ const APPLICATION = 'com.apple.product-type.application'
151
+ // files xcode never builds into a target even inside a synchronized folder.
152
+ const NEVER_MEMBER =
153
+ /(?:^|\/)(?:Info\.plist|[^/]+\.entitlements|[^/]+\.xcconfig|\.DS_Store)$/
154
+ // directories xcode copies as a whole resource.
155
+ const BUNDLE_DIR =
156
+ /\.(xcassets|bundle|lproj|xcdatamodeld|scnassets|spriteatlas|rcproject|storyboardc|icon)$/
157
+
158
+ function walkFiles(dir: string): string[] {
159
+ if (!fs.existsSync(dir)) return []
160
+ const out: string[] = []
161
+ for (const entry of fs
162
+ .readdirSync(dir, { withFileTypes: true })
163
+ .sort((left, right) => left.name.localeCompare(right.name))) {
164
+ if (entry.name.startsWith('.')) continue
165
+ const full = path.join(dir, entry.name)
166
+ if (entry.isDirectory()) out.push(...walkFiles(full))
167
+ else if (entry.isFile()) out.push(full)
168
+ }
169
+ return out
170
+ }
171
+
172
+ function readXcconfig(file: string, seen = new Set<string>()): Record<string, string> {
173
+ if (seen.has(file) || !fs.existsSync(file)) return {}
174
+ seen.add(file)
175
+ const settings: Record<string, string> = {}
176
+ for (const raw of fs.readFileSync(file, 'utf8').split('\n')) {
177
+ const line = raw.replace(/\/\/.*$/, '').trim()
178
+ const include = /^#include\??\s+"([^"]+)"/.exec(line)
179
+ if (include) {
180
+ Object.assign(
181
+ settings,
182
+ readXcconfig(path.resolve(path.dirname(file), include[1]!), seen),
183
+ )
184
+ continue
185
+ }
186
+ const setting = /^([A-Z_][A-Z0-9_]*)(?:\[[^\]]*\])?\s*=\s*(.*)$/.exec(line)
187
+ if (setting) settings[setting[1]!] = setting[2]!.trim()
188
+ }
189
+ return settings
190
+ }
191
+
192
+ // xcode's c99extidentifier: what PRODUCT_MODULE_NAME defaults to.
193
+ function c99Identifier(name: string): string {
194
+ const id = name.replace(/[^A-Za-z0-9_]/g, '_')
195
+ return /^[0-9]/.test(id) ? `_${id}` : id
196
+ }
197
+
198
+ /** the .xcodeproj a path names: the bundle itself, or the only one in a directory. */
199
+ export function findXcodeProject(input: string): string | null {
200
+ const resolved = path.resolve(input)
201
+ if (resolved.endsWith('.xcodeproj')) return resolved
202
+ if (resolved.endsWith('.xcworkspace')) {
203
+ const data = path.join(resolved, 'contents.xcworkspacedata')
204
+ const projects = fs.existsSync(data)
205
+ ? [
206
+ ...fs
207
+ .readFileSync(data, 'utf8')
208
+ .matchAll(/location\s*=\s*"group:([^"]+\.xcodeproj)"/g),
209
+ ]
210
+ .map((match) => path.resolve(path.dirname(resolved), match[1]!))
211
+ .filter((project) => !project.includes(`${path.sep}Pods${path.sep}`))
212
+ : []
213
+ if (projects.length !== 1) {
214
+ throw new Error(
215
+ `${path.basename(resolved)} lists ${projects.length} app projects; pass the .xcodeproj`,
216
+ )
217
+ }
218
+ return projects[0]!
219
+ }
220
+ if (!fs.existsSync(resolved) || !fs.statSync(resolved).isDirectory()) return null
221
+ const projects = fs.readdirSync(resolved).filter((name) => name.endsWith('.xcodeproj'))
222
+ if (projects.length > 1) {
223
+ throw new Error(
224
+ `${resolved} holds ${projects.length} Xcode projects (${projects.join(', ')}); pass one`,
225
+ )
226
+ }
227
+ return projects.length === 1 ? path.join(resolved, projects[0]!) : null
228
+ }
229
+
230
+ const parsed = new Map<string, { mtimeMs: number; root: PlistDict }>()
231
+
232
+ function readProject(pbxproj: string): PlistDict {
233
+ const mtimeMs = fs.statSync(pbxproj).mtimeMs
234
+ const cached = parsed.get(pbxproj)
235
+ if (cached?.mtimeMs === mtimeMs) return cached.root
236
+ const root = parseOpenStepPlist(fs.readFileSync(pbxproj, 'utf8'))
237
+ if (!isDict(root) || !isDict(root.objects)) {
238
+ throw new Error(`${pbxproj} has no objects table`)
239
+ }
240
+ parsed.set(pbxproj, { mtimeMs, root })
241
+ return root
242
+ }
243
+
244
+ /**
245
+ * the application target `targetName` names (or the project's only one),
246
+ * with its member files listed fresh from disk: a file added to a
247
+ * synchronized folder joins the target without any project change.
248
+ */
249
+ export function readXcodeAppTarget(
250
+ xcodeproj: string,
251
+ targetName?: string,
252
+ ): XcodeAppTarget {
253
+ const pbxproj = path.join(xcodeproj, 'project.pbxproj')
254
+ if (!fs.existsSync(pbxproj)) throw new Error(`${xcodeproj} has no project.pbxproj`)
255
+ const root = readProject(pbxproj)
256
+ const objects = root.objects as PlistDict
257
+ const object = (id: string | undefined): PlistDict | undefined => {
258
+ const found = id === undefined ? undefined : objects[id]
259
+ return isDict(found) ? found : undefined
260
+ }
261
+ const project = object(str(root.rootObject))
262
+ if (!project || project.isa !== 'PBXProject') {
263
+ throw new Error(`${pbxproj} has no PBXProject root`)
264
+ }
265
+ // canonical, so every member path agrees with the host's realpath checks
266
+ const projectDir = fs.realpathSync(
267
+ path.resolve(path.dirname(xcodeproj), str(project.projectDirPath) ?? ''),
268
+ )
269
+
270
+ // every group child's parent, so a "<group>" path resolves up the tree.
271
+ const parent = new Map<string, string>()
272
+ for (const [id, entry] of Object.entries(objects)) {
273
+ if (!isDict(entry)) continue
274
+ if (entry.isa === 'PBXGroup' || entry.isa === 'PBXVariantGroup') {
275
+ for (const child of list(entry.children)) parent.set(child, id)
276
+ }
277
+ }
278
+ const resolvedPaths = new Map<string, string | null>()
279
+ const resolvePath = (id: string): string | null => {
280
+ if (resolvedPaths.has(id)) return resolvedPaths.get(id)!
281
+ const entry = object(id)
282
+ let result: string | null = null
283
+ if (entry) {
284
+ const own = str(entry.path) ?? ''
285
+ const tree = str(entry.sourceTree) ?? '<group>'
286
+ if (tree === '<absolute>') result = own
287
+ else if (tree === 'SOURCE_ROOT') result = path.join(projectDir, own)
288
+ else if (tree === '<group>') {
289
+ const up = parent.get(id)
290
+ const base = up ? resolvePath(up) : projectDir
291
+ result = base === null ? null : path.join(base, own)
292
+ }
293
+ }
294
+ resolvedPaths.set(id, result)
295
+ return result
296
+ }
297
+
298
+ const targets = list(project.targets)
299
+ .map((id) => ({ id, target: object(id) }))
300
+ .filter(
301
+ (entry): entry is { id: string; target: PlistDict } =>
302
+ entry.target?.isa === 'PBXNativeTarget',
303
+ )
304
+ const appTargets = targets.filter((entry) => entry.target.productType === APPLICATION)
305
+ const appTargetNames = appTargets.map((entry) => str(entry.target.name) ?? entry.id)
306
+ const chosen = targetName
307
+ ? appTargets.find((entry) => entry.target.name === targetName)
308
+ : appTargets.length === 1
309
+ ? appTargets[0]
310
+ : undefined
311
+ if (!chosen) {
312
+ throw new Error(
313
+ targetName
314
+ ? `${path.basename(xcodeproj)} has no application target ${targetName}; it has ${appTargetNames.join(', ') || 'none'}`
315
+ : `${path.basename(xcodeproj)} has ${appTargetNames.length} application targets (${appTargetNames.join(', ') || 'none'}); pass --target`,
316
+ )
317
+ }
318
+ const { id: targetId, target } = chosen
319
+
320
+ const configSettings = (listId: string | undefined): Record<string, string> => {
321
+ const configs = list(object(listId)?.buildConfigurations).map(object)
322
+ const config =
323
+ configs.find((entry) => entry?.name === 'Debug') ?? configs.find(Boolean)
324
+ if (!config) return {}
325
+ const base = str(config.baseConfigurationReference)
326
+ const fromFile = base ? readXcconfig(resolvePath(base) ?? '') : {}
327
+ const own = isDict(config.buildSettings) ? config.buildSettings : {}
328
+ return {
329
+ ...fromFile,
330
+ ...Object.fromEntries(
331
+ Object.entries(own).flatMap(([key, value]) =>
332
+ typeof value === 'string' ? [[key, value]] : [],
333
+ ),
334
+ ),
335
+ }
336
+ }
337
+ const settings = {
338
+ ...configSettings(str(project.buildConfigurationList)),
339
+ ...configSettings(str(target.buildConfigurationList)),
340
+ }
341
+ const name = str(target.name) ?? targetId
342
+ const productName = (settings.PRODUCT_NAME ?? '$(TARGET_NAME)')
343
+ .replaceAll('$(TARGET_NAME)', name)
344
+ .replaceAll('${TARGET_NAME}', name)
345
+ const moduleName = c99Identifier(
346
+ settings.PRODUCT_MODULE_NAME && !settings.PRODUCT_MODULE_NAME.includes('$(')
347
+ ? settings.PRODUCT_MODULE_NAME
348
+ : productName,
349
+ )
350
+
351
+ const variables = {
352
+ ...settings,
353
+ EXECUTABLE_NAME: settings.EXECUTABLE_NAME ?? productName,
354
+ DEVELOPMENT_LANGUAGE:
355
+ settings.DEVELOPMENT_LANGUAGE ?? str(project.developmentRegion) ?? 'en',
356
+ TARGET_NAME: name,
357
+ PRODUCT_NAME: productName,
358
+ PRODUCT_MODULE_NAME: moduleName,
359
+ SRCROOT: projectDir,
360
+ SOURCE_ROOT: projectDir,
361
+ }
362
+ const expand = (value: string): string => {
363
+ const seen = new Set<string>()
364
+ while (/\$[({][A-Za-z_][A-Za-z0-9_]*[)}]/.test(value)) {
365
+ if (seen.has(value)) throw new Error(`${name} has cyclic Info.plist build settings`)
366
+ seen.add(value)
367
+ value = value.replace(
368
+ /\$[({]([A-Za-z_][A-Za-z0-9_]*)[)}]/g,
369
+ (match, key: string) => {
370
+ const replacement = Object.entries(variables).find(
371
+ ([name]) => name === key,
372
+ )?.[1]
373
+ if (replacement === undefined)
374
+ throw new Error(`${name} Info.plist has unresolved setting ${match}`)
375
+ return replacement
376
+ },
377
+ )
378
+ }
379
+ return value
380
+ }
381
+ const expandValue = (value: plist.PlistValue): plist.PlistValue => {
382
+ if (typeof value === 'string') return expand(value)
383
+ if (Array.isArray(value)) return value.map(expandValue)
384
+ if (
385
+ typeof value === 'object' &&
386
+ !(value instanceof Date) &&
387
+ !Buffer.isBuffer(value)
388
+ ) {
389
+ return Object.fromEntries(
390
+ Object.entries(value).map(([key, value]) => [key, expandValue(value)]),
391
+ )
392
+ }
393
+ return value
394
+ }
395
+ const infoPlistFile = settings.INFOPLIST_FILE
396
+ ? path.resolve(projectDir, expand(settings.INFOPLIST_FILE))
397
+ : null
398
+ const sourceInfo = infoPlistFile
399
+ ? plist.parse(fs.readFileSync(infoPlistFile, 'utf8'))
400
+ : {}
401
+ if (
402
+ !sourceInfo ||
403
+ typeof sourceInfo !== 'object' ||
404
+ Array.isArray(sourceInfo) ||
405
+ sourceInfo instanceof Date ||
406
+ Buffer.isBuffer(sourceInfo)
407
+ ) {
408
+ throw new Error(`${name} Info.plist must contain a dictionary`)
409
+ }
410
+ const info: Record<string, plist.PlistValue> = {}
411
+ for (const [key, value] of Object.entries(sourceInfo)) info[key] = expandValue(value)
412
+ for (const [key, value] of Object.entries(settings)) {
413
+ if (!key.startsWith('INFOPLIST_KEY_') || key.endsWith('_Generation')) continue
414
+ info[key.slice('INFOPLIST_KEY_'.length)] =
415
+ value === 'YES' ? true : value === 'NO' ? false : expand(value)
416
+ }
417
+ if (
418
+ settings.INFOPLIST_KEY_UIApplicationSceneManifest_Generation === 'YES' &&
419
+ info.UIApplicationSceneManifest === undefined
420
+ ) {
421
+ info.UIApplicationSceneManifest = {
422
+ UIApplicationSupportsMultipleScenes: true,
423
+ UISceneConfigurations: {},
424
+ }
425
+ }
426
+
427
+ const sources = new Set<string>()
428
+ const resources = new Set<string>()
429
+ const addResource = (file: string) => {
430
+ if (fs.existsSync(file) && fs.statSync(file).isDirectory()) {
431
+ for (const inner of walkFiles(file)) resources.add(inner)
432
+ } else if (fs.existsSync(file)) resources.add(file)
433
+ }
434
+ const fileRefPaths = (refId: string): string[] => {
435
+ const ref = object(refId)
436
+ if (!ref) return []
437
+ if (ref.isa === 'PBXVariantGroup') {
438
+ return list(ref.children).flatMap((child) => {
439
+ const resolved = resolvePath(child)
440
+ return resolved ? [resolved] : []
441
+ })
442
+ }
443
+ const resolved = resolvePath(refId)
444
+ return resolved ? [resolved] : []
445
+ }
446
+ for (const phaseId of list(target.buildPhases)) {
447
+ const phase = object(phaseId)
448
+ if (phase?.isa !== 'PBXSourcesBuildPhase' && phase?.isa !== 'PBXResourcesBuildPhase')
449
+ continue
450
+ for (const buildFileId of list(phase.files)) {
451
+ for (const file of fileRefPaths(str(object(buildFileId)?.fileRef) ?? '')) {
452
+ if (phase.isa === 'PBXSourcesBuildPhase' && file.endsWith('.swift')) {
453
+ if (fs.existsSync(file)) sources.add(file)
454
+ } else if (phase.isa === 'PBXResourcesBuildPhase') addResource(file)
455
+ }
456
+ }
457
+ }
458
+
459
+ // synchronized folders: every file under a folder the target owns is a
460
+ // member, minus that folder's exceptions for this target; a folder the
461
+ // target does not own adds exactly the files its exceptions name for it.
462
+ const addSynchronized = (folder: string, relative: string) => {
463
+ const file = path.join(folder, relative)
464
+ if (NEVER_MEMBER.test(relative)) return
465
+ if (file.endsWith('.swift')) {
466
+ if (!relative.split('/').some((segment) => BUNDLE_DIR.test(segment)))
467
+ sources.add(file)
468
+ } else if (/\.(h|m|mm|c|cpp|metal)$/.test(file)) {
469
+ return
470
+ } else resources.add(file)
471
+ }
472
+ const owned = new Set(list(target.fileSystemSynchronizedGroups))
473
+ for (const [groupId, group] of Object.entries(objects)) {
474
+ if (!isDict(group) || group.isa !== 'PBXFileSystemSynchronizedRootGroup') continue
475
+ const folder = resolvePath(groupId)
476
+ if (!folder) continue
477
+ const exceptions = new Set<string>()
478
+ for (const exceptionId of list(group.exceptions)) {
479
+ const exception = object(exceptionId)
480
+ if (exception?.target !== targetId) continue
481
+ for (const relative of list(exception.membershipExceptions))
482
+ exceptions.add(relative)
483
+ }
484
+ if (owned.has(groupId)) {
485
+ for (const file of walkFiles(folder)) {
486
+ const relative = path.relative(folder, file).split(path.sep).join('/')
487
+ if (
488
+ [...exceptions].some(
489
+ (exception) => relative === exception || relative.startsWith(`${exception}/`),
490
+ )
491
+ )
492
+ continue
493
+ addSynchronized(folder, relative)
494
+ }
495
+ } else {
496
+ for (const relative of exceptions) {
497
+ const file = path.join(folder, relative)
498
+ if (fs.existsSync(file) && fs.statSync(file).isDirectory()) {
499
+ for (const inner of walkFiles(file))
500
+ addSynchronized(
501
+ folder,
502
+ path.relative(folder, inner).split(path.sep).join('/'),
503
+ )
504
+ } else if (fs.existsSync(file)) addSynchronized(folder, relative)
505
+ }
506
+ }
507
+ }
508
+
509
+ // linked products: a remote reference names its repository; one without a
510
+ // package comes from a local package folder the project references.
511
+ const localPackageDirs = Object.entries(objects).flatMap(([id, entry]) => {
512
+ if (!isDict(entry)) return []
513
+ const candidate =
514
+ entry.isa === 'XCLocalSwiftPackageReference'
515
+ ? path.resolve(projectDir, str(entry.relativePath) ?? '')
516
+ : entry.isa === 'PBXFileReference' && entry.lastKnownFileType === 'wrapper'
517
+ ? resolvePath(id)
518
+ : null
519
+ return candidate && fs.existsSync(path.join(candidate, 'Package.swift'))
520
+ ? [candidate]
521
+ : []
522
+ })
523
+ const graphs = new Map<string, ReturnType<typeof swiftManifestGraph>>()
524
+ const graphOf = (dir: string) => {
525
+ const cached = graphs.get(dir)
526
+ if (cached) return cached
527
+ const graph = swiftManifestGraph(
528
+ fs.readFileSync(path.join(dir, 'Package.swift'), 'utf8'),
529
+ )
530
+ graphs.set(dir, graph)
531
+ return graph
532
+ }
533
+ const localProducts = new Map<string, string>()
534
+ for (const dir of localPackageDirs) {
535
+ for (const product of graphOf(dir).products.keys()) localProducts.set(product, dir)
536
+ }
537
+ const localPackages = new Set<string>()
538
+ const remoteProducts: XcodeRemoteProduct[] = []
539
+ const unresolvedProducts: string[] = []
540
+ for (const dependencyId of list(target.packageProductDependencies)) {
541
+ const dependency = object(dependencyId)
542
+ const product = str(dependency?.productName)
543
+ if (!dependency || !product) continue
544
+ const reference = object(str(dependency.package))
545
+ if (reference?.isa === 'XCRemoteSwiftPackageReference') {
546
+ remoteProducts.push({
547
+ product,
548
+ repositoryURL: str(reference.repositoryURL) ?? '',
549
+ })
550
+ } else if (reference?.isa === 'XCLocalSwiftPackageReference') {
551
+ localPackages.add(path.resolve(projectDir, str(reference.relativePath) ?? ''))
552
+ } else if (localProducts.has(product)) {
553
+ localPackages.add(localProducts.get(product)!)
554
+ } else unresolvedProducts.push(product)
555
+ }
556
+
557
+ // local packages depend on further local packages by path, and on remote
558
+ // products by the package's url.
559
+ const pending = [...localPackages]
560
+ while (pending.length > 0) {
561
+ const dir = pending.pop()!
562
+ const graph = graphOf(dir)
563
+ for (const relative of graph.packagePaths) {
564
+ const next = path.resolve(dir, relative)
565
+ if (localPackages.has(next) || !fs.existsSync(path.join(next, 'Package.swift')))
566
+ continue
567
+ localPackages.add(next)
568
+ pending.push(next)
569
+ }
570
+ for (const dependencies of graph.dependencies.values()) {
571
+ for (const dependency of dependencies) {
572
+ if (dependency.kind !== 'product') continue
573
+ const url = graph.packageURLs.get(dependency.package)
574
+ if (url && !remoteProducts.some((entry) => entry.product === dependency.name))
575
+ remoteProducts.push({ product: dependency.name, repositoryURL: url })
576
+ }
577
+ }
578
+ }
579
+
580
+ // xcode keeps the pins beside the project, or in the workspace that opens it
581
+ const packageResolved =
582
+ [
583
+ path.join(xcodeproj, 'project.xcworkspace'),
584
+ ...fs
585
+ .readdirSync(projectDir)
586
+ .filter((entry) => entry.endsWith('.xcworkspace'))
587
+ .map((entry) => path.join(projectDir, entry)),
588
+ ]
589
+ .map((workspace) =>
590
+ path.join(workspace, 'xcshareddata', 'swiftpm', 'Package.resolved'),
591
+ )
592
+ .find((file) => fs.existsSync(file)) ?? null
593
+
594
+ const swiftVersion = Number.parseFloat(settings.SWIFT_VERSION ?? '5')
595
+ return {
596
+ projectDir,
597
+ pbxproj,
598
+ name,
599
+ moduleName,
600
+ sources: [...sources].sort(),
601
+ resources: [...resources].sort(),
602
+ infoPlist: plist.build(info),
603
+ infoPlistFile,
604
+ languageMode: swiftVersion >= 6 ? 6 : 5,
605
+ mainActorIsolation: settings.SWIFT_DEFAULT_ACTOR_ISOLATION === 'MainActor',
606
+ localPackages: [...localPackages].sort(),
607
+ remoteProducts,
608
+ packageResolved,
609
+ unresolvedProducts,
610
+ appTargets: appTargetNames,
611
+ }
612
+ }