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

import 'dart:async';

import '../base/process.dart';
8
import '../device.dart';
9
import '../emulator.dart';
10
import '../globals.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 21

  @override
  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 31 32 33 34 35

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

  @override
  String get manufacturer => 'Apple';

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

  @override
  PlatformType get platformType => PlatformType.ios;

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

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

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

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

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

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