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

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

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

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

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

  @override
  bool get canLaunchAnything => canListAnything;
23 24 25
}

class IOSEmulator extends Emulator {
26
  const IOSEmulator(String id) : super(id, true);
27 28 29 30 31 32 33

  @override
  String get name => 'iOS Simulator';

  @override
  String get manufacturer => 'Apple';

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

  @override
  PlatformType get platformType => PlatformType.ios;

40
  @override
41
  Future<void> launch() async {
42
    Future<bool> launchSimulator(List<String> additionalArgs) async {
43 44 45 46
      final List<String> args = <String>[
        'open',
        ...additionalArgs,
        '-a',
47
        globals.xcode.getSimulatorPath(),
48
      ];
49

50
      final RunResult launchResult = await processUtils.run(args);
51
      if (launchResult.exitCode != 0) {
52
        globals.printError('$launchResult');
53 54 55
        return false;
      }
      return true;
56
    }
57 58

    // First run with `-n` to force a device to boot if there isn't already one
59
    if (!await launchSimulator(<String>['-n'])) {
60
      return;
61
    }
62

63 64
    // Run again to force it to Foreground (using -n doesn't force existing
    // devices to the foreground)
65
    await launchSimulator(<String>[]);
66 67 68 69 70
  }
}

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

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