ios_emulators.dart 2.2 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
import '../base/common.dart';
6
import '../base/process.dart';
7
import '../device.dart';
8
import '../emulator.dart';
9
import '../globals.dart' as globals;
10
import 'simulators.dart';
11 12 13

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

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

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

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

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

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

  @override
  String get manufacturer => 'Apple';

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

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

41
  @override
42
  Future<void> launch({bool coldBoot = false}) async {
43 44 45 46
    final String? simulatorPath = globals.xcode?.getSimulatorPath();
    if (simulatorPath == null) {
      throwToolExit('Could not find Simulator app');
    }
47 48 49 50 51
    Future<bool> launchSimulator(List<String> additionalArgs) async {
      final List<String> args = <String>[
        'open',
        ...additionalArgs,
        '-a',
52
        simulatorPath,
53 54 55 56 57 58 59 60 61 62 63 64 65
      ];

      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;
66
    }
67 68 69 70

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

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

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