• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import '../base/common.dart';
8import '../base/utils.dart';
9import '../device.dart';
10import '../doctor.dart';
11import '../globals.dart';
12import '../runner/flutter_command.dart';
13
14class DevicesCommand extends FlutterCommand {
15  @override
16  final String name = 'devices';
17
18  @override
19  final String description = 'List all connected devices.';
20
21  @override
22  Future<FlutterCommandResult> runCommand() async {
23    if (!doctor.canListAnything) {
24      throwToolExit(
25        "Unable to locate a development device; please run 'flutter doctor' for "
26        'information about installing additional components.',
27        exitCode: 1);
28    }
29
30    final List<Device> devices = await deviceManager.getAllConnectedDevices().toList();
31
32    if (devices.isEmpty) {
33      printStatus(
34        'No devices detected.\n\n'
35        "Run 'flutter emulators' to list and start any available device emulators.\n\n"
36        'Or, if you expected your device to be detected, please run "flutter doctor" to diagnose '
37        'potential issues, or visit https://flutter.dev/setup/ for troubleshooting tips.');
38      final List<String> diagnostics = await deviceManager.getDeviceDiagnostics();
39      if (diagnostics.isNotEmpty) {
40        printStatus('');
41        for (String diagnostic in diagnostics) {
42          printStatus('• $diagnostic', hangingIndent: 2);
43        }
44      }
45    } else {
46      printStatus('${devices.length} connected ${pluralize('device', devices.length)}:\n');
47      await Device.printDevices(devices);
48    }
49
50    return null;
51  }
52}
53