ios_emulators.dart 1.98 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 8
// 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/platform.dart';
import '../base/process.dart';
9
import '../device.dart';
10 11
import '../emulator.dart';
import '../globals.dart';
12
import '../macos/xcode.dart';
13
import 'ios_workflow.dart';
14
import 'simulators.dart';
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

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

  @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';

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 49 50
      final List<String> args = <String>[
        'open',
        ...additionalArgs,
        '-a',
        xcode.getSimulatorPath(),
      ];
51

52
      final RunResult launchResult = await processUtils.run(args);
53 54 55 56 57
      if (launchResult.exitCode != 0) {
        printError('$launchResult');
        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 = xcode.getSimulatorPath();
74 75 76 77
  if (simulatorPath == null) {
    return <IOSEmulator>[];
  }

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