Skip to content

Commit

Permalink
Revert "Launch named iOS simulators (#72323)" (#72447)
Browse files Browse the repository at this point in the history
This reverts commit 84a7a61.
  • Loading branch information
jmagman committed Dec 16, 2020
1 parent ef8959a commit cdacae8
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class AndroidEmulator extends Emulator {
Category get category => Category.mobile;

@override
String get platformDisplay => PlatformType.android.toString();
PlatformType get platformType => PlatformType.android;

String _prop(String name) => _properties != null ? _properties[name] : null;

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/commands/daemon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ Map<String, dynamic> _emulatorToMap(Emulator emulator) {
'id': emulator.id,
'name': emulator.name,
'category': emulator.category?.toString(),
'platformType': emulator.platformDisplay,
'platformType': emulator.platformType?.toString(),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/commands/emulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EmulatorsCommand extends FlutterCommand {
final String description = 'List, launch and create emulators.';

@override
final List<String> aliases = <String>['emulator', 'simulators', 'simulator'];
final List<String> aliases = <String>['emulator'];

@override
Future<FlutterCommandResult> runCommand() async {
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_tools/lib/src/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ abstract class Device {
/// Check if the device is supported by Flutter.
bool isSupported();

// String meant to be displayed to the user indicating if the device is
// supported by Flutter, and, if not, why.
String supportMessage() => isSupported() ? 'Supported' : 'Unsupported';

/// The device's platform.
Future<TargetPlatform> get targetPlatform;

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/lib/src/emulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ abstract class Emulator {
String get name;
String get manufacturer;
Category get category;
String get platformDisplay;
PlatformType get platformType;

@override
int get hashCode => id.hashCode;
Expand Down Expand Up @@ -283,7 +283,7 @@ abstract class Emulator {
emulator.id ?? '',
emulator.name ?? '',
emulator.manufacturer ?? '',
emulator.platformDisplay ?? '',
emulator.platformType?.toString() ?? '',
],
];

Expand Down
60 changes: 37 additions & 23 deletions packages/flutter_tools/lib/src/ios/ios_emulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,17 @@ class IOSEmulators extends EmulatorDiscovery {
bool get canListAnything => globals.iosWorkflow.canListEmulators;

@override
Future<List<Emulator>> get emulators async {
final List<IOSSimulator> simulators = await globals.iosSimulatorUtils.getAvailableDevices();
return simulators.map<Emulator>((IOSSimulator device) {
return IOSEmulator(device);
}).toList();
}
Future<List<Emulator>> get emulators async => getEmulators();

@override
bool get canLaunchAnything => canListAnything;
}

class IOSEmulator extends Emulator {
IOSEmulator(IOSSimulator simulator)
: _simulator = simulator,
super(simulator.id, true);

final IOSSimulator _simulator;
const IOSEmulator(String id) : super(id, true);

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

@override
String get manufacturer => 'Apple';
Expand All @@ -44,20 +35,43 @@ class IOSEmulator extends Emulator {
Category get category => Category.mobile;

@override
String get platformDisplay =>
// com.apple.CoreSimulator.SimRuntime.iOS-10-3 => iOS-10-3
_simulator.simulatorCategory?.split('.')?.last ?? 'ios';
PlatformType get platformType => PlatformType.ios;

@override
Future<void> launch() async {
final RunResult launchResult = await globals.processUtils.run(<String>[
'open',
'-a',
globals.xcode.getSimulatorPath(),
]);
if (launchResult.exitCode != 0) {
globals.printError('$launchResult');
Future<bool> launchSimulator(List<String> additionalArgs) async {
final List<String> args = <String>[
'open',
...additionalArgs,
'-a',
globals.xcode.getSimulatorPath(),
];

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;
}
return _simulator.boot();

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

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

return <IOSEmulator>[const IOSEmulator(iosSimulatorId)];
}
83 changes: 13 additions & 70 deletions packages/flutter_tools/lib/src/ios/simulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class IOSSimulatorUtils {
return <IOSSimulator>[];
}

final List<SimDevice> connected = (await _simControl.getAvailableDevices())
.where((SimDevice device) => device.isBooted)
.toList();
final List<SimDevice> connected = await _simControl.getConnectedDevices();
return connected.map<IOSSimulator>((SimDevice device) {
return IOSSimulator(
device.udid,
Expand All @@ -79,26 +77,6 @@ class IOSSimulatorUtils {
);
}).toList();
}

Future<List<IOSSimulator>> getAvailableDevices() async {
if (!_xcode.isInstalledAndMeetsVersionCheck) {
return <IOSSimulator>[];
}

final List<SimDevice> available = await _simControl.getAvailableDevices();
return available
.map<IOSSimulator>((SimDevice device) {
return IOSSimulator(
device.udid,
name: device.name,
simControl: _simControl,
simulatorCategory: device.category,
xcode: _xcode,
);
})
.where((IOSSimulator simulator) => simulator.isSupported())
.toList();
}
}

/// A wrapper around the `simctl` command line tool.
Expand All @@ -111,23 +89,6 @@ class SimControl {
_xcode = xcode,
_processUtils = ProcessUtils(processManager: processManager, logger: logger);

/// Create a [SimControl] for testing.
///
/// Defaults to a buffer logger.
@visibleForTesting
factory SimControl.test({
@required ProcessManager processManager,
Logger logger,
Xcode xcode,
}) {
logger ??= BufferLogger.test();
return SimControl(
logger: logger,
xcode: xcode,
processManager: processManager,
);
}

final Logger _logger;
final ProcessUtils _processUtils;
final Xcode _xcode;
Expand Down Expand Up @@ -199,10 +160,10 @@ class SimControl {
return devices;
}

/// Returns all the available simulator devices.
Future<List<SimDevice>> getAvailableDevices() async {
/// Returns all the connected simulator devices.
Future<List<SimDevice>> getConnectedDevices() async {
final List<SimDevice> simDevices = await getDevices();
return simDevices.where((SimDevice device) => device.isAvailable).toList();
return simDevices.where((SimDevice device) => device.isBooted).toList();
}

Future<bool> isInstalled(String deviceId, String appId) {
Expand Down Expand Up @@ -273,17 +234,6 @@ class SimControl {
return result;
}

Future<RunResult> boot(String deviceId) {
return _processUtils.run(
<String>[
..._xcode.xcrunCommand(),
'simctl',
'boot',
deviceId,
],
);
}

Future<void> takeScreenshot(String deviceId, String outputPath) async {
try {
await _processUtils.run(
Expand Down Expand Up @@ -346,11 +296,7 @@ class SimDevice {
final Map<String, dynamic> data;

String get state => data['state']?.toString();

bool get isAvailable =>
data['isAvailable'] == true ||
data['availability']?.toString() == '(available)';

String get availability => data['availability']?.toString();
String get name => data['name']?.toString();
String get udid => data['udid']?.toString();

Expand Down Expand Up @@ -448,32 +394,29 @@ class IOSSimulator extends Device {
@override
bool isSupported() {
if (!globals.platform.isMacOS) {
_supportMessage = 'iOS devices require a Mac host machine.';
return false;
}

// Check if the device is part of a blocked category.
// We do not yet support WatchOS or tvOS devices.
final RegExp blocklist = RegExp(r'Apple (TV|Watch)', caseSensitive: false);
if (blocklist.hasMatch(name)) {
_supportMessage = 'Flutter does not support Apple TV or Apple Watch.';
return false;
}
return true;
}

Future<bool> boot() async {
final RunResult result = await _simControl.boot(id);
String _supportMessage;

if (result.exitCode == 0) {
return true;
}
// 149 exit code means the device is already booted. Ignore this error.
if (result.exitCode == 149) {
globals.printTrace('Simulator "$id" already booted.');
return true;
@override
String supportMessage() {
if (isSupported()) {
return 'Supported';
}

globals.logger.printError('$result');
return false;
return _supportMessage ?? 'Unknown';
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void main() {
expect(emulator.name, displayName);
expect(emulator.manufacturer, manufacturer);
expect(emulator.category, Category.mobile);
expect(emulator.platformDisplay, 'android');
expect(emulator.platformType, PlatformType.android);
});

testWithoutContext('prefers displayname for name', () {
Expand Down

0 comments on commit cdacae8

Please sign in to comment.