Add support for launching emulators

This commit is contained in:
Danny Tuppeny 2018-04-18 14:50:16 +01:00 committed by Danny Tuppeny
parent 4d7c3c775f
commit 4c67885b8e
4 changed files with 65 additions and 18 deletions

View file

@ -9,8 +9,10 @@ import 'package:meta/meta.dart';
import '../android/android_sdk.dart';
import '../android/android_workflow.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../emulator.dart';
import '../globals.dart';
import 'android_sdk.dart';
class AndroidEmulators extends EmulatorDiscovery {
@ -39,11 +41,19 @@ class AndroidEmulator extends Emulator {
@override
String get label => _properties['avd.ini.displayname'];
// @override
// Future<bool> launch() async {
// // TODO: ...
// return null;Í
// }
@override
Future<bool> launch() async {
final Status status = logger.startProgress('Launching $id...');
final RunResult launchResult = await runAsync(<String>[getEmulatorPath(), '-avd', id]);
status.stop();
if (launchResult.exitCode != 0) {
printError('Error: emulator exited with exit code ${launchResult.exitCode}');
printError('$launchResult');
return false;
}
return true;
}
}
/// Return the list of available emulator AVDs.

View file

@ -12,6 +12,11 @@ import '../globals.dart';
import '../runner/flutter_command.dart';
class EmulatorsCommand extends FlutterCommand {
EmulatorsCommand() {
argParser.addOption('start',
help: 'The full or partial ID of the emulator to start.');
}
@override
final String name = 'emulators';
@ -20,23 +25,46 @@ class EmulatorsCommand extends FlutterCommand {
@override
Future<Null> runCommand() async {
if (!doctor.canListAnything) {
if (doctor.workflows.every((Workflow w) => !w.canListEmulators)) {
throwToolExit(
"Unable to locate emulators; please run 'flutter doctor' for "
'information about installing additional components.',
exitCode: 1);
"Unable to query emulators; please run 'flutter doctor' for "
'information about installing additional components.',
exitCode: 1);
}
final List<Emulator> emulators = await emulatorManager.getAllAvailableEmulators().toList();
if (argResults.wasParsed('start')) {
await _startEmulator(argResults['start']);
} else {
await _listEmulators();
}
}
Future<Null> _startEmulator(String id) async {
final List<Emulator> emulators =
await emulatorManager.getEmulatorsById(id).toList();
if (emulators.isEmpty) {
printStatus(
'No emulators available.\n\n'
// TODO: Change these when we support creation
// 'You may need to create images using "flutter emulators --create"\n'
'You may need to create one using Android Studio\n'
'or visit https://flutter.io/setup/ for troubleshooting tips.');
final List<String> diagnostics = await emulatorManager.getEmulatorDiagnostics();
printStatus("No emulator found that matches the ID '$id'.");
} else if (emulators.length > 1) {
printStatus("More than one emulator matches the ID '$id':\n");
Emulator.printEmulators(emulators);
} else {
emulators.first.launch();
}
}
Future<Null> _listEmulators() async {
final List<Emulator> emulators =
await emulatorManager.getAllAvailableEmulators().toList();
if (emulators.isEmpty) {
printStatus('No emulators available.\n\n'
// TODO: Change these when we support creation
// 'You may need to create images using "flutter emulators --create"\n'
'You may need to create one using Android Studio\n'
'or visit https://flutter.io/setup/ for troubleshooting tips.');
final List<String> diagnostics =
await emulatorManager.getEmulatorDiagnostics();
if (diagnostics.isNotEmpty) {
printStatus('');
for (String diagnostic in diagnostics) {
@ -44,7 +72,8 @@ class EmulatorsCommand extends FlutterCommand {
}
}
} else {
printStatus('${emulators.length} available ${pluralize('emulators', emulators.length)}:\n');
printStatus(
'${emulators.length} available ${pluralize('emulators', emulators.length)}:\n');
await Emulator.printEmulators(emulators);
}
}

View file

@ -135,6 +135,8 @@ abstract class Emulator {
return id == other.id;
}
void launch();
@override
String toString() => name;

View file

@ -61,4 +61,10 @@ class _MockEmulator extends Emulator {
@override
final String label;
@override
void launch() {
throw new UnimplementedError('Not implemented in Mock');
}
}