ios_emulators.dart 1.97 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 '../macos/xcode.dart';
12
import 'ios_workflow.dart';
13
import 'simulators.dart';
14 15 16

class IOSEmulators extends EmulatorDiscovery {
  @override
17
  bool get supportsPlatform => globals.platform.isMacOS;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

  @override
  bool get canListAnything => iosWorkflow.canListEmulators;

  @override
  Future<List<Emulator>> get emulators async => getEmulators();
}

class IOSEmulator extends Emulator {
  IOSEmulator(String id) : super(id, true);

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

  @override
  String get manufacturer => 'Apple';

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

  @override
  PlatformType get platformType => PlatformType.ios;

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

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

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

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

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

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