emulators.dart 4.44 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 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';

import '../base/common.dart';
8
import '../base/platform.dart';
9 10 11 12 13 14 15
import '../base/utils.dart';
import '../doctor.dart';
import '../emulator.dart';
import '../globals.dart';
import '../runner/flutter_command.dart';

class EmulatorsCommand extends FlutterCommand {
16
  EmulatorsCommand() {
17 18
    argParser.addOption('launch',
        help: 'The full or partial ID of the emulator to launch.');
19 20 21 22 23
    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.');
24 25
  }

26 27 28 29
  @override
  final String name = 'emulators';

  @override
30
  final String description = 'List, launch and create emulators.';
31

32 33 34
  @override
  final List<String> aliases = <String>['emulator'];

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

46 47
    if (argResults.wasParsed('launch')) {
      await _launchEmulator(argResults['launch']);
48 49
    } else if (argResults.wasParsed('create')) {
      await _createEmulator(name: argResults['name']);
50
    } else {
51 52 53 54 55
      final String searchText =
          argResults.rest != null && argResults.rest.isNotEmpty
              ? argResults.rest.first
              : null;
      await _listEmulators(searchText);
56
    }
57 58

    return null;
59 60
  }

61
  Future<void> _launchEmulator(String id) async {
62
    final List<Emulator> emulators =
63
        await emulatorManager.getEmulatorsMatching(id);
64 65

    if (emulators.isEmpty) {
66
      printStatus("No emulator found that matches '$id'.");
67
    } else if (emulators.length > 1) {
68 69 70 71
      _printEmulatorList(
        emulators,
        "More than one emulator matches '$id':",
      );
72
    } else {
73 74 75 76
      try {
        await emulators.first.launch();
      }
      catch (e) {
77 78 79 80 81
        if (e is String) {
          printError(e);
        } else {
          rethrow;
        }
82
      }
83 84 85
    }
  }

86
  Future<void> _createEmulator({String name}) async {
87 88 89 90 91 92 93 94 95 96 97 98
    final CreateEmulatorResult createResult =
        await emulatorManager.createEmulator(name: name);

    if (createResult.success) {
      printStatus("Emulator '${createResult.emulatorName}' created successfully.");
    } else {
      printStatus("Failed to create emulator '${createResult.emulatorName}'.\n");
      printStatus(createResult.error.trim());
      _printAdditionalInfo();
    }
  }

99
  Future<void> _listEmulators(String searchText) async {
Danny Tuppeny's avatar
Danny Tuppeny committed
100
    final List<Emulator> emulators = searchText == null
101 102
        ? await emulatorManager.getAllAvailableEmulators()
        : await emulatorManager.getEmulatorsMatching(searchText);
103 104

    if (emulators.isEmpty) {
105 106
      printStatus('No emulators available.');
      _printAdditionalInfo(showCreateInstruction: true);
107
    } else {
108 109 110 111
      _printEmulatorList(
        emulators,
        '${emulators.length} available ${pluralize('emulator', emulators.length)}:',
      );
112 113
    }
  }
114 115 116 117

  void _printEmulatorList(List<Emulator> emulators, String message) {
    printStatus('$message\n');
    Emulator.printEmulators(emulators);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    _printAdditionalInfo(showCreateInstruction: true, showRunInstruction: true);
  }

  void _printAdditionalInfo({ bool showRunInstruction = false,
      bool showCreateInstruction = false }) {
    printStatus('');
    if (showRunInstruction) {
      printStatus(
          "To run an emulator, run 'flutter emulators --launch <emulator id>'.");
    }
    if (showCreateInstruction) {
      printStatus(
          "To create a new emulator, run 'flutter emulators --create [--name xyz]'.");
    }

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