|  | 
|  | 1 | +/** | 
|  | 2 | + * Tool to boot iOS simulator | 
|  | 3 | + */ | 
|  | 4 | +import { z } from 'zod'; | 
|  | 5 | +import { Simctl } from 'node-simctl'; | 
|  | 6 | +import { IOSManager } from '../devicemanager/ios-manager.js'; | 
|  | 7 | + | 
|  | 8 | +export default function bootSimulator(server: any): void { | 
|  | 9 | +  server.addTool({ | 
|  | 10 | +    name: 'boot_simulator', | 
|  | 11 | +    description: | 
|  | 12 | +      'Boot an iOS simulator and wait for it to be ready. This speeds up subsequent session creation by ensuring the simulator is already running.', | 
|  | 13 | +    parameters: z.object({ | 
|  | 14 | +      udid: z | 
|  | 15 | +        .string() | 
|  | 16 | +        .describe( | 
|  | 17 | +          'The UDID of the iOS simulator to boot. Use select_platform and select_device tools first to get the UDID.' | 
|  | 18 | +        ), | 
|  | 19 | +    }), | 
|  | 20 | +    annotations: { | 
|  | 21 | +      readOnlyHint: false, | 
|  | 22 | +      openWorldHint: false, | 
|  | 23 | +    }, | 
|  | 24 | +    execute: async (args: any, context: any): Promise<any> => { | 
|  | 25 | +      try { | 
|  | 26 | +        const { udid } = args; | 
|  | 27 | + | 
|  | 28 | +        // Verify it's a macOS system | 
|  | 29 | +        if (process.platform !== 'darwin') { | 
|  | 30 | +          throw new Error('iOS simulators can only be booted on macOS systems'); | 
|  | 31 | +        } | 
|  | 32 | + | 
|  | 33 | +        const iosManager = IOSManager.getInstance(); | 
|  | 34 | +        const simulators = await iosManager.listSimulators(); | 
|  | 35 | + | 
|  | 36 | +        // Find the simulator with the given UDID | 
|  | 37 | +        const simulator = simulators.find(sim => sim.udid === udid); | 
|  | 38 | + | 
|  | 39 | +        if (!simulator) { | 
|  | 40 | +          throw new Error( | 
|  | 41 | +            `Simulator with UDID "${udid}" not found. Please use select_platform and select_device tools to get a valid UDID.` | 
|  | 42 | +          ); | 
|  | 43 | +        } | 
|  | 44 | + | 
|  | 45 | +        // Check current state | 
|  | 46 | +        if (simulator.state === 'Booted') { | 
|  | 47 | +          return { | 
|  | 48 | +            content: [ | 
|  | 49 | +              { | 
|  | 50 | +                type: 'text', | 
|  | 51 | +                text: `✅ Simulator "${simulator.name}" is already booted and ready!\n\nUDID: ${udid}\niOS Version: ${simulator.platform || 'Unknown'}\nState: ${simulator.state}`, | 
|  | 52 | +              }, | 
|  | 53 | +            ], | 
|  | 54 | +          }; | 
|  | 55 | +        } | 
|  | 56 | + | 
|  | 57 | +        console.log(`Booting iOS simulator: ${simulator.name} (${udid})...`); | 
|  | 58 | + | 
|  | 59 | +        const simctl = new Simctl(); | 
|  | 60 | +        simctl.udid = udid; | 
|  | 61 | + | 
|  | 62 | +        // Boot the device | 
|  | 63 | +        await simctl.bootDevice(); | 
|  | 64 | +        console.log( | 
|  | 65 | +          'Simulator boot initiated, waiting for boot to complete...' | 
|  | 66 | +        ); | 
|  | 67 | + | 
|  | 68 | +        // Wait for boot to complete with 2 minute timeout | 
|  | 69 | +        await simctl.startBootMonitor({ timeout: 120000 }); | 
|  | 70 | + | 
|  | 71 | +        console.log(`Simulator "${simulator.name}" booted successfully!`); | 
|  | 72 | + | 
|  | 73 | +        return { | 
|  | 74 | +          content: [ | 
|  | 75 | +            { | 
|  | 76 | +              type: 'text', | 
|  | 77 | +              text: `✅ Simulator booted successfully!\n\nDevice: ${simulator.name}\nUDID: ${udid}\niOS Version: ${simulator.platform || 'Unknown'}\n\n🚀 The simulator is now ready for session creation. You can now use the create_session tool to start an Appium session.`, | 
|  | 78 | +            }, | 
|  | 79 | +          ], | 
|  | 80 | +        }; | 
|  | 81 | +      } catch (error: any) { | 
|  | 82 | +        console.error('Error booting simulator:', error); | 
|  | 83 | +        throw new Error(`Failed to boot simulator: ${error.message}`); | 
|  | 84 | +      } | 
|  | 85 | +    }, | 
|  | 86 | +  }); | 
|  | 87 | +} | 
0 commit comments