Skip to content

Commit 9474d57

Browse files
committed
[flutter_tools] Simplify emulator creation routing and daemon wiring
Keep the `flutter daemon` wire protocol unchanged at `0.6.1` (`EmulatorDomain.create` continues creating Android emulators while `emulator.getEmulators` and `emulator.launch` include tool extension emulators via the injected `EmulatorManager`), default `EmulatorManager.createEmulator` `platform` to `androidPlatform`, inline interactive platform prompting inside `EmulatorsCommand`, mark `TargetEmulator` as `final`, fail fast via `throwToolExit` on unknown `--platform` values, and consolidate extension error formatting. Part of flutter#190755 Part of flutter#190750
1 parent 4ef0f49 commit 9474d57

13 files changed

Lines changed: 545 additions & 377 deletions

File tree

‎packages/flutter_tools/doc/daemon.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,13 @@ Return a list of all available emulators. The `params` field will be a List; eac
289289
The `launch()` command allows launching an emulator/simulator by its `id`.
290290

291291
- `emulatorId`: the id of an emulator as returned by `getEmulators`.
292-
- `coldBoot`: an optional boolean flag which indicates if the emulator should be cold booted. Supported for Android and tool extension emulators; silently ignored for other emulator types.
292+
- `coldBoot`: an optional boolean flag which indicates if the emulator should be cold booted. Only supported for android emulators, silently ignored if emulator type is not android.
293293

294294
#### emulator.create
295295

296-
The `create()` command creates a new emulator with an optional `name` and `platform`.
296+
The `create()` command creates a new Android emulator with an optional `name`.
297297

298298
- `name`: an optional name for this emulator
299-
- `platform`: an optional target platform identifier (for example, `'android'` or a tool extension platform identifier). When omitted, defaults to `'android'` if Android emulator creation is available, or to the single available tool extension emulator platform otherwise.
300299

301300
The returned `params` will contain:
302301

@@ -353,7 +352,6 @@ See the [source](https://github.com/flutter/flutter/blob/main/packages/flutter_t
353352

354353
## Changelog
355354

356-
- 0.6.2: Added `platform` option to `emulator.create` command.
357355
- 0.6.1: Added `coldBoot` option to `emulator.launch` command.
358356
- 0.6.0: Added `debounce` option to `app.restart` command.
359357
- 0.5.3: Added `emulatorId` field to device.

‎packages/flutter_tools/lib/src/base/terminal.dart‎

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -416,59 +416,6 @@ class AnsiTerminal implements Terminal {
416416
}
417417
}
418418

419-
const _defaultNumberedChoicePrompt = 'Please choose one (or "q" to quit)';
420-
421-
/// Prompts the user to pick a 1-based option from `1..optionCount` or `'q'` to
422-
/// quit, using single-character input when [optionCount] is less than `10` and
423-
/// line input otherwise.
424-
Future<String> promptForNumberedChoice({
425-
required Logger logger,
426-
required int optionCount,
427-
required Terminal terminal,
428-
void Function()? onInvalidInput,
429-
String prompt = _defaultNumberedChoicePrompt,
430-
}) async {
431-
if (optionCount >= 10) {
432-
return readNumberedChoiceLine(
433-
logger: logger,
434-
optionCount: optionCount,
435-
terminal: terminal,
436-
onInvalidInput: onInvalidInput,
437-
prompt: prompt,
438-
);
439-
}
440-
terminal.usesTerminalUi = true;
441-
return terminal.promptForCharInput(
442-
<String>[for (int i = 0; i < optionCount; i++) '${i + 1}', 'q', 'Q'],
443-
displayAcceptedCharacters: false,
444-
logger: logger,
445-
prompt: prompt,
446-
);
447-
}
448-
449-
/// Reads a line-buffered 1-based choice from `1..optionCount` or `'q'` to quit.
450-
Future<String> readNumberedChoiceLine({
451-
required Logger logger,
452-
required int optionCount,
453-
required Terminal terminal,
454-
void Function()? onInvalidInput,
455-
String prompt = _defaultNumberedChoicePrompt,
456-
}) async {
457-
while (true) {
458-
logger.printStatus(prompt, emphasis: true, newline: false);
459-
logger.printStatus(': ', emphasis: true, newline: false);
460-
final String choice = (await terminal.readLine()).trim();
461-
if (choice.toLowerCase() == 'q') {
462-
return choice;
463-
}
464-
final int? number = int.tryParse(choice);
465-
if (number != null && number >= 1 && number <= optionCount) {
466-
return choice;
467-
}
468-
onInvalidInput?.call();
469-
}
470-
}
471-
472419
class _TestTerminal implements Terminal {
473420
_TestTerminal({this.supportsColor = false, this.supportsEmoji = false});
474421

‎packages/flutter_tools/lib/src/commands/daemon.dart‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import '../runner/flutter_command.dart';
4545
import '../vmservice.dart';
4646
import '../web/web_runner.dart';
4747

48-
const protocolVersion = '0.6.2';
48+
const protocolVersion = '0.6.1';
4949

5050
/// A server process command. This command will start up a long-lived server.
5151
/// It reads JSON-RPC based commands from stdin, executes them, and returns
@@ -1890,8 +1890,7 @@ class EmulatorDomain extends Domain {
18901890

18911891
Future<Map<String, Object?>> create(Map<String, Object?> args) async {
18921892
final String? name = _getStringArg(args, 'name');
1893-
final String? platform = _getStringArg(args, 'platform');
1894-
final CreateEmulatorResult res = await emulators.createEmulator(name: name, platform: platform);
1893+
final CreateEmulatorResult res = await emulators.createEmulator(name: name);
18951894
return <String, Object?>{
18961895
'success': res.success,
18971896
'emulatorName': res.emulatorName,

‎packages/flutter_tools/lib/src/commands/emulators.dart‎

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,45 @@ class EmulatorsCommand extends FlutterCommand {
114114

115115
Future<void> _createEmulator({String? name, String? platform}) async {
116116
final ToolContext(:Logger logger, :AnsiTerminal terminal) = toolContext;
117-
var resolvedPlatform = platform;
118-
List<String>? availablePlatforms;
119-
if (resolvedPlatform != null) {
120-
if (resolvedPlatform.isEmpty) {
117+
final String resolvedPlatform;
118+
if (platform != null) {
119+
if (platform.isEmpty) {
121120
throwToolExit('The "--$_platformOption" option must not be empty.');
122121
}
123-
} else {
124-
availablePlatforms = await _emulatorManager.getAvailableCreatePlatforms();
125-
if (availablePlatforms.length > 1) {
126-
if (!terminal.stdinHasTerminal) {
122+
if (platform != EmulatorManager.androidPlatform) {
123+
final List<String> availablePlatforms = await _emulatorManager
124+
.getAvailableCreatePlatforms();
125+
if (!availablePlatforms.contains(platform)) {
127126
throwToolExit(
128-
'Multiple emulator creation platforms are available '
129-
'(${availablePlatforms.join(', ')}). '
130-
'Specify one using "--$_platformOption <platform>".',
127+
availablePlatforms.isEmpty
128+
? 'Unknown emulator creation platform "$platform". '
129+
'No emulator creation platforms are available.'
130+
: 'Unknown emulator creation platform "$platform". '
131+
'Available platforms: ${availablePlatforms.join(', ')}.',
131132
);
132133
}
133-
resolvedPlatform = await _promptForPlatform(
134-
availablePlatforms,
135-
logger: logger,
136-
terminal: terminal,
137-
);
134+
}
135+
resolvedPlatform = platform;
136+
} else {
137+
final List<String> availablePlatforms = await _emulatorManager.getAvailableCreatePlatforms();
138+
switch (availablePlatforms) {
139+
case []:
140+
resolvedPlatform = EmulatorManager.androidPlatform;
141+
case [final String singlePlatform]:
142+
resolvedPlatform = singlePlatform;
143+
default:
144+
if (!terminal.stdinHasTerminal) {
145+
throwToolExit(
146+
'Multiple emulator creation platforms are available '
147+
'(${availablePlatforms.join(', ')}). '
148+
'Specify one using "--$_platformOption <platform>".',
149+
);
150+
}
151+
resolvedPlatform = await _promptForPlatform(
152+
availablePlatforms,
153+
logger: logger,
154+
terminal: terminal,
155+
);
138156
}
139157
}
140158

@@ -155,15 +173,7 @@ class EmulatorsCommand extends FlutterCommand {
155173
if (error != null && error.trim().isNotEmpty) {
156174
logger.printStatus(error.trim());
157175
}
158-
final bool isAndroidOrUnconfigured = switch (resolvedPlatform) {
159-
EmulatorManager.androidPlatform => true,
160-
null =>
161-
availablePlatforms == null ||
162-
availablePlatforms.isEmpty ||
163-
availablePlatforms.single == EmulatorManager.androidPlatform,
164-
_ => false,
165-
};
166-
if (isAndroidOrUnconfigured) {
176+
if (resolvedPlatform == EmulatorManager.androidPlatform) {
167177
_printAdditionalInfo();
168178
}
169179
}
@@ -174,15 +184,28 @@ class EmulatorsCommand extends FlutterCommand {
174184
required Logger logger,
175185
required AnsiTerminal terminal,
176186
}) async {
187+
const prompt = 'Please choose one (or "q" to quit)';
177188
logger.printStatus('Multiple emulator creation platforms are available:');
178189
for (final (int index, String platform) in platforms.indexed) {
179190
logger.printStatus('[${index + 1}]: $platform');
180191
}
181-
final String choice = await promptForNumberedChoice(
182-
logger: logger,
183-
optionCount: platforms.length,
184-
terminal: terminal,
185-
);
192+
final String choice;
193+
if (platforms.length < 10) {
194+
terminal.usesTerminalUi = true;
195+
choice = await terminal.promptForCharInput(
196+
<String>[for (var i = 0; i < platforms.length; i++) '${i + 1}', 'q', 'Q'],
197+
displayAcceptedCharacters: false,
198+
logger: logger,
199+
prompt: prompt,
200+
);
201+
} else {
202+
choice = await _readPlatformChoiceLine(
203+
logger: logger,
204+
optionCount: platforms.length,
205+
prompt: prompt,
206+
terminal: terminal,
207+
);
208+
}
186209
if (choice.toLowerCase() == 'q') {
187210
throwToolExit('');
188211
}
@@ -191,6 +214,22 @@ class EmulatorsCommand extends FlutterCommand {
191214
return selected;
192215
}
193216

217+
Future<String> _readPlatformChoiceLine({
218+
required Logger logger,
219+
required int optionCount,
220+
required String prompt,
221+
required AnsiTerminal terminal,
222+
}) async {
223+
while (true) {
224+
logger.printStatus('$prompt: ', emphasis: true, newline: false);
225+
final String line = (await terminal.readLine()).trim();
226+
final int? number = int.tryParse(line);
227+
if (line.toLowerCase() == 'q' || (number != null && number >= 1 && number <= optionCount)) {
228+
return line;
229+
}
230+
}
231+
}
232+
194233
Future<void> _listEmulators(String? searchText) async {
195234
final Logger logger = toolContext.logger;
196235
final List<Emulator> emulators = searchText == null

‎packages/flutter_tools/lib/src/emulator.dart‎

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -140,62 +140,34 @@ class EmulatorManager {
140140
}
141141

142142
/// Creates a new emulator, optionally named [name], targeting [platform]
143-
/// (`'android'` or an extension platform identifier).
144-
///
145-
/// When [platform] is omitted, defaults to Android when Android AVD creation
146-
/// is available, delegates to the single extension platform when exactly one
147-
/// extension supports emulator creation, or returns a failed
148-
/// [CreateEmulatorResult] when multiple extension platforms are available.
149-
Future<CreateEmulatorResult> createEmulator({String? name, String? platform}) async {
150-
if (platform == androidPlatform || (platform == null && _canCreateAndroidEmulator)) {
143+
/// (`'android'` by default, or an extension platform identifier).
144+
Future<CreateEmulatorResult> createEmulator({
145+
String? name,
146+
String platform = androidPlatform,
147+
}) async {
148+
if (platform == androidPlatform) {
151149
return _createAndroidEmulator(name: name);
152150
}
153151

154152
final ExtensionEmulators? extensionEmulators = _extensionEmulators;
155-
if (platform != null) {
156-
if (extensionEmulators != null) {
157-
final CreateEmulatorResult? result = await extensionEmulators.createEmulator(
158-
platform: platform,
159-
name: name,
160-
);
161-
if (result != null) {
162-
return result;
163-
}
164-
}
165-
final List<String> availablePlatforms = await getAvailableCreatePlatforms();
166-
return CreateEmulatorResult(
167-
name ?? '',
168-
success: false,
169-
error: availablePlatforms.isEmpty
170-
? 'Unknown emulator creation platform "$platform". '
171-
'No emulator creation platforms are available.'
172-
: 'Unknown emulator creation platform "$platform". '
173-
'Available platforms: ${availablePlatforms.join(', ')}.',
174-
);
175-
}
176-
177-
if (extensionEmulators == null) {
178-
return _createAndroidEmulator(name: name);
179-
}
180-
final List<String> extensionPlatforms = await extensionEmulators.getCreatePlatforms();
181-
if (extensionPlatforms.isEmpty) {
182-
return _createAndroidEmulator(name: name);
183-
}
184-
if (extensionPlatforms case [final String singlePlatform]) {
153+
if (extensionEmulators != null) {
185154
final CreateEmulatorResult? result = await extensionEmulators.createEmulator(
186-
platform: singlePlatform,
155+
platform: platform,
187156
name: name,
188157
);
189158
if (result != null) {
190159
return result;
191160
}
192161
}
162+
final List<String> availablePlatforms = await getAvailableCreatePlatforms();
193163
return CreateEmulatorResult(
194164
name ?? '',
195165
success: false,
196-
error:
197-
'Multiple emulator creation platforms are available '
198-
'(${extensionPlatforms.join(', ')}). Specify a "platform" parameter.',
166+
error: availablePlatforms.isEmpty
167+
? 'Unknown emulator creation platform "$platform". '
168+
'No emulator creation platforms are available.'
169+
: 'Unknown emulator creation platform "$platform". '
170+
'Available platforms: ${availablePlatforms.join(', ')}.',
199171
);
200172
}
201173

0 commit comments

Comments
 (0)