Add timeout flag to devices command, pipe through discovery (#51678)

This commit is contained in:
Jenn Magder
2020-03-16 14:15:00 -07:00
committed by GitHub
parent 0a09b78297
commit 2f216ceee5
23 changed files with 251 additions and 52 deletions

View File

@@ -62,7 +62,7 @@ class AndroidDevices extends PollingDeviceDiscovery {
bool get canListAnything => androidWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async => getAdbDevices();
Future<List<Device>> pollingGetDevices({ Duration timeout }) async => getAdbDevices();
@override
Future<List<String>> getDiagnostics() async => getAdbDeviceDiagnostics();

View File

@@ -12,12 +12,36 @@ import '../globals.dart' as globals;
import '../runner/flutter_command.dart';
class DevicesCommand extends FlutterCommand {
DevicesCommand() {
argParser.addOption(
'timeout',
abbr: 't',
defaultsTo: null,
help: 'Time in seconds to wait for devices to attach. Longer timeouts may be necessary for networked devices.'
);
}
@override
final String name = 'devices';
@override
final String description = 'List all connected devices.';
Duration get timeout {
if (argResults['timeout'] == null) {
return null;
}
if (_timeout == null) {
final int timeoutSeconds = int.tryParse(stringArg('timeout'));
if (timeoutSeconds == null) {
throwToolExit( 'Could not parse -t/--timeout argument. It must be an integer.');
}
_timeout = Duration(seconds: timeoutSeconds);
}
return _timeout;
}
Duration _timeout;
@override
Future<FlutterCommandResult> runCommand() async {
if (!doctor.canListAnything) {
@@ -27,14 +51,21 @@ class DevicesCommand extends FlutterCommand {
exitCode: 1);
}
final List<Device> devices = await deviceManager.getAllConnectedDevices();
final List<Device> devices = await deviceManager.refreshAllConnectedDevices(timeout: timeout);
if (devices.isEmpty) {
globals.printStatus(
'No devices detected.\n\n'
"Run 'flutter emulators' to list and start any available device emulators.\n\n"
'Or, if you expected your device to be detected, please run "flutter doctor" to diagnose '
'potential issues, or visit https://flutter.dev/setup/ for troubleshooting tips.');
final StringBuffer status = StringBuffer('No devices detected.');
status.writeln();
status.writeln();
status.writeln('Run "flutter emulators" to list and start any available device emulators.');
status.writeln();
status.write('If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. ');
if (timeout == null) {
status.write('You may also try increasing the time to wait for connected devices with the --timeout flag. ');
}
status.write('Visit https://flutter.dev/setup/ for troubleshooting tips.');
globals.printStatus(status.toString());
final List<String> diagnostics = await deviceManager.getDeviceDiagnostics();
if (diagnostics.isNotEmpty) {
globals.printStatus('');

View File

@@ -124,7 +124,7 @@ class DeviceManager {
return devices.where(startsWithDeviceId).toList();
}
/// Return the list of connected devices, filtered by any user-specified device id.
/// Returns the list of connected devices, filtered by any user-specified device id.
Future<List<Device>> getDevices() {
return hasSpecifiedDeviceId
? getDevicesById(specifiedDeviceId)
@@ -135,7 +135,7 @@ class DeviceManager {
return deviceDiscoverers.where((DeviceDiscovery discoverer) => discoverer.supportsPlatform);
}
/// Return the list of all connected devices.
/// Returns the list of all connected devices.
Future<List<Device>> getAllConnectedDevices() async {
final List<List<Device>> devices = await Future.wait<List<Device>>(<Future<List<Device>>>[
for (final DeviceDiscovery discoverer in _platformDiscoverers)
@@ -145,6 +145,16 @@ class DeviceManager {
return devices.expand<Device>((List<Device> deviceList) => deviceList).toList();
}
/// Returns the list of all connected devices. Discards existing cache of devices.
Future<List<Device>> refreshAllConnectedDevices({ Duration timeout }) async {
final List<List<Device>> devices = await Future.wait<List<Device>>(<Future<List<Device>>>[
for (final DeviceDiscovery discoverer in _platformDiscoverers)
discoverer.discoverDevices(timeout: timeout),
]);
return devices.expand<Device>((List<Device> deviceList) => deviceList).toList();
}
/// Whether we're capable of listing any devices given the current environment configuration.
bool get canListAnything {
return _platformDiscoverers.any((DeviceDiscovery discoverer) => discoverer.canListAnything);
@@ -237,8 +247,12 @@ abstract class DeviceDiscovery {
/// current environment configuration.
bool get canListAnything;
/// Return all connected devices, cached on subsequent calls.
Future<List<Device>> get devices;
/// Return all connected devices. Discards existing cache of devices.
Future<List<Device>> discoverDevices({ Duration timeout });
/// Gets a list of diagnostic messages pertaining to issues with any connected
/// devices (will be an empty list if there are no issues).
Future<List<String>> getDiagnostics() => Future<List<String>>.value(<String>[]);
@@ -256,7 +270,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
ItemListNotifier<Device> _items;
Timer _timer;
Future<List<Device>> pollingGetDevices();
Future<List<Device>> pollingGetDevices({ Duration timeout });
void startPolling() {
if (_timer == null) {
@@ -268,7 +282,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
Timer _initTimer() {
return Timer(_pollingInterval, () async {
try {
final List<Device> devices = await pollingGetDevices().timeout(_pollingTimeout);
final List<Device> devices = await pollingGetDevices(timeout: _pollingTimeout);
_items.updateWithNewList(devices);
} on TimeoutException {
globals.printTrace('Device poll timed out. Will retry.');
@@ -284,7 +298,17 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
@override
Future<List<Device>> get devices async {
_items ??= ItemListNotifier<Device>.from(await pollingGetDevices());
return _populateDevices();
}
@override
Future<List<Device>> discoverDevices({ Duration timeout }) async {
_items = null;
return _populateDevices(timeout: timeout);
}
Future<List<Device>> _populateDevices({ Duration timeout }) async {
_items ??= ItemListNotifier<Device>.from(await pollingGetDevices(timeout: timeout));
return _items.items;
}

View File

@@ -22,7 +22,7 @@ class FuchsiaDevFinder {
/// Returns a list of attached devices as a list of strings with entries
/// formatted as follows:
/// 192.168.42.172 scare-cable-skip-joy
Future<List<String>> list() async {
Future<List<String>> list({ Duration timeout }) async {
if (fuchsiaArtifacts.devFinder == null ||
!fuchsiaArtifacts.devFinder.existsSync()) {
throwToolExit('Fuchsia device-finder tool not found.');
@@ -31,6 +31,8 @@ class FuchsiaDevFinder {
fuchsiaArtifacts.devFinder.path,
'list',
'-full',
if (timeout != null)
...<String>['-timeout', '${timeout.inMilliseconds}ms']
];
final RunResult result = await processUtils.run(command);
if (result.exitCode != 0) {

View File

@@ -145,11 +145,11 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
bool get canListAnything => fuchsiaWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
if (!fuchsiaWorkflow.canListDevices) {
return <Device>[];
}
final String text = await fuchsiaSdk.listDevices();
final String text = await fuchsiaSdk.listDevices(timeout: timeout);
if (text == null || text.isEmpty) {
return <Device>[];
}

View File

@@ -47,12 +47,12 @@ class FuchsiaSdk {
/// Example output:
/// $ device-finder list -full
/// > 192.168.42.56 paper-pulp-bush-angel
Future<String> listDevices() async {
Future<String> listDevices({ Duration timeout }) async {
if (fuchsiaArtifacts.devFinder == null ||
!fuchsiaArtifacts.devFinder.existsSync()) {
return null;
}
final List<String> devices = await fuchsiaDevFinder.list();
final List<String> devices = await fuchsiaDevFinder.list(timeout: timeout);
if (devices == null) {
return null;
}

View File

@@ -38,7 +38,10 @@ class IOSDevices extends PollingDeviceDiscovery {
bool get canListAnything => globals.iosWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() => IOSDevice.getAttachedDevices(globals.platform, globals.xcdevice);
Future<List<Device>> pollingGetDevices({ Duration timeout }) {
return IOSDevice.getAttachedDevices(
globals.platform, globals.xcdevice, timeout: timeout);
}
@override
Future<List<String>> getDiagnostics() => IOSDevice.getDiagnostics(globals.platform, globals.xcdevice);
@@ -109,12 +112,12 @@ class IOSDevice extends Device {
@override
bool get supportsStartPaused => false;
static Future<List<IOSDevice>> getAttachedDevices(Platform platform, XCDevice xcdevice) async {
static Future<List<IOSDevice>> getAttachedDevices(Platform platform, XCDevice xcdevice, { Duration timeout }) async {
if (!platform.isMacOS) {
throw UnsupportedError('Control of iOS devices or simulators only supported on macOS.');
}
return await xcdevice.getAvailableTetheredIOSDevices();
return await xcdevice.getAvailableTetheredIOSDevices(timeout: timeout);
}
static Future<List<String>> getDiagnostics(Platform platform, XCDevice xcdevice) async {

View File

@@ -43,7 +43,7 @@ class IOSSimulators extends PollingDeviceDiscovery {
bool get canListAnything => globals.iosWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async => _iosSimulatorUtils.getAttachedDevices();
Future<List<Device>> pollingGetDevices({ Duration timeout }) async => _iosSimulatorUtils.getAttachedDevices();
}
class IOSSimulatorUtils {

View File

@@ -76,7 +76,7 @@ class LinuxDevices extends PollingDeviceDiscovery {
bool get canListAnything => _linuxWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
if (!canListAnything) {
return const <Device>[];
}

View File

@@ -78,7 +78,7 @@ class MacOSDevices extends PollingDeviceDiscovery {
bool get canListAnything => macOSWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
if (!canListAnything) {
return const <Device>[];
}

View File

@@ -228,7 +228,10 @@ class XCDevice {
return _xcdevicePath;
}
Future<List<dynamic>> _getAllDevices({bool useCache = false}) async {
Future<List<dynamic>> _getAllDevices({
bool useCache = false,
@required Duration timeout
}) async {
if (!isInstalled) {
_logger.printTrace("Xcode not found. Run 'flutter doctor' for more information.");
return null;
@@ -244,7 +247,7 @@ class XCDevice {
'xcdevice',
'list',
'--timeout',
'1',
timeout.inSeconds.toString(),
],
throwOnError: true,
);
@@ -265,9 +268,9 @@ class XCDevice {
List<dynamic> _cachedListResults;
/// List of devices available over USB.
Future<List<IOSDevice>> getAvailableTetheredIOSDevices() async {
final List<dynamic> allAvailableDevices = await _getAllDevices();
/// [timeout] defaults to 1 second.
Future<List<IOSDevice>> getAvailableTetheredIOSDevices({ Duration timeout }) async {
final List<dynamic> allAvailableDevices = await _getAllDevices(timeout: timeout ?? const Duration(seconds: 1));
if (allAvailableDevices == null) {
return const <IOSDevice>[];
@@ -501,7 +504,10 @@ class XCDevice {
/// List of all devices reporting errors.
Future<List<String>> getDiagnostics() async {
final List<dynamic> allAvailableDevices = await _getAllDevices(useCache: true);
final List<dynamic> allAvailableDevices = await _getAllDevices(
useCache: true,
timeout: const Duration(seconds: 1)
);
if (allAvailableDevices == null) {
return const <String>[];

View File

@@ -246,7 +246,7 @@ class FlutterTesterDevices extends PollingDeviceDiscovery {
bool get supportsPlatform => true;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
return showFlutterTesterDevice ? <Device>[_testerDevice] : <Device>[];
}
}

View File

@@ -185,7 +185,7 @@ class WebDevices extends PollingDeviceDiscovery {
bool get canListAnything => featureFlags.isWebEnabled;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
return <Device>[
if (_chromeIsAvailable)
_webDevice,

View File

@@ -66,7 +66,7 @@ class WindowsDevices extends PollingDeviceDiscovery {
bool get canListAnything => windowsWorkflow.canListDevices;
@override
Future<List<Device>> pollingGetDevices() async {
Future<List<Device>> pollingGetDevices({ Duration timeout }) async {
if (!canListAnything) {
return const <Device>[];
}