|
| 1 | +import { |
| 2 | + CreatePlan, |
| 3 | + DestroyPlan, |
| 4 | + ExampleConfig, |
| 5 | + ModifyPlan, |
| 6 | + ParameterChange, |
| 7 | + Resource, |
| 8 | + ResourceSettings, |
| 9 | + SpawnStatus, |
| 10 | + getPty, |
| 11 | + z, |
| 12 | +} from '@codifycli/plugin-core'; |
| 13 | +import { OS } from '@codifycli/schemas'; |
| 14 | + |
| 15 | +const schema = z.object({ |
| 16 | + name: z |
| 17 | + .string() |
| 18 | + .describe('Name for the iOS simulator instance (e.g. "iPhone 15 Dev")'), |
| 19 | + deviceType: z |
| 20 | + .string() |
| 21 | + .describe('Device type identifier (e.g. "com.apple.CoreSimulator.SimDeviceType.iPhone-15")'), |
| 22 | + runtime: z |
| 23 | + .string() |
| 24 | + .describe('Runtime identifier (e.g. "com.apple.CoreSimulator.SimRuntime.iOS-18-0")'), |
| 25 | + state: z |
| 26 | + .enum(['Booted', 'Shutdown']) |
| 27 | + .optional() |
| 28 | + .describe('Desired runtime state of the simulator. Defaults to Shutdown.'), |
| 29 | +}); |
| 30 | + |
| 31 | +export type IosSimulatorConfig = z.infer<typeof schema>; |
| 32 | + |
| 33 | +interface SimDevice { |
| 34 | + udid: string; |
| 35 | + name: string; |
| 36 | + state: string; |
| 37 | + deviceTypeIdentifier: string; |
| 38 | +} |
| 39 | + |
| 40 | +interface SimctlDevicesOutput { |
| 41 | + devices: Record<string, SimDevice[]>; |
| 42 | +} |
| 43 | + |
| 44 | +const defaultConfig: Partial<IosSimulatorConfig> & { os: any } = { |
| 45 | + name: '<Replace me here!>', |
| 46 | + deviceType: 'com.apple.CoreSimulator.SimDeviceType.iPhone-15', |
| 47 | + runtime: 'com.apple.CoreSimulator.SimRuntime.iOS-18-0', |
| 48 | + state: 'Shutdown', |
| 49 | + os: ['macOS'], |
| 50 | +}; |
| 51 | + |
| 52 | +const exampleBasic: ExampleConfig = { |
| 53 | + title: 'iPhone 15 simulator for development', |
| 54 | + description: 'Create an iPhone 15 simulator running iOS 18 for use in development and UI testing.', |
| 55 | + configs: [{ |
| 56 | + type: 'ios-simulator', |
| 57 | + name: 'iPhone 15 Dev', |
| 58 | + deviceType: 'com.apple.CoreSimulator.SimDeviceType.iPhone-15', |
| 59 | + runtime: 'com.apple.CoreSimulator.SimRuntime.iOS-18-0', |
| 60 | + state: 'Shutdown', |
| 61 | + os: ['macOS'], |
| 62 | + }], |
| 63 | +}; |
| 64 | + |
| 65 | +const exampleMultiDevice: ExampleConfig = { |
| 66 | + title: 'iPhone and iPad simulator setup', |
| 67 | + description: 'Install Xcode Command Line Tools and create an iPhone and iPad simulator for cross-device testing.', |
| 68 | + configs: [ |
| 69 | + { type: 'xcode-tools', os: ['macOS'] }, |
| 70 | + { |
| 71 | + type: 'ios-simulator', |
| 72 | + name: 'iPhone 15 Pro', |
| 73 | + deviceType: 'com.apple.CoreSimulator.SimDeviceType.iPhone-15-Pro', |
| 74 | + runtime: 'com.apple.CoreSimulator.SimRuntime.iOS-18-0', |
| 75 | + state: 'Shutdown', |
| 76 | + os: ['macOS'], |
| 77 | + }, |
| 78 | + { |
| 79 | + type: 'ios-simulator', |
| 80 | + name: 'iPad Pro 11-inch', |
| 81 | + deviceType: 'com.apple.CoreSimulator.SimDeviceType.iPad-Pro-11-inch-M4', |
| 82 | + runtime: 'com.apple.CoreSimulator.SimRuntime.iOS-18-0', |
| 83 | + state: 'Shutdown', |
| 84 | + os: ['macOS'], |
| 85 | + }, |
| 86 | + ], |
| 87 | +}; |
| 88 | + |
| 89 | +export class IosSimulatorResource extends Resource<IosSimulatorConfig> { |
| 90 | + getSettings(): ResourceSettings<IosSimulatorConfig> { |
| 91 | + return { |
| 92 | + id: 'ios-simulator', |
| 93 | + defaultConfig, |
| 94 | + exampleConfigs: { |
| 95 | + example1: exampleBasic, |
| 96 | + example2: exampleMultiDevice, |
| 97 | + }, |
| 98 | + operatingSystems: [OS.Darwin], |
| 99 | + dependencies: ['xcode-tools'], |
| 100 | + schema, |
| 101 | + parameterSettings: { |
| 102 | + state: { type: 'string', canModify: true }, |
| 103 | + }, |
| 104 | + allowMultiple: { |
| 105 | + identifyingParameters: ['name'], |
| 106 | + }, |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + async refresh(parameters: Partial<IosSimulatorConfig>): Promise<Partial<IosSimulatorConfig> | null> { |
| 111 | + const $ = getPty(); |
| 112 | + |
| 113 | + const { status, data } = await $.spawnSafe('xcrun simctl list devices --json'); |
| 114 | + if (status !== SpawnStatus.SUCCESS) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + let parsed: SimctlDevicesOutput; |
| 119 | + try { |
| 120 | + parsed = JSON.parse(data); |
| 121 | + } catch { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + for (const [runtimeId, devices] of Object.entries(parsed.devices)) { |
| 126 | + const match = devices.find((d) => d.name === parameters.name); |
| 127 | + if (match) { |
| 128 | + return { |
| 129 | + name: match.name, |
| 130 | + deviceType: match.deviceTypeIdentifier, |
| 131 | + runtime: runtimeId, |
| 132 | + state: match.state === 'Booted' ? 'Booted' : 'Shutdown', |
| 133 | + }; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + async create(plan: CreatePlan<IosSimulatorConfig>): Promise<void> { |
| 141 | + const $ = getPty(); |
| 142 | + const { name, deviceType, runtime, state } = plan.desiredConfig; |
| 143 | + |
| 144 | + // xcrun simctl create prints the new simulator's UDID to stdout |
| 145 | + const { data: udid } = await $.spawn( |
| 146 | + `xcrun simctl create "${name}" "${deviceType}" "${runtime}"`, |
| 147 | + { interactive: true } |
| 148 | + ); |
| 149 | + |
| 150 | + if (state === 'Booted') { |
| 151 | + await $.spawn(`xcrun simctl boot "${udid.trim()}"`, { interactive: true }); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + async modify(pc: ParameterChange<IosSimulatorConfig>, plan: ModifyPlan<IosSimulatorConfig>): Promise<void> { |
| 156 | + if (pc.name !== 'state') return; |
| 157 | + |
| 158 | + const $ = getPty(); |
| 159 | + const udid = await this.getUdidByName(plan.desiredConfig.name); |
| 160 | + if (!udid) return; |
| 161 | + |
| 162 | + if (plan.desiredConfig.state === 'Booted') { |
| 163 | + await $.spawn(`xcrun simctl boot "${udid}"`, { interactive: true }); |
| 164 | + } else { |
| 165 | + await $.spawn(`xcrun simctl shutdown "${udid}"`, { interactive: true }); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + async destroy(plan: DestroyPlan<IosSimulatorConfig>): Promise<void> { |
| 170 | + const $ = getPty(); |
| 171 | + const udid = await this.getUdidByName(plan.currentConfig.name); |
| 172 | + if (!udid) return; |
| 173 | + |
| 174 | + await $.spawn(`xcrun simctl delete "${udid}"`, { interactive: true }); |
| 175 | + } |
| 176 | + |
| 177 | + private async getUdidByName(name: string | undefined): Promise<string | null> { |
| 178 | + if (!name) return null; |
| 179 | + |
| 180 | + const $ = getPty(); |
| 181 | + const { status, data } = await $.spawnSafe('xcrun simctl list devices --json'); |
| 182 | + if (status !== SpawnStatus.SUCCESS) return null; |
| 183 | + |
| 184 | + try { |
| 185 | + const parsed: SimctlDevicesOutput = JSON.parse(data); |
| 186 | + for (const devices of Object.values(parsed.devices)) { |
| 187 | + const match = devices.find((d) => d.name === name); |
| 188 | + if (match) return match.udid; |
| 189 | + } |
| 190 | + } catch { |
| 191 | + // ignore parse errors |
| 192 | + } |
| 193 | + |
| 194 | + return null; |
| 195 | + } |
| 196 | +} |
0 commit comments