"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter_tools/lib/src/commands/emulators.dart" (13 Nov 2020, 4530 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter 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 
    5 import 'dart:async';
    6 
    7 import '../base/common.dart';
    8 import '../base/utils.dart';
    9 import '../doctor.dart';
   10 import '../emulator.dart';
   11 import '../globals.dart' as globals;
   12 import '../runner/flutter_command.dart';
   13 
   14 class EmulatorsCommand extends FlutterCommand {
   15   EmulatorsCommand() {
   16     argParser.addOption('launch',
   17         help: 'The full or partial ID of the emulator to launch.');
   18     argParser.addFlag('create',
   19         help: 'Creates a new Android emulator based on a Pixel device.',
   20         negatable: false);
   21     argParser.addOption('name',
   22         help: 'Used with flag --create. Specifies a name for the emulator being created.');
   23   }
   24 
   25   @override
   26   final String name = 'emulators';
   27 
   28   @override
   29   final String description = 'List, launch and create emulators.';
   30 
   31   @override
   32   final List<String> aliases = <String>['emulator'];
   33 
   34   @override
   35   Future<FlutterCommandResult> runCommand() async {
   36     if (globals.doctor.workflows.every((Workflow w) => !w.canListEmulators)) {
   37       throwToolExit(
   38           'Unable to find any emulator sources. Please ensure you have some\n'
   39               'Android AVD images ' +
   40               (globals.platform.isMacOS ? 'or an iOS Simulator ' : '') +
   41               'available.',
   42           exitCode: 1);
   43     }
   44 
   45     if (argResults.wasParsed('launch')) {
   46       await _launchEmulator(stringArg('launch'));
   47     } else if (argResults.wasParsed('create')) {
   48       await _createEmulator(name: stringArg('name'));
   49     } else {
   50       final String searchText =
   51           argResults.rest != null && argResults.rest.isNotEmpty
   52               ? argResults.rest.first
   53               : null;
   54       await _listEmulators(searchText);
   55     }
   56 
   57     return FlutterCommandResult.success();
   58   }
   59 
   60   Future<void> _launchEmulator(String id) async {
   61     final List<Emulator> emulators =
   62         await emulatorManager.getEmulatorsMatching(id);
   63 
   64     if (emulators.isEmpty) {
   65       globals.printStatus("No emulator found that matches '$id'.");
   66     } else if (emulators.length > 1) {
   67       _printEmulatorList(
   68         emulators,
   69         "More than one emulator matches '$id':",
   70       );
   71     } else {
   72       await emulators.first.launch();
   73     }
   74   }
   75 
   76   Future<void> _createEmulator({ String name }) async {
   77     final CreateEmulatorResult createResult =
   78         await emulatorManager.createEmulator(name: name);
   79 
   80     if (createResult.success) {
   81       globals.printStatus("Emulator '${createResult.emulatorName}' created successfully.");
   82     } else {
   83       globals.printStatus("Failed to create emulator '${createResult.emulatorName}'.\n");
   84       globals.printStatus(createResult.error.trim());
   85       _printAdditionalInfo();
   86     }
   87   }
   88 
   89   Future<void> _listEmulators(String searchText) async {
   90     final List<Emulator> emulators = searchText == null
   91         ? await emulatorManager.getAllAvailableEmulators()
   92         : await emulatorManager.getEmulatorsMatching(searchText);
   93 
   94     if (emulators.isEmpty) {
   95       globals.printStatus('No emulators available.');
   96       _printAdditionalInfo(showCreateInstruction: true);
   97     } else {
   98       _printEmulatorList(
   99         emulators,
  100         '${emulators.length} available ${pluralize('emulator', emulators.length)}:',
  101       );
  102     }
  103   }
  104 
  105   void _printEmulatorList(List<Emulator> emulators, String message) {
  106     globals.printStatus('$message\n');
  107     Emulator.printEmulators(emulators, globals.logger);
  108     _printAdditionalInfo(showCreateInstruction: true, showRunInstruction: true);
  109   }
  110 
  111   void _printAdditionalInfo({
  112     bool showRunInstruction = false,
  113     bool showCreateInstruction = false,
  114   }) {
  115     globals.printStatus('');
  116     if (showRunInstruction) {
  117       globals.printStatus(
  118           "To run an emulator, run 'flutter emulators --launch <emulator id>'.");
  119     }
  120     if (showCreateInstruction) {
  121       globals.printStatus(
  122           "To create a new emulator, run 'flutter emulators --create [--name xyz]'.");
  123     }
  124 
  125     if (showRunInstruction || showCreateInstruction) {
  126       globals.printStatus('');
  127     }
  128     // TODO(dantup): Update this link to flutter.dev if/when we have a better page.
  129     // That page can then link out to these places if required.
  130     globals.printStatus('You can find more information on managing emulators at the links below:\n'
  131         '  https://developer.android.com/studio/run/managing-avds\n'
  132         '  https://developer.android.com/studio/command-line/avdmanager');
  133   }
  134 }