ios_emulators.dart 2.05 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import '../base/process.dart';
8
import '../device.dart';
9
import '../emulator.dart';
10
import '../globals_null_migrated.dart' as globals;
11
import 'simulators.dart';
12 13 14

class IOSEmulators extends EmulatorDiscovery {
  @override
15
  bool get supportsPlatform => globals.platform.isMacOS;
16 17

  @override
18
  bool get canListAnything => globals.iosWorkflow.canListEmulators;
19 20

  @override
21
  Future<List<Emulator>> get emulators async => getEmulators();
22 23 24

  @override
  bool get canLaunchAnything => canListAnything;
25 26 27
}

class IOSEmulator extends Emulator {
28
  const IOSEmulator(String id) : super(id, true);
29 30

  @override
31
  String get name => 'iOS Simulator';
32 33 34 35

  @override
  String get manufacturer => 'Apple';

36 37 38 39
  @override
  Category get category => Category.mobile;

  @override
40
  PlatformType get platformType => PlatformType.ios;
41

42
  @override
43
  Future<void> launch({bool coldBoot = false}) async {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    Future<bool> launchSimulator(List<String> additionalArgs) async {
      final List<String> args = <String>[
        'open',
        ...additionalArgs,
        '-a',
        globals.xcode.getSimulatorPath(),
      ];

      final RunResult launchResult = await globals.processUtils.run(args);
      if (launchResult.exitCode != 0) {
        globals.printError('$launchResult');
        return false;
      }
      return true;
    }

    // First run with `-n` to force a device to boot if there isn't already one
    if (!await launchSimulator(<String>['-n'])) {
      return;
63
    }
64 65 66 67

    // Run again to force it to Foreground (using -n doesn't force existing
    // devices to the foreground)
    await launchSimulator(<String>[]);
68 69
  }
}
70 71 72 73 74 75 76 77 78 79

/// Return the list of iOS Simulators (there can only be zero or one).
List<IOSEmulator> getEmulators() {
  final String simulatorPath = globals.xcode.getSimulatorPath();
  if (simulatorPath == null) {
    return <IOSEmulator>[];
  }

  return <IOSEmulator>[const IOSEmulator(iosSimulatorId)];
}