ensureBooted method

Future<String?> ensureBooted(
  1. DevicePlatform platform,
  2. String deviceId
)

Boot the iOS sim (idempotent) and open Simulator; returns null on success else an error.

Implementation

Future<String?> ensureBooted(DevicePlatform platform, String deviceId) async {
  if (platform != DevicePlatform.ios) return null; // android boot deferred
  final ProcessResult boot;
  try {
    boot = await runBounded('xcrun', ['simctl', 'boot', deviceId],
        timeout: const Duration(seconds: 30), phase: 'simctl boot');
  } on Object catch (e) {
    return 'could not run simctl boot: $e';
  }
  final stderr = (boot.stderr as String?) ?? '';
  final alreadyBooted = stderr.contains('Booted'); // "current state: Booted"
  if (boot.exitCode != 0 && !alreadyBooted) {
    return 'simctl boot failed: ${stderr.trim()}';
  }
  await runBounded('open', ['-a', 'Simulator'], timeout: const Duration(seconds: 10), phase: 'opening Simulator');
  try {
    await runBounded('xcrun', ['simctl', 'bootstatus', deviceId, '-b'],
        timeout: const Duration(minutes: 3), phase: 'waiting for the simulator to finish booting');
  } on Object {
    // bootstatus just waits; proceed if unavailable.
  }
  return null;
}