forked from firka/flutter
Refactor DeviceManager.findTargetDevices() and FlutterCommand.findAllTargetDevices(), and add a flag to not show prompt. (#112223)
This commit is contained in:
@@ -205,7 +205,6 @@ Future<T> runInContext<T>(
|
||||
),
|
||||
fuchsiaSdk: globals.fuchsiaSdk!,
|
||||
operatingSystemUtils: globals.os,
|
||||
terminal: globals.terminal,
|
||||
customDevicesConfig: globals.customDevicesConfig,
|
||||
),
|
||||
DevtoolsLauncher: () => DevtoolsServerLauncher(
|
||||
|
||||
@@ -9,13 +9,10 @@ import 'package:meta/meta.dart';
|
||||
|
||||
import 'application_package.dart';
|
||||
import 'artifacts.dart';
|
||||
import 'base/common.dart';
|
||||
import 'base/context.dart';
|
||||
import 'base/dds.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/logger.dart';
|
||||
import 'base/terminal.dart';
|
||||
import 'base/user_messages.dart' hide userMessages;
|
||||
import 'base/utils.dart';
|
||||
import 'build_info.dart';
|
||||
import 'devfs.dart';
|
||||
@@ -83,15 +80,9 @@ class PlatformType {
|
||||
abstract class DeviceManager {
|
||||
DeviceManager({
|
||||
required Logger logger,
|
||||
required Terminal terminal,
|
||||
required UserMessages userMessages,
|
||||
}) : _logger = logger,
|
||||
_terminal = terminal,
|
||||
_userMessages = userMessages;
|
||||
}) : _logger = logger;
|
||||
|
||||
final Logger _logger;
|
||||
final Terminal _terminal;
|
||||
final UserMessages _userMessages;
|
||||
|
||||
/// Constructing DeviceManagers is cheap; they only do expensive work if some
|
||||
/// of their methods are called.
|
||||
@@ -219,7 +210,12 @@ abstract class DeviceManager {
|
||||
];
|
||||
}
|
||||
|
||||
/// Find and return a list of devices based on the current project and environment.
|
||||
/// Find and return all target [Device]s based upon currently connected
|
||||
/// devices, the current project, and criteria entered by the user on
|
||||
/// the command line.
|
||||
///
|
||||
/// If no device can be found that meets specified criteria,
|
||||
/// then print an error message and return null.
|
||||
///
|
||||
/// Returns a list of devices specified by the user.
|
||||
///
|
||||
@@ -233,9 +229,13 @@ abstract class DeviceManager {
|
||||
/// device connected, then filter out unsupported devices and prioritize
|
||||
/// ephemeral devices.
|
||||
///
|
||||
/// * If [flutterProject] is null, then assume the project supports all
|
||||
/// device types.
|
||||
Future<List<Device>> findTargetDevices(FlutterProject? flutterProject, { Duration? timeout }) async {
|
||||
/// * If [promptUserToChooseDevice] is true, and there are more than one
|
||||
/// device after the aforementioned filters, and the user is connected to a
|
||||
/// terminal, then show a prompt asking the user to choose one.
|
||||
Future<List<Device>> findTargetDevices(
|
||||
FlutterProject? flutterProject, {
|
||||
Duration? timeout,
|
||||
}) async {
|
||||
if (timeout != null) {
|
||||
// Reset the cache with the specified timeout.
|
||||
await refreshAllConnectedDevices(timeout: timeout);
|
||||
@@ -244,97 +244,56 @@ abstract class DeviceManager {
|
||||
List<Device> devices = (await getDevices())
|
||||
.where((Device device) => device.isSupported()).toList();
|
||||
|
||||
// Always remove web and fuchsia devices from `--all`. This setting
|
||||
// currently requires devices to share a frontend_server and resident
|
||||
// runner instance. Both web and fuchsia require differently configured
|
||||
// compilers, and web requires an entirely different resident runner.
|
||||
if (hasSpecifiedAllDevices) {
|
||||
// User has specified `--device all`.
|
||||
//
|
||||
// Always remove web and fuchsia devices from `--all`. This setting
|
||||
// currently requires devices to share a frontend_server and resident
|
||||
// runner instance. Both web and fuchsia require differently configured
|
||||
// compilers, and web requires an entirely different resident runner.
|
||||
devices = <Device>[
|
||||
for (final Device device in devices)
|
||||
if (await device.targetPlatform != TargetPlatform.fuchsia_arm64 &&
|
||||
await device.targetPlatform != TargetPlatform.fuchsia_x64 &&
|
||||
await device.targetPlatform != TargetPlatform.web_javascript)
|
||||
await device.targetPlatform != TargetPlatform.web_javascript &&
|
||||
isDeviceSupportedForProject(device, flutterProject))
|
||||
device,
|
||||
];
|
||||
}
|
||||
} else if (!hasSpecifiedDeviceId) {
|
||||
// User did not specify the device.
|
||||
|
||||
// If there is no specified device, the remove all devices which are not
|
||||
// supported by the current application. For example, if there was no
|
||||
// 'android' folder then don't attempt to launch with an Android device.
|
||||
if (devices.length > 1 && !hasSpecifiedDeviceId) {
|
||||
// Remove all devices which are not supported by the current application.
|
||||
// For example, if there was no 'android' folder then don't attempt to
|
||||
// launch with an Android device.
|
||||
devices = <Device>[
|
||||
for (final Device device in devices)
|
||||
if (isDeviceSupportedForProject(device, flutterProject))
|
||||
device,
|
||||
];
|
||||
} else if (devices.length == 1 &&
|
||||
!hasSpecifiedDeviceId &&
|
||||
!isDeviceSupportedForProject(devices.single, flutterProject)) {
|
||||
// If there is only a single device but it is not supported, then return
|
||||
// early.
|
||||
return <Device>[];
|
||||
}
|
||||
|
||||
// If there are still multiple devices and the user did not specify to run
|
||||
// all, then attempt to prioritize ephemeral devices. For example, if the
|
||||
// user only typed 'flutter run' and both an Android device and desktop
|
||||
// device are available, choose the Android device.
|
||||
if (devices.length > 1 && !hasSpecifiedAllDevices) {
|
||||
// Note: ephemeral is nullable for device types where this is not well
|
||||
// defined.
|
||||
if (devices.any((Device device) => device.ephemeral == true)) {
|
||||
// if there is only one ephemeral device, get it
|
||||
final List<Device> ephemeralDevices = devices
|
||||
.where((Device device) => device.ephemeral == true)
|
||||
.toList();
|
||||
if (devices.length > 1) {
|
||||
// If there are still multiple devices and the user did not specify to run
|
||||
// all, then attempt to prioritize ephemeral devices. For example, if the
|
||||
// user only typed 'flutter run' and both an Android device and desktop
|
||||
// device are available, choose the Android device.
|
||||
|
||||
if (ephemeralDevices.length == 1) {
|
||||
devices = ephemeralDevices;
|
||||
}
|
||||
}
|
||||
// If it was not able to prioritize a device. For example, if the user
|
||||
// has two active Android devices running, then we request the user to
|
||||
// choose one. If the user has two nonEphemeral devices running, we also
|
||||
// request input to choose one.
|
||||
if (devices.length > 1 && _terminal.stdinHasTerminal) {
|
||||
_logger.printStatus(_userMessages.flutterMultipleDevicesFound);
|
||||
await Device.printDevices(devices, _logger);
|
||||
final Device chosenDevice = await _chooseOneOfAvailableDevices(devices);
|
||||
specifiedDeviceId = chosenDevice.id;
|
||||
devices = <Device>[chosenDevice];
|
||||
// Note: ephemeral is nullable for device types where this is not well
|
||||
// defined.
|
||||
final List<Device> ephemeralDevices = <Device>[
|
||||
for (final Device device in devices)
|
||||
if (device.ephemeral == true)
|
||||
device,
|
||||
];
|
||||
|
||||
if (ephemeralDevices.length == 1) {
|
||||
devices = ephemeralDevices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
Future<Device> _chooseOneOfAvailableDevices(List<Device> devices) async {
|
||||
_displayDeviceOptions(devices);
|
||||
final String userInput = await _readUserInput(devices.length);
|
||||
if (userInput.toLowerCase() == 'q') {
|
||||
throwToolExit('');
|
||||
}
|
||||
return devices[int.parse(userInput) - 1];
|
||||
}
|
||||
|
||||
void _displayDeviceOptions(List<Device> devices) {
|
||||
int count = 1;
|
||||
for (final Device device in devices) {
|
||||
_logger.printStatus(_userMessages.flutterChooseDevice(count, device.name, device.id));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _readUserInput(int deviceCount) async {
|
||||
_terminal.usesTerminalUi = true;
|
||||
final String result = await _terminal.promptForCharInput(
|
||||
<String>[ for (int i = 0; i < deviceCount; i++) '${i + 1}', 'q', 'Q'],
|
||||
displayAcceptedCharacters: false,
|
||||
logger: _logger,
|
||||
prompt: _userMessages.flutterChooseOne,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Returns whether the device is supported for the project.
|
||||
///
|
||||
/// This exists to allow the check to be overridden for google3 clients. If
|
||||
|
||||
@@ -11,6 +11,7 @@ import 'artifacts.dart';
|
||||
import 'base/file_system.dart';
|
||||
import 'base/os.dart';
|
||||
import 'base/platform.dart';
|
||||
import 'base/user_messages.dart';
|
||||
import 'custom_devices/custom_device.dart';
|
||||
import 'custom_devices/custom_devices_config.dart';
|
||||
import 'device.dart';
|
||||
@@ -51,10 +52,9 @@ class FlutterDeviceManager extends DeviceManager {
|
||||
required Artifacts artifacts,
|
||||
required MacOSWorkflow macOSWorkflow,
|
||||
required FuchsiaSdk fuchsiaSdk,
|
||||
required super.userMessages,
|
||||
required UserMessages userMessages,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required WindowsWorkflow windowsWorkflow,
|
||||
required super.terminal,
|
||||
required CustomDevicesConfig customDevicesConfig,
|
||||
}) : deviceDiscoverers = <DeviceDiscovery>[
|
||||
AndroidDevices(
|
||||
|
||||
@@ -1413,52 +1413,105 @@ abstract class FlutterCommand extends Command<void> {
|
||||
timeout: deviceDiscoveryTimeout,
|
||||
);
|
||||
|
||||
if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) {
|
||||
globals.printStatus(userMessages.flutterNoMatchingDevice(deviceManager.specifiedDeviceId!));
|
||||
final List<Device> allDevices = await deviceManager.getAllConnectedDevices();
|
||||
if (allDevices.isNotEmpty) {
|
||||
globals.printStatus('');
|
||||
globals.printStatus('The following devices were found:');
|
||||
await Device.printDevices(allDevices, globals.logger);
|
||||
}
|
||||
return null;
|
||||
} else if (devices.isEmpty) {
|
||||
if (deviceManager.hasSpecifiedAllDevices) {
|
||||
globals.printStatus(userMessages.flutterNoDevicesFound);
|
||||
} else {
|
||||
globals.printStatus(userMessages.flutterNoSupportedDevices);
|
||||
}
|
||||
final List<Device> unsupportedDevices = await deviceManager.getDevices();
|
||||
if (unsupportedDevices.isNotEmpty) {
|
||||
final StringBuffer result = StringBuffer();
|
||||
result.writeln(userMessages.flutterFoundButUnsupportedDevices);
|
||||
result.writeAll(
|
||||
(await Device.descriptions(unsupportedDevices))
|
||||
.map((String desc) => desc)
|
||||
.toList(),
|
||||
'\n',
|
||||
);
|
||||
result.writeln();
|
||||
result.writeln(userMessages.flutterMissPlatformProjects(
|
||||
Device.devicesPlatformTypes(unsupportedDevices),
|
||||
));
|
||||
globals.printStatus(result.toString());
|
||||
}
|
||||
return null;
|
||||
} else if (devices.length > 1 && !deviceManager.hasSpecifiedAllDevices) {
|
||||
if (devices.isEmpty) {
|
||||
if (deviceManager.hasSpecifiedDeviceId) {
|
||||
globals.printStatus(userMessages.flutterFoundSpecifiedDevices(devices.length, deviceManager.specifiedDeviceId!));
|
||||
globals.logger.printStatus(userMessages.flutterNoMatchingDevice(deviceManager.specifiedDeviceId!));
|
||||
final List<Device> allDevices = await deviceManager.getAllConnectedDevices();
|
||||
if (allDevices.isNotEmpty) {
|
||||
globals.logger.printStatus('');
|
||||
globals.logger.printStatus('The following devices were found:');
|
||||
await Device.printDevices(allDevices, globals.logger);
|
||||
}
|
||||
return null;
|
||||
} else if (deviceManager.hasSpecifiedAllDevices) {
|
||||
globals.logger.printStatus(userMessages.flutterNoDevicesFound);
|
||||
await _printUnsupportedDevice(deviceManager);
|
||||
return null;
|
||||
} else {
|
||||
globals.printStatus(userMessages.flutterSpecifyDeviceWithAllOption);
|
||||
devices = await deviceManager.getAllConnectedDevices();
|
||||
globals.logger.printStatus(userMessages.flutterNoSupportedDevices);
|
||||
await _printUnsupportedDevice(deviceManager);
|
||||
return null;
|
||||
}
|
||||
} else if (devices.length > 1) {
|
||||
if (deviceManager.hasSpecifiedDeviceId) {
|
||||
globals.logger.printStatus(userMessages.flutterFoundSpecifiedDevices(devices.length, deviceManager.specifiedDeviceId!));
|
||||
return null;
|
||||
} else if (!deviceManager.hasSpecifiedAllDevices) {
|
||||
if (globals.terminal.stdinHasTerminal) {
|
||||
// If DeviceManager was not able to prioritize a device. For example, if the user
|
||||
// has two active Android devices running, then we request the user to
|
||||
// choose one. If the user has two nonEphemeral devices running, we also
|
||||
// request input to choose one.
|
||||
globals.logger.printStatus(userMessages.flutterMultipleDevicesFound);
|
||||
await Device.printDevices(devices, globals.logger);
|
||||
final Device chosenDevice = await _chooseOneOfAvailableDevices(devices);
|
||||
|
||||
// Update the [DeviceManager.specifiedDeviceId] so that we will not be prompted again.
|
||||
deviceManager.specifiedDeviceId = chosenDevice.id;
|
||||
|
||||
devices = <Device>[chosenDevice];
|
||||
} else {
|
||||
// Show an error message asking the user to specify `-d all` if they
|
||||
// want to run on multiple devices.
|
||||
final List<Device> allDevices = await deviceManager.getAllConnectedDevices();
|
||||
globals.logger.printStatus(userMessages.flutterSpecifyDeviceWithAllOption);
|
||||
globals.logger.printStatus('');
|
||||
await Device.printDevices(allDevices, globals.logger);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
globals.printStatus('');
|
||||
await Device.printDevices(devices, globals.logger);
|
||||
return null;
|
||||
}
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
Future<void> _printUnsupportedDevice(DeviceManager deviceManager) async {
|
||||
final List<Device> unsupportedDevices = await deviceManager.getDevices();
|
||||
if (unsupportedDevices.isNotEmpty) {
|
||||
final StringBuffer result = StringBuffer();
|
||||
result.writeln(userMessages.flutterFoundButUnsupportedDevices);
|
||||
result.writeAll(
|
||||
(await Device.descriptions(unsupportedDevices))
|
||||
.map((String desc) => desc)
|
||||
.toList(),
|
||||
'\n',
|
||||
);
|
||||
result.writeln();
|
||||
result.writeln(userMessages.flutterMissPlatformProjects(
|
||||
Device.devicesPlatformTypes(unsupportedDevices),
|
||||
));
|
||||
globals.logger.printStatus(result.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Future<Device> _chooseOneOfAvailableDevices(List<Device> devices) async {
|
||||
_displayDeviceOptions(devices);
|
||||
final String userInput = await _readUserInput(devices.length);
|
||||
if (userInput.toLowerCase() == 'q') {
|
||||
throwToolExit('');
|
||||
}
|
||||
return devices[int.parse(userInput) - 1];
|
||||
}
|
||||
|
||||
void _displayDeviceOptions(List<Device> devices) {
|
||||
int count = 1;
|
||||
for (final Device device in devices) {
|
||||
globals.logger.printStatus(userMessages.flutterChooseDevice(count, device.name, device.id));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _readUserInput(int deviceCount) async {
|
||||
globals.terminal.usesTerminalUi = true;
|
||||
final String result = await globals.terminal.promptForCharInput(
|
||||
<String>[ for (int i = 0; i < deviceCount; i++) '${i + 1}', 'q', 'Q'],
|
||||
displayAcceptedCharacters: false,
|
||||
logger: globals.logger,
|
||||
prompt: userMessages.flutterChooseOne,
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Find and return the target [Device] based upon currently connected
|
||||
/// devices and criteria entered by the user on the command line.
|
||||
/// If a device cannot be found that meets specified criteria,
|
||||
|
||||
Reference in New Issue
Block a user