emulators.dart 4.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../base/common.dart';
import '../base/utils.dart';
import '../doctor.dart';
import '../emulator.dart';
9
import '../globals.dart' as globals;
10 11 12
import '../runner/flutter_command.dart';

class EmulatorsCommand extends FlutterCommand {
13
  EmulatorsCommand() {
14 15
    argParser.addOption('launch',
        help: 'The full or partial ID of the emulator to launch.');
16 17 18 19 20
    argParser.addFlag('create',
        help: 'Creates a new Android emulator based on a Pixel device.',
        negatable: false);
    argParser.addOption('name',
        help: 'Used with flag --create. Specifies a name for the emulator being created.');
21 22
  }

23 24 25 26
  @override
  final String name = 'emulators';

  @override
27
  final String description = 'List, launch and create emulators.';
28

29 30 31
  @override
  final List<String> aliases = <String>['emulator'];

32
  @override
33
  Future<FlutterCommandResult> runCommand() async {
34
    if (globals.doctor.workflows.every((Workflow w) => !w.canListEmulators)) {
35
      throwToolExit(
36
          'Unable to find any emulator sources. Please ensure you have some\n'
Danny Tuppeny's avatar
Danny Tuppeny committed
37
              'Android AVD images ' +
38
              (globals.platform.isMacOS ? 'or an iOS Simulator ' : '') +
Danny Tuppeny's avatar
Danny Tuppeny committed
39
              'available.',
40
          exitCode: 1);
41 42
    }

43
    if (argResults.wasParsed('launch')) {
44
      await _launchEmulator(stringArg('launch'));
45
    } else if (argResults.wasParsed('create')) {
46
      await _createEmulator(name: stringArg('name'));
47
    } else {
48 49 50 51 52
      final String searchText =
          argResults.rest != null && argResults.rest.isNotEmpty
              ? argResults.rest.first
              : null;
      await _listEmulators(searchText);
53
    }
54

55
    return FlutterCommandResult.success();
56 57
  }

58
  Future<void> _launchEmulator(String id) async {
59
    final List<Emulator> emulators =
60
        await emulatorManager.getEmulatorsMatching(id);
61 62

    if (emulators.isEmpty) {
63
      globals.printStatus("No emulator found that matches '$id'.");
64
    } else if (emulators.length > 1) {
65 66 67 68
      _printEmulatorList(
        emulators,
        "More than one emulator matches '$id':",
      );
69
    } else {
Dan Field's avatar
Dan Field committed
70
      await emulators.first.launch();
71 72 73
    }
  }

74
  Future<void> _createEmulator({ String name }) async {
75 76 77 78
    final CreateEmulatorResult createResult =
        await emulatorManager.createEmulator(name: name);

    if (createResult.success) {
79
      globals.printStatus("Emulator '${createResult.emulatorName}' created successfully.");
80
    } else {
81 82
      globals.printStatus("Failed to create emulator '${createResult.emulatorName}'.\n");
      globals.printStatus(createResult.error.trim());
83 84 85 86
      _printAdditionalInfo();
    }
  }

87
  Future<void> _listEmulators(String searchText) async {
Danny Tuppeny's avatar
Danny Tuppeny committed
88
    final List<Emulator> emulators = searchText == null
89 90
        ? await emulatorManager.getAllAvailableEmulators()
        : await emulatorManager.getEmulatorsMatching(searchText);
91 92

    if (emulators.isEmpty) {
93
      globals.printStatus('No emulators available.');
94
      _printAdditionalInfo(showCreateInstruction: true);
95
    } else {
96 97 98 99
      _printEmulatorList(
        emulators,
        '${emulators.length} available ${pluralize('emulator', emulators.length)}:',
      );
100 101
    }
  }
102 103

  void _printEmulatorList(List<Emulator> emulators, String message) {
104
    globals.printStatus('$message\n');
105
    Emulator.printEmulators(emulators, globals.logger);
106 107 108
    _printAdditionalInfo(showCreateInstruction: true, showRunInstruction: true);
  }

109 110 111 112
  void _printAdditionalInfo({
    bool showRunInstruction = false,
    bool showCreateInstruction = false,
  }) {
113
    globals.printStatus('');
114
    if (showRunInstruction) {
115
      globals.printStatus(
116 117 118
          "To run an emulator, run 'flutter emulators --launch <emulator id>'.");
    }
    if (showCreateInstruction) {
119
      globals.printStatus(
120 121 122 123
          "To create a new emulator, run 'flutter emulators --create [--name xyz]'.");
    }

    if (showRunInstruction || showCreateInstruction) {
124
      globals.printStatus('');
125
    }
126
    // TODO(dantup): Update this link to flutter.dev if/when we have a better page.
127
    // That page can then link out to these places if required.
128
    globals.printStatus('You can find more information on managing emulators at the links below:\n'
129 130
        '  https://developer.android.com/studio/run/managing-avds\n'
        '  https://developer.android.com/studio/command-line/avdmanager');
131
  }
132
}