Future createSimulator()

in packages/simulators/lib/simulator_manager.dart [35:92]


  Future<IosSimulator> createSimulator(
      int majorVersion, int minorVersion, String device) async {
    final String runtime = 'iOS ${majorVersion}.${minorVersion}';

    // Check if the runtime is available.
    final io.ProcessResult runtimeListResult =
        await io.Process.run('xcrun', ['simctl', 'list', 'runtimes']);

    if (runtimeListResult.exitCode != 0) {
      throw Exception('Failed to boot list runtimes(versions). Command used: '
          'xcrun simctl list runtimes');
    }

    final String output = runtimeListResult.stdout as String;
    if (!output.contains(runtime)) {
      print(output);
      throw Exception('Mac does not have the requested $runtime '
          'available for simulators. Please use Xcode to install.');
    }

    // Check if the device is available.
    final io.ProcessResult deviceListResult =
        await io.Process.run('xcrun', ['simctl', 'list', 'devicetypes']);

    if (deviceListResult.exitCode != 0) {
      throw Exception('Failed to boot list available simulator device types.'
          'Command used: xcrun simctl list devicetypes');
    }

    final String deviceListOutput = deviceListResult.stdout as String;
    if (!deviceListOutput.contains(device)) {
      print(deviceListOutput);
      throw Exception('Mac does not have the requested device type $device '
          'available for simulators. Please use Xcode to install.');
    }

    // Prepate device type argument. It should look like:
    // com.apple.CoreSimulator.SimDeviceType.iPhone-11-Pro
    final String deviceTypeAsArg =
        'com.apple.CoreSimulator.SimDeviceType.${device.replaceAll(' ', '-')}';

    // Prepare runtime as argument using the versions. It should look like:
    // com.apple.CoreSimulator.SimRuntime.iOS-13-1.
    final String runtimeTypeAsArg =
        'com.apple.CoreSimulator.SimRuntime.iOS-${majorVersion}-${minorVersion}';

    final io.ProcessResult createResult = await io.Process.run('xcrun',
        ['simctl', 'create', device, deviceTypeAsArg, runtimeTypeAsArg]);

    if (createResult.exitCode != 0) {
      throw Exception('Failed to create requested simulator using $device '
          '$deviceTypeAsArg $runtimeTypeAsArg arguments.');
    }

    // Output will have the simulator id.
    final String simulatorId = createResult.stdout as String;
    return IosSimulator._(false, simulatorId.trim());
  }