peachsim 0.1.759 → 0.1.760

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 (77) hide show
  1. package/dist-lib/agent-daemon-client.cjs +1 -1
  2. package/dist-lib/agent-events.cjs +1 -1
  3. package/dist-lib/agent-identity.cjs +1 -1
  4. package/dist-lib/agent-sessions.cjs +1 -1
  5. package/dist-lib/attached-projects.cjs +1 -1
  6. package/dist-lib/auth/shared-session.cjs +1 -1
  7. package/dist-lib/backend-origin.cjs +1 -1
  8. package/dist-lib/beta.cjs +1 -1
  9. package/dist-lib/beta.mjs +1 -1
  10. package/dist-lib/bridge-constants.cjs +1 -1
  11. package/dist-lib/bridge-contract-input.cjs +1 -1
  12. package/dist-lib/bridge-contract-input.mjs +1 -1
  13. package/dist-lib/bridge-contract.cjs +1 -1
  14. package/dist-lib/bridge-contract.mjs +1 -1
  15. package/dist-lib/capture-contract.cjs +1 -1
  16. package/dist-lib/capture-contract.mjs +1 -1
  17. package/dist-lib/cli-constants.cjs +1 -1
  18. package/dist-lib/cloud-contract.cjs +1 -1
  19. package/dist-lib/cloud-contract.mjs +1 -1
  20. package/dist-lib/cloud-test-args.cjs +1 -1
  21. package/dist-lib/cloud-test-args.mjs +1 -1
  22. package/dist-lib/cloud.cjs +1 -1
  23. package/dist-lib/cloud.mjs +1 -1
  24. package/dist-lib/config.cjs +1 -1
  25. package/dist-lib/detox/index.cjs +1 -1
  26. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  27. package/dist-lib/home-paths.cjs +1 -1
  28. package/dist-lib/host/bridge-host.cjs +823 -728
  29. package/dist-lib/host/fetch-proxy-handler.cjs +1 -1
  30. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  31. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  32. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  33. package/dist-lib/host/websocket-proxy.cjs +1 -1
  34. package/dist-lib/index.cjs +37 -220
  35. package/dist-lib/jump-to-source-babel.cjs +1 -1
  36. package/dist-lib/jump-to-source-native.cjs +1 -1
  37. package/dist-lib/kotlin-host.cjs +1 -1
  38. package/dist-lib/menu.cjs +1 -1
  39. package/dist-lib/menu.mjs +1 -1
  40. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  41. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  42. package/dist-lib/metro-production-bundle.cjs +1 -1
  43. package/dist-lib/metro-production-bundle.mjs +1 -1
  44. package/dist-lib/metro.cjs +37 -220
  45. package/dist-lib/native-app-identity.cjs +1 -1
  46. package/dist-lib/native-app-identity.mjs +1 -1
  47. package/dist-lib/native-firebase-metadata.cjs +1 -1
  48. package/dist-lib/native-firebase-metadata.mjs +1 -1
  49. package/dist-lib/plist.cjs +1 -1
  50. package/dist-lib/plist.mjs +1 -1
  51. package/dist-lib/profiles.cjs +1 -1
  52. package/dist-lib/public-brand.cjs +1 -1
  53. package/dist-lib/react-native-host-modules.cjs +1 -1
  54. package/dist-lib/react-native-host-modules.mjs +1 -1
  55. package/dist-lib/render-mode.cjs +1 -1
  56. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  57. package/dist-lib/sdk.cjs +1 -1
  58. package/dist-lib/sdk.mjs +1 -1
  59. package/dist-lib/skills.cjs +1120 -1019
  60. package/dist-lib/swift-compile.cjs +1 -1
  61. package/dist-lib/swift-compile.mjs +1 -1
  62. package/dist-lib/swift-package.cjs +1 -110
  63. package/dist-lib/swift-package.mjs +1 -109
  64. package/dist-lib/swift-submit.cjs +1 -1
  65. package/dist-lib/swift-submit.mjs +1 -1
  66. package/dist-lib/swift-toolchain.cjs +1 -1
  67. package/dist-lib/swift.cjs +1 -1
  68. package/dist-lib/vite-swift.cjs +607 -512
  69. package/dist-lib/vite.cjs +1 -1
  70. package/package.json +1 -1
  71. package/src/native-seam-manifest.ts +8 -0
  72. package/src/swift-host.ts +64 -64
  73. package/src/swift-package-manifest.ts +307 -0
  74. package/src/swift-package.ts +2 -135
  75. package/src/swift-packages.ts +341 -0
  76. package/src/xcode-project.ts +57 -70
  77. package/src/swift-remote-packages.ts +0 -255
package/dist-lib/vite.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! rnx v0.1.759 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
1
+ /*! rnx v0.1.760 | (c) 2026 Tamagui LLC | Proprietary — see LICENSE */
2
2
  let __sootsim_import_meta_url = ''; try { __sootsim_import_meta_url = require('url').pathToFileURL(__filename).href; } catch {}
3
3
  "use strict";
4
4
  var __create = Object.create;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "peachsim",
3
- "version": "0.1.759",
3
+ "version": "0.1.760",
4
4
  "description": "Vite and Metro plugins, testing drivers, and SDK exports for peach.",
5
5
  "author": "Tamagui LLC",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -130,6 +130,14 @@ export const NATIVE_SEAM_MANIFEST: NativeSeamEntry[] = [
130
130
  notes:
131
131
  'iOS BGTaskScheduler / Android WorkManager TurboModule — no real scheduler in browser; acknowledge registrations, never fire periodic events',
132
132
  },
133
+ {
134
+ pkg: 'expo',
135
+ // winter imports this relatively during Expo.fx, before bundle globals.
136
+ // use the same browser-backed native boundary as guest registry imports.
137
+ seamBasenames: ['FormData'],
138
+ target: path.resolve(HERE, '../../peach-compat/src/stubs/expo-winter-formdata.ts'),
139
+ notes: 'winter FormData installation on the browser-native networking boundary',
140
+ },
133
141
  {
134
142
  pkg: 'expo-apple-authentication',
135
143
  // src/ExpoAppleAuthenticationButton.ts is the package's native-view
package/src/swift-host.ts CHANGED
@@ -24,12 +24,12 @@ import {
24
24
  swiftApplicationEntry,
25
25
  } from './swift-compile'
26
26
  import { swiftDistributionRoot, swiftModuleToolchain } from './swift-distribution'
27
- import { swiftImplicitResourcePaths, swiftManifestTargets } from './swift-package'
27
+ import { swiftImplicitResourcePaths, type SwiftPackageTarget } from './swift-package'
28
28
  import {
29
29
  REMOTE_CHECKOUTS,
30
- resolveRemotePackages,
31
- type RemotePackages,
32
- } from './swift-remote-packages'
30
+ resolveSwiftPackages,
31
+ type SwiftPackages,
32
+ } from './swift-packages'
33
33
  import {
34
34
  MAX_SWIFT_COMPILE_FILES,
35
35
  MAX_SWIFT_COMPILE_SOURCE_BYTES,
@@ -181,7 +181,7 @@ interface XcodeProjectEntry {
181
181
  target?: string
182
182
  /** peach's own modules: a remote product named one links it, never its source. */
183
183
  provided: ReadonlySet<string> | null
184
- remote?: { key: string; packages: RemotePackages }
184
+ packages?: { key: string; packages: SwiftPackages }
185
185
  }
186
186
 
187
187
  const xcodeProjects = new Map<string, XcodeProjectEntry>()
@@ -207,26 +207,32 @@ export function openXcodeProject(
207
207
  return root
208
208
  }
209
209
 
210
- // remote packages resolve once per set of linked products and pins.
211
- function remotePackages(entry: XcodeProjectEntry, app: XcodeAppTarget): RemotePackages {
212
- if (!entry.provided) return { targets: [], unavailable: new Map() }
210
+ // packages resolve once per set of linked products, pins and local manifests.
211
+ function swiftPackages(entry: XcodeProjectEntry, app: XcodeAppTarget): SwiftPackages {
212
+ const stamp = (file: string | null) =>
213
+ file && fs.existsSync(file) ? fs.statSync(file).mtimeMs : 0
213
214
  const key = JSON.stringify([
214
- app.remoteProducts,
215
+ app.linkedProducts,
215
216
  app.packageResolved,
216
- app.packageResolved ? fs.statSync(app.packageResolved).mtimeMs : 0,
217
+ stamp(app.packageResolved),
218
+ app.localPackageDirs.map((dir) => stamp(path.join(dir, 'Package.swift'))),
219
+ app.linkedProducts.map(({ source }) =>
220
+ source.kind === 'local' ? stamp(path.join(source.dir, 'Package.swift')) : 0,
221
+ ),
217
222
  ])
218
- if (entry.remote?.key !== key) {
219
- entry.remote = {
223
+ if (entry.packages?.key !== key) {
224
+ entry.packages = {
220
225
  key,
221
- packages: resolveRemotePackages(
222
- app.rootDir,
223
- app.packageResolved,
224
- app.remoteProducts,
225
- entry.provided,
226
- ),
226
+ packages: resolveSwiftPackages({
227
+ rootDir: app.rootDir,
228
+ resolved: app.packageResolved,
229
+ products: app.linkedProducts,
230
+ localDirs: app.localPackageDirs,
231
+ provided: entry.provided,
232
+ }),
227
233
  }
228
234
  }
229
- return entry.remote.packages
235
+ return entry.packages.packages
230
236
  }
231
237
 
232
238
  // the app target's sources compile as one executable target under this
@@ -252,10 +258,9 @@ function xcodeInputKey(layout: XcodeLayout, file: string, relative: string): str
252
258
 
253
259
  interface XcodeLayout {
254
260
  app: XcodeAppTarget
255
- /** the local and remote package targets the app links, with project-relative paths. */
256
- targets: ReturnType<typeof swiftManifestTargets>
257
- /** remote products that cannot compile, with why. */
258
- unavailable: RemotePackages['unavailable']
261
+ /** framework and package targets, with root-relative paths and the nested target directories they exclude. */
262
+ targets: (SwiftPackageTarget & { exclude: string[] })[]
263
+ packages: SwiftPackages
259
264
  /** the input directory each app and framework source compiles under. */
260
265
  targetDirs: Map<string, string>
261
266
  files: string[]
@@ -281,56 +286,50 @@ function xcodeLayout(root: string): XcodeLayout | null {
281
286
  path: dir,
282
287
  executable: false,
283
288
  resources: false,
289
+ exclude: [],
284
290
  languageMode: framework.languageMode,
285
291
  mainActorIsolation: framework.mainActorIsolation,
286
292
  })
287
293
  }
288
- for (const dir of app.localPackages) {
289
- const manifestFile = path.join(dir, 'Package.swift')
290
- files.add(manifestFile)
291
- const manifest = fs.readFileSync(manifestFile, 'utf8')
292
- const prefix = path.relative(root, dir).split(path.sep).join(path.posix.sep)
293
- // only target directories: a package at a repository's root holds the
294
- // whole app beside its sources
295
- const declared = swiftManifestTargets(manifest).filter((target) => !target.executable)
296
- const all = declared.flatMap((target) =>
297
- sourceTreeFiles(path.join(dir, target.path), root).map((file) =>
298
- path.relative(dir, file).split(path.sep).join(path.posix.sep),
299
- ),
294
+ const packages = swiftPackages(registered, app)
295
+ for (const target of packages.targets) {
296
+ const relative = path.relative(
297
+ target.remote ? path.join(root, REMOTE_CHECKOUTS) : root,
298
+ target.dir,
300
299
  )
301
- for (const target of swiftManifestTargets(manifest, all)) {
302
- if (target.executable) continue
303
- targets.push({ ...target, path: path.posix.join(prefix, target.path) })
304
- for (const relative of all) {
305
- if (relative.startsWith(`${target.path}/`)) files.add(path.join(dir, relative))
306
- }
307
- }
308
- }
309
- const remote = remotePackages(registered, app)
310
- for (const target of remote.targets) {
300
+ if (
301
+ relative === '..' ||
302
+ relative.startsWith(`..${path.sep}`) ||
303
+ path.isAbsolute(relative)
304
+ )
305
+ throw new Error(`swift input escapes the selected package: ${target.dir}`)
306
+ const posix = relative.split(path.sep).join(path.posix.sep)
311
307
  targets.push({
312
308
  ...target,
313
- path: path.posix.join(
314
- XCODE_PACKAGES_DIR,
315
- path
316
- .relative(path.join(root, REMOTE_CHECKOUTS), target.dir)
317
- .split(path.sep)
318
- .join(path.posix.sep),
319
- ),
309
+ path: target.remote ? path.posix.join(XCODE_PACKAGES_DIR, posix) : posix,
310
+ exclude: [],
320
311
  })
312
+ if (!target.remote) files.add(path.join(target.packageDir, 'Package.swift'))
321
313
  for (const file of target.files) files.add(file)
322
314
  }
315
+ // swiftpm assigns a file to one target: a target directory nested in
316
+ // another's is that one's exclusion
317
+ for (const target of targets) {
318
+ target.exclude = targets
319
+ .filter((other) => other.path.startsWith(`${target.path}/`))
320
+ .map((other) => other.path.slice(target.path.length + 1))
321
+ }
323
322
  return {
324
323
  app,
325
324
  targets,
326
- unavailable: remote.unavailable,
325
+ packages,
327
326
  targetDirs,
328
327
  files: [...files].sort(),
329
328
  }
330
329
  }
331
330
 
332
331
  // the manifest the compile reads for an xcode app: the app target and each
333
- // linked local package target, in the forms swiftManifestTargets scans.
332
+ // framework and package target, in the forms swiftManifestTargets scans.
334
333
  function xcodeManifest(layout: XcodeLayout): string {
335
334
  const settings = (mode: number, mainActor: boolean) =>
336
335
  `swiftSettings: [.swiftLanguageMode(.v${mode})${mainActor ? ', .defaultIsolation(MainActor.self)' : ''}]`
@@ -341,7 +340,7 @@ function xcodeManifest(layout: XcodeLayout): string {
341
340
  ` .executableTarget(name: "${layout.app.moduleName}", path: "${XCODE_APP_DIR}", ${settings(layout.app.languageMode, layout.app.mainActorIsolation)}),`,
342
341
  ...layout.targets.map(
343
342
  (target) =>
344
- ` .target(name: "${target.name}", path: "${target.path}", ${target.resources ? 'resources: [], ' : ''}${settings(target.languageMode, target.mainActorIsolation)}),`,
343
+ ` .target(name: "${target.name}", path: "${target.path}", ${target.exclude.length > 0 ? `exclude: ${JSON.stringify(target.exclude)}, ` : ''}${target.resources ? 'resources: [], ' : ''}${settings(target.languageMode, target.mainActorIsolation)}),`,
345
344
  ),
346
345
  '])',
347
346
  '',
@@ -941,19 +940,20 @@ function missingModulesMessage(
941
940
  missing: Map<string, string[]>,
942
941
  ): string {
943
942
  const layout = xcodeLayout(packagePath)
944
- const remote = new Map(
945
- (layout?.app.remoteProducts ?? []).map((entry) => [
946
- entry.product,
947
- entry.repositoryURL,
948
- ]),
949
- )
943
+ const gaps = new Map([
944
+ ...(layout?.app.binaryFrameworks ?? []).map(
945
+ (name) =>
946
+ [name, { source: 'the project', reason: 'a vendored binary framework' }] as const,
947
+ ),
948
+ ...(layout?.packages.modules ?? []),
949
+ ...(layout?.packages.unavailable ?? []),
950
+ ])
950
951
  const width = Math.max(...[...missing.keys()].map((name) => name.length))
951
952
  return [
952
953
  `Peach does not provide ${missing.size} module${missing.size === 1 ? '' : 's'} this app imports:`,
953
954
  ...[...missing].map(([name, files]) => {
954
- const source = remote.get(name)
955
- const reason = layout?.unavailable.get(name)?.reason
956
- return ` ${name.padEnd(width)} ${files.length} file${files.length === 1 ? '' : 's'}${source ? `, package ${source}` : ''}${reason ? ` (${reason})` : ''}`
955
+ const gap = gaps.get(name)
956
+ return ` ${name.padEnd(width)} ${files.length} file${files.length === 1 ? '' : 's'}${gap ? `, ${gap.source.includes('://') ? `package ${gap.source}` : gap.source.startsWith('/') ? `package ${path.relative(packagePath, gap.source) || '.'}` : gap.source} (${gap.reason})` : ''}`
957
957
  }),
958
958
  ].join('\n')
959
959
  }
@@ -0,0 +1,307 @@
1
+ // a swift package's manifest as swiftpm evaluates it. a manifest is swift
2
+ // code: names come from constants, products from helper extensions, and a
3
+ // version-specific Package@swift-x.swift can stand in for Package.swift, so
4
+ // only swiftpm reads it right. `swift package dump-package` runs it, through
5
+ // xcode's swiftpm on macOS (the one xcode resolves an app's packages with)
6
+ // and the pinned swift.org toolchain on linux, where no xcode exists.
7
+
8
+ import { execFileSync } from 'node:child_process'
9
+ import { createHash } from 'node:crypto'
10
+ import fs from 'node:fs'
11
+ import os from 'node:os'
12
+ import path from 'node:path'
13
+ import lock from './swift-distribution.lock.json' with { type: 'json' }
14
+ import { swiftToolchainBin } from './swift-toolchain'
15
+ import type { SwiftTargetDependency } from './swift-package'
16
+
17
+ const TARGET_TYPES = [
18
+ 'regular',
19
+ 'executable',
20
+ 'test',
21
+ 'binary',
22
+ 'system',
23
+ 'macro',
24
+ 'plugin',
25
+ ] as const
26
+ export type SwiftManifestTargetType = (typeof TARGET_TYPES)[number]
27
+ const isTargetType = (value: string | undefined): value is SwiftManifestTargetType =>
28
+ TARGET_TYPES.some((type) => type === value)
29
+
30
+ export interface SwiftManifestTarget {
31
+ name: string
32
+ type: SwiftManifestTargetType
33
+ /** the target directory, absolute. */
34
+ dir: string
35
+ /** the target's `sources:`, relative to `dir`; null means all of `dir`. */
36
+ sources: string[] | null
37
+ /** the target's `exclude:`, relative to `dir`. */
38
+ exclude: string[]
39
+ /** dependencies that link on iOS with the package's default traits. */
40
+ dependencies: SwiftTargetDependency[]
41
+ languageMode: number
42
+ mainActorIsolation: boolean
43
+ /** true when the target declares `resources:`. */
44
+ declaresResources: boolean
45
+ }
46
+
47
+ export interface SwiftPackageManifest {
48
+ dir: string
49
+ /** library product name to the targets it vends. */
50
+ products: Map<string, string[]>
51
+ targets: Map<string, SwiftManifestTarget>
52
+ /** package dependencies by identity and by any `name:` they carry. */
53
+ packages: Map<string, { url: string } | { dir: string }>
54
+ }
55
+
56
+ // the dump's tagged unions: `{ "<case>": [payload...] }`
57
+ type Json = null | boolean | number | string | Json[] | { [key: string]: Json }
58
+ const record = (value: Json | undefined): Record<string, Json> =>
59
+ value && typeof value === 'object' && !Array.isArray(value) ? value : {}
60
+ const array = (value: Json | undefined): Json[] => (Array.isArray(value) ? value : [])
61
+ const string = (value: Json | undefined): string | undefined =>
62
+ typeof value === 'string' ? value : undefined
63
+
64
+ function swiftCommand(): { file: string; args: string[] } {
65
+ return process.platform === 'darwin'
66
+ ? { file: 'xcrun', args: ['swift'] }
67
+ : { file: path.join(swiftToolchainBin(lock.release), 'swift'), args: [] }
68
+ }
69
+
70
+ const cacheDir = path.join(os.tmpdir(), 'peach-swift-manifests')
71
+ const evaluated = new Map<string, SwiftPackageManifest>()
72
+
73
+ function dump(dir: string): { key: string; raw: Record<string, Json> } {
74
+ const manifests = fs
75
+ .readdirSync(dir)
76
+ .filter((name) => /^Package(@swift-[\d.]+)?\.swift$/.test(name))
77
+ .sort()
78
+ const command = swiftCommand()
79
+ const key = createHash('sha256')
80
+ .update(JSON.stringify([command, path.resolve(dir)]))
81
+ .update(
82
+ manifests
83
+ .map((name) => `${name}\0${fs.readFileSync(path.join(dir, name), 'utf8')}`)
84
+ .join('\0'),
85
+ )
86
+ .digest('hex')
87
+ .slice(0, 32)
88
+ const cached = path.join(cacheDir, `${key}.json`)
89
+ if (fs.existsSync(cached))
90
+ return { key, raw: record(JSON.parse(fs.readFileSync(cached, 'utf8'))) }
91
+ let output: string
92
+ try {
93
+ output = execFileSync(
94
+ command.file,
95
+ [
96
+ ...command.args,
97
+ 'package',
98
+ '--package-path',
99
+ dir,
100
+ // swiftpm's scratch files never land in the app's own package
101
+ '--scratch-path',
102
+ path.join(cacheDir, `${key}.build`),
103
+ 'dump-package',
104
+ ],
105
+ {
106
+ encoding: 'utf8',
107
+ stdio: ['ignore', 'pipe', 'pipe'],
108
+ maxBuffer: 64 * 1024 * 1024,
109
+ },
110
+ )
111
+ } catch (error) {
112
+ const stderr =
113
+ error instanceof Error && 'stderr' in error ? String(error.stderr).trim() : ''
114
+ throw new Error(
115
+ `swiftpm could not evaluate ${path.join(dir, 'Package.swift')}${stderr ? `: ${stderr}` : ''}`,
116
+ )
117
+ }
118
+ fs.mkdirSync(cacheDir, { recursive: true })
119
+ fs.writeFileSync(cached, output)
120
+ return { key, raw: record(JSON.parse(output)) }
121
+ }
122
+
123
+ // the traits a consumer gets by default: `default`'s, and what they enable
124
+ function defaultTraits(manifest: Record<string, Json>): Set<string> {
125
+ const traits = new Map(
126
+ array(manifest.traits).map((trait) => [
127
+ string(record(trait).name) ?? '',
128
+ array(record(trait).enabledTraits).flatMap((name) => string(name) ?? []),
129
+ ]),
130
+ )
131
+ const enabled = new Set<string>()
132
+ const pending = [...(traits.get('default') ?? [])]
133
+ while (pending.length > 0) {
134
+ const name = pending.pop()!
135
+ if (enabled.has(name)) continue
136
+ enabled.add(name)
137
+ pending.push(...(traits.get(name) ?? []))
138
+ }
139
+ return enabled
140
+ }
141
+
142
+ // a condition holds on iOS when it names no platforms or names iOS, and its
143
+ // traits, if any, include one the package enables by default
144
+ function linksOnIOS(condition: Json | undefined, traits: Set<string>): boolean {
145
+ const { platformNames, traits: needed } = record(condition)
146
+ const platforms = array(platformNames)
147
+ if (platforms.length > 0 && !platforms.includes('ios')) return false
148
+ const gates = array(needed)
149
+ return gates.length === 0 || gates.some((name) => traits.has(string(name) ?? ''))
150
+ }
151
+
152
+ /** the manifest of the package at `dir`, evaluated once per manifest content. */
153
+ export function swiftPackageManifest(dir: string): SwiftPackageManifest {
154
+ dir = fs.realpathSync(dir)
155
+ const { key, raw } = dump(dir)
156
+ const known = evaluated.get(key)
157
+ if (known) return known
158
+ const traits = defaultTraits(raw)
159
+ const tools = Number(
160
+ /^(\d+)/.exec(string(record(raw.toolsVersion)._version) ?? '5')?.[1] ?? 5,
161
+ )
162
+ const declaredModes = array(raw.swiftLanguageVersions).flatMap((mode) => {
163
+ const major = Number(/^(\d+)/.exec(string(mode) ?? '')?.[1])
164
+ return Number.isFinite(major) ? [major] : []
165
+ })
166
+ // swiftpm builds in the newest mode a package declares; with none, tools
167
+ // 6 and later default to 6. peach compiles modes 5 and 6.
168
+ const packageMode = Math.max(
169
+ 5,
170
+ declaredModes.length > 0 ? Math.max(...declaredModes) : tools >= 6 ? 6 : 5,
171
+ )
172
+
173
+ const packages: SwiftPackageManifest['packages'] = new Map()
174
+ for (const entry of array(raw.dependencies)) {
175
+ const { sourceControl, fileSystem } = record(entry)
176
+ const source = record(array(sourceControl ?? fileSystem)[0])
177
+ const identity = string(source.identity)
178
+ if (!identity) continue
179
+ const url = string(record(array(record(source.location).remote)[0]).urlString)
180
+ const local = string(source.path)
181
+ const value = url ? { url } : local ? { dir: local } : null
182
+ if (!value) continue
183
+ packages.set(identity, value)
184
+ const alias = string(source.nameForTargetDependencyResolutionOnly)
185
+ if (alias) packages.set(alias.toLowerCase(), value)
186
+ }
187
+
188
+ const products = new Map<string, string[]>()
189
+ for (const product of array(raw.products)) {
190
+ const { name, targets, type } = record(product)
191
+ if (!string(name) || !('library' in record(type))) continue
192
+ products.set(
193
+ string(name)!,
194
+ array(targets).flatMap((target) => string(target) ?? []),
195
+ )
196
+ }
197
+
198
+ const targets = new Map<string, SwiftManifestTarget>()
199
+ const names = new Set(array(raw.targets).map((target) => string(record(target).name)))
200
+ for (const entry of array(raw.targets)) {
201
+ const target = record(entry)
202
+ const name = string(target.name)
203
+ const kind = string(target.type)
204
+ if (!name || !isTargetType(kind)) continue
205
+ const declared = string(target.path)
206
+ const roots = kind === 'test' ? ['Tests'] : ['Sources', 'Source', 'src', 'srcs']
207
+ const targetDir = declared
208
+ ? path.resolve(dir, declared)
209
+ : (roots
210
+ .map((root) => path.join(dir, root, name))
211
+ .find((candidate) => fs.existsSync(candidate)) ??
212
+ path.join(dir, roots[0]!, name))
213
+ const dependencies: SwiftTargetDependency[] = []
214
+ for (const dependency of array(target.dependencies)) {
215
+ const { target: local, product, byName } = record(dependency)
216
+ const payload = array(local ?? product ?? byName)
217
+ if (!linksOnIOS(payload[payload.length - 1], traits)) continue
218
+ const dependencyName = string(payload[0])
219
+ if (!dependencyName) continue
220
+ if (product)
221
+ dependencies.push({
222
+ kind: 'product',
223
+ name: dependencyName,
224
+ package: (string(payload[1]) ?? '').toLowerCase(),
225
+ })
226
+ // a by-name dependency that is no target here is the product of that
227
+ // name in the dependency package of the same name
228
+ else if (local || names.has(dependencyName))
229
+ dependencies.push({ kind: 'target', name: dependencyName })
230
+ else
231
+ dependencies.push({
232
+ kind: 'product',
233
+ name: dependencyName,
234
+ package: dependencyName.toLowerCase(),
235
+ })
236
+ }
237
+ let languageMode = packageMode
238
+ let mainActorIsolation = false
239
+ for (const setting of array(target.settings)) {
240
+ const settingKind = record(record(setting).kind)
241
+ const mode = string(record(settingKind.swiftLanguageMode)._0)
242
+ if (mode)
243
+ languageMode = Math.max(5, Number(/^(\d+)/.exec(mode)?.[1] ?? languageMode))
244
+ if (string(record(settingKind.defaultIsolation)._0) === 'MainActor')
245
+ mainActorIsolation = true
246
+ }
247
+ targets.set(name, {
248
+ name,
249
+ type: kind,
250
+ dir: targetDir,
251
+ sources:
252
+ target.sources == null
253
+ ? null
254
+ : array(target.sources).flatMap((file) => string(file) ?? []),
255
+ exclude: array(target.exclude).flatMap((file) => string(file) ?? []),
256
+ dependencies,
257
+ languageMode,
258
+ mainActorIsolation,
259
+ declaresResources: array(target.resources).length > 0,
260
+ })
261
+ }
262
+ const manifest = { dir, products, targets, packages }
263
+ evaluated.set(key, manifest)
264
+ return manifest
265
+ }
266
+
267
+ const within = (file: string, roots: string[]) =>
268
+ roots.some((root) => file === root || file.startsWith(`${root}${path.sep}`))
269
+
270
+ function walk(dir: string): string[] {
271
+ if (!fs.existsSync(dir)) return []
272
+ if (fs.statSync(dir).isFile()) return [dir]
273
+ const out: string[] = []
274
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
275
+ if (entry.name.startsWith('.')) continue
276
+ const full = path.join(dir, entry.name)
277
+ if (entry.isDirectory()) out.push(...walk(full))
278
+ else if (entry.isFile()) out.push(full)
279
+ }
280
+ return out
281
+ }
282
+
283
+ /**
284
+ * the files swiftpm assigns a target, absolute and sorted: its `sources:`
285
+ * (else its whole directory), less its `exclude:` and the directories of
286
+ * the package's other targets nested inside it.
287
+ */
288
+ export function swiftTargetFiles(
289
+ manifest: SwiftPackageManifest,
290
+ target: SwiftManifestTarget,
291
+ ): string[] {
292
+ const included = target.sources
293
+ ? target.sources.map((source) => path.resolve(target.dir, source))
294
+ : [target.dir]
295
+ const excluded = [
296
+ ...target.exclude.map((file) => path.resolve(target.dir, file)),
297
+ ...[...manifest.targets.values()].flatMap((other) =>
298
+ other !== target && within(other.dir, [target.dir]) && other.dir !== target.dir
299
+ ? [other.dir]
300
+ : [],
301
+ ),
302
+ ]
303
+ return included
304
+ .flatMap(walk)
305
+ .filter((file) => !within(file, excluded))
306
+ .sort()
307
+ }