import { existsSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import type { IosPlugin } from './plugin-ios.ts'; const EXACT_CLI = 'exact.mjs'; const BUILT_APP = /^host\/apple: (\/.+?\.app)(?: on | \(|$)/; const ERROR_LINE = /^error(?:\[\w+\])?: /; const MISSING_HERMES = /^the pinned Hermes bundle for \S+ is not installed:/; const HERMES_INSTALL = /^\s+cargo run --manifest-path .*\/hermes-lean-sys-installer\/Cargo\.toml /; // exact2's host/apple builds the simulator's Rust code under target//: host-dev, or release with --release. // CARGO_TARGET_DIR moves it, so such a run does not say; an enclosing Cargo workspace or a size-optimized Release reads as a first build. const SIMULATOR_RUST_TARGET = process.arch === 'arm64' ? 'aarch64-apple-ios-sim' : 'x86_64-apple-ios'; const isObject = (value: unknown): value is Record => typeof value === 'object' && value !== null && !Array.isArray(value); function readManifest(root: string): Record | null { try { const manifest: unknown = JSON.parse(readFileSync(join(root, 'app.json'), 'utf8')); return isObject(manifest) ? manifest : null; } catch { return null; } } /** Whether `app.json` declares native modules, whose roster Exact may read by running code on a simulator. */ function exactDeclaresModules(manifest: Record): boolean { const { modules } = manifest; return Array.isArray(modules) ? modules.length > 0 : modules !== undefined && modules !== null; } function exactBuiltApp(lines: readonly string[]): string | null { for (const line of lines.toReversed()) { const app = BUILT_APP.exec(line)?.[1]; if (app) return app; } return null; } export const exactIosPlugin: IosPlugin = { cacheIneligible: 'Exact builds incrementally in its own target/ directory', configurationProblem(scheme, configuration) { if (scheme !== undefined || (configuration && configuration !== 'Debug' && configuration !== 'Release')) return { message: 'Exact apps have no Xcode schemes; --configuration takes Debug (the development app) or Release (./exact.mjs ios --release).', remedy: 'Drop --scheme, and pass --configuration Debug or Release.', }; return null; }, bundleId(root) { const app = readManifest(root)?.app; const id = isObject(app) ? app.id : undefined; return typeof id === 'string' && id !== '' ? id : null; }, simulator(root) { const manifest = readManifest(root); return !manifest || !exactDeclaresModules(manifest) ? null : { reason: 'an Exact app that declares native modules runs on a simulator on this Mac, which a build here uses to read them', buildOnly: { message: "This Exact app declares native modules in app.json, and Exact may run code on a simulator to read them, so on this Mac it builds only next to this workspace's own booted simulator.", remedy: 'Build and run it with stim ios, or build it on a build machine with --remote-build, which gives the build a simulator of its own.', }, }; }, incremental(root, configuration, env) { if (env.CARGO_TARGET_DIR) return undefined; const profile = configuration === 'Release' ? 'release' : 'host-dev'; return existsSync(join(root, 'target', SIMULATOR_RUST_TARGET, profile)); }, build({ root, configuration, simulator, env }) { const buildEnv = { ...env }; if (simulator) buildEnv.EXACT_SIM = simulator; else delete buildEnv.EXACT_SIM; return { argv: [join(root, EXACT_CLI), 'ios', '--no-install', ...(configuration === 'Release' ? ['--release'] : [])], env: { ...buildEnv, FORCE_COLOR: '0' }, }; }, result({ lines, exitCode, error }) { const errorLines = lines .filter( (line, index, all) => ERROR_LINE.test(line) || MISSING_HERMES.test(line) || (HERMES_INSTALL.test(line) && MISSING_HERMES.test(all[index - 1] ?? '')), ) .map((line) => line.trimStart()); const lastLine = lines.at(-1); return { app: error || exitCode !== 0 ? null : exactBuiltApp(lines), diagnostics: [ ...(error ? [`could not run ./${EXACT_CLI}: ${error}`] : []), ...(!error && exitCode === 0 ? [`./${EXACT_CLI} ios reported no built app with a bundle id`] : []), ...(errorLines.length === 0 && !error && exitCode !== 0 && lastLine ? [lastLine] : errorLines), ], }; }, doctor: () => [ { level: 'note', title: 'Exact application', detail: `stim ios builds with ./${EXACT_CLI} ios --no-install (--release for --configuration Release) on the workspace's own simulator, or with no simulator for a hosted one, and launches it without Metro. Run Exact's own commands (test, agent) with EXACT_SIM set to a simulator on this Mac.`, fix: null, }, ], };