devices.dart 1.68 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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';

7
import '../base/common.dart';
8
import '../base/utils.dart';
9
import '../device.dart';
10
import '../doctor.dart';
11
import '../globals.dart';
12
import '../runner/flutter_command.dart';
13

14
class 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 25
      throwToolExit(
        "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 34
      printStatus(
        '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 39 40 41
      final List<String> diagnostics = await deviceManager.getDeviceDiagnostics();
      if (diagnostics.isNotEmpty) {
        printStatus('');
        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
  }
}