mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

* Add --create option to flutter emulators * Tweaks to error message * Simplify emulator search logic * Make name optional * Add a note about this option being used with --create * Tweaks to help information * Switch to processManager for easier testing * Don't crash on missing files or missing properties in Android Emulator * Move name suffixing into emulator manager This allows it to be tested in the EmulatorManager tests and also used by daemon later if desired. * Pass the context's android SDK through so it can be mocked by tests * Misc fixes * Add tests around emulator creation Process calls are mocked to avoid needing a real SDK (and to be fast). Full integration tests may be useful, but may require ensuring all build environments etc. are set up correctly. * Simplify avdManagerPath Previous changes were to emulatorPath! * Fix lint errors * Fix incorrect file exgtension for Windows * Fix an issue where no system images would crash reduce throws on an empty collection. * Fix "null" appearing in error messages The name we attempted to use will now always be returned, even in the case of failure. * Add additional info to missing-system-image failure message On Windows after installing Andriod Studio I didn't have any of these and got this message. Installing with sdkmanager fixed the issue. * Fix thrown errors runResult had a toString() but we moved to ProcessResult when switching to ProcessManager to this ended up throwing "Instance of ProcessResult". * Fix package import * Fix more package imports * Move mock implementation into Mock class There seemed to be issues using Lists in args with Mockito that I couldn't figure out (docs say to use typed() but I couldn't make this compile with these lists still).. * Rename method that's ambigious now we have create * Handle where there's no avd path * Add another toList() :( * Remove comment that was rewritten * Fix forbidden import * Make optional arg more obviously optional * Reformat doc * Note that we create a pixel device in help text * Make this a named arg
137 lines
4.3 KiB
Dart
137 lines
4.3 KiB
Dart
// Copyright 2018 The Chromium Authors. All rights reserved.
|
|
// 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/common.dart';
|
|
import '../base/platform.dart';
|
|
import '../base/utils.dart';
|
|
import '../doctor.dart';
|
|
import '../emulator.dart';
|
|
import '../globals.dart';
|
|
import '../runner/flutter_command.dart';
|
|
|
|
class EmulatorsCommand extends FlutterCommand {
|
|
EmulatorsCommand() {
|
|
argParser.addOption('launch',
|
|
help: 'The full or partial ID of the emulator to launch.');
|
|
argParser.addFlag('create',
|
|
help: 'Creates a new Android emulator based on a Pixel device.',
|
|
negatable: false);
|
|
argParser.addOption('name',
|
|
help: 'Used with flag --create. Specifies a name for the emulator being created.');
|
|
}
|
|
|
|
@override
|
|
final String name = 'emulators';
|
|
|
|
@override
|
|
final String description = 'List, launch and create emulators.';
|
|
|
|
@override
|
|
final List<String> aliases = <String>['emulator'];
|
|
|
|
@override
|
|
Future<Null> runCommand() async {
|
|
if (doctor.workflows.every((Workflow w) => !w.canListEmulators)) {
|
|
throwToolExit(
|
|
'Unable to find any emulator sources. Please ensure you have some\n'
|
|
'Android AVD images ' +
|
|
(platform.isMacOS ? 'or an iOS Simulator ' : '') +
|
|
'available.',
|
|
exitCode: 1);
|
|
}
|
|
|
|
if (argResults.wasParsed('launch')) {
|
|
await _launchEmulator(argResults['launch']);
|
|
} else if (argResults.wasParsed('create')) {
|
|
await _createEmulator(name: argResults['name']);
|
|
} else {
|
|
final String searchText =
|
|
argResults.rest != null && argResults.rest.isNotEmpty
|
|
? argResults.rest.first
|
|
: null;
|
|
await _listEmulators(searchText);
|
|
}
|
|
}
|
|
|
|
Future<void> _launchEmulator(String id) async {
|
|
final List<Emulator> emulators =
|
|
await emulatorManager.getEmulatorsMatching(id);
|
|
|
|
if (emulators.isEmpty) {
|
|
printStatus("No emulator found that matches '$id'.");
|
|
} else if (emulators.length > 1) {
|
|
_printEmulatorList(
|
|
emulators,
|
|
"More than one emulator matches '$id':",
|
|
);
|
|
} else {
|
|
try {
|
|
await emulators.first.launch();
|
|
}
|
|
catch (e) {
|
|
printError(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<Null> _createEmulator({String name}) async {
|
|
final CreateEmulatorResult createResult =
|
|
await emulatorManager.createEmulator(name: name);
|
|
|
|
if (createResult.success) {
|
|
printStatus("Emulator '${createResult.emulatorName}' created successfully.");
|
|
} else {
|
|
printStatus("Failed to create emulator '${createResult.emulatorName}'.\n");
|
|
printStatus(createResult.error.trim());
|
|
_printAdditionalInfo();
|
|
}
|
|
}
|
|
|
|
Future<void> _listEmulators(String searchText) async {
|
|
final List<Emulator> emulators = searchText == null
|
|
? await emulatorManager.getAllAvailableEmulators()
|
|
: await emulatorManager.getEmulatorsMatching(searchText);
|
|
|
|
if (emulators.isEmpty) {
|
|
printStatus('No emulators available.');
|
|
_printAdditionalInfo(showCreateInstruction: true);
|
|
} else {
|
|
_printEmulatorList(
|
|
emulators,
|
|
'${emulators.length} available ${pluralize('emulator', emulators.length)}:',
|
|
);
|
|
}
|
|
}
|
|
|
|
void _printEmulatorList(List<Emulator> emulators, String message) {
|
|
printStatus('$message\n');
|
|
Emulator.printEmulators(emulators);
|
|
_printAdditionalInfo(showCreateInstruction: true, showRunInstruction: true);
|
|
}
|
|
|
|
void _printAdditionalInfo({ bool showRunInstruction = false,
|
|
bool showCreateInstruction = false }) {
|
|
printStatus('');
|
|
if (showRunInstruction) {
|
|
printStatus(
|
|
"To run an emulator, run 'flutter emulators --launch <emulator id>'.");
|
|
}
|
|
if (showCreateInstruction) {
|
|
printStatus(
|
|
"To create a new emulator, run 'flutter emulators --create [--name xyz]'.");
|
|
}
|
|
|
|
if (showRunInstruction || showCreateInstruction) {
|
|
printStatus('');
|
|
}
|
|
// TODO(dantup): Update this link to flutter.io if/when we have a better page.
|
|
// That page can then link out to these places if required.
|
|
printStatus('You can find more information on managing emulators at the links below:\n'
|
|
' https://developer.android.com/studio/run/managing-avds\n'
|
|
' https://developer.android.com/studio/command-line/avdmanager');
|
|
}
|
|
}
|