flutter_command.dart 25.4 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 'package:args/args.dart';
8
import 'package:args/command_runner.dart';
9
import 'package:meta/meta.dart';
10
import 'package:quiver/strings.dart';
11 12

import '../application_package.dart';
13
import '../base/common.dart';
14
import '../base/context.dart';
15
import '../base/file_system.dart';
16
import '../base/io.dart' as io;
17
import '../base/terminal.dart';
18
import '../base/time.dart';
19
import '../base/user_messages.dart';
20
import '../base/utils.dart';
21
import '../build_info.dart';
22
import '../bundle.dart' as bundle;
23
import '../cache.dart';
24
import '../dart/package_map.dart';
25
import '../dart/pub.dart';
26
import '../device.dart';
27
import '../doctor.dart';
28
import '../features.dart';
29
import '../globals.dart';
30
import '../project.dart';
31
import '../reporting/reporting.dart';
32 33
import 'flutter_command_runner.dart';

34 35
export '../cache.dart' show DevelopmentArtifact;

36 37 38 39 40 41 42
enum ExitStatus {
  success,
  warning,
  fail,
}

/// [FlutterCommand]s' subclasses' [FlutterCommand.runCommand] can optionally
43
/// provide a [FlutterCommandResult] to furnish additional information for
44 45
/// analytics.
class FlutterCommandResult {
46
  const FlutterCommandResult(
47
    this.exitStatus, {
48
    this.timingLabelParts,
49
    this.endTimeOverride,
50
  });
51 52 53

  final ExitStatus exitStatus;

54
  /// Optional data that can be appended to the timing event.
55 56
  /// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#timingLabel
  /// Do not add PII.
57
  final List<String> timingLabelParts;
58

59
  /// Optional epoch time when the command's non-interactive wait time is
60
  /// complete during the command's execution. Use to measure user perceivable
61 62
  /// latency without measuring user interaction time.
  ///
63
  /// [FlutterCommand] will automatically measure and report the command's
64
  /// complete time if not overridden.
65
  final DateTime endTimeOverride;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  @override
  String toString() {
    switch (exitStatus) {
      case ExitStatus.success:
        return 'success';
      case ExitStatus.warning:
        return 'warning';
      case ExitStatus.fail:
        return 'fail';
      default:
        assert(false);
        return null;
    }
  }
81 82
}

83 84 85 86
/// Common flutter command line options.
class FlutterOptions {
  static const String kExtraFrontEndOptions = 'extra-front-end-options';
  static const String kExtraGenSnapshotOptions = 'extra-gen-snapshot-options';
87
  static const String kEnableExperiment = 'enable-experiment';
88 89
  static const String kFileSystemRoot = 'filesystem-root';
  static const String kFileSystemScheme = 'filesystem-scheme';
90 91
}

92
abstract class FlutterCommand extends Command<void> {
93 94 95
  /// The currently executing command (or sub-command).
  ///
  /// Will be `null` until the top-most command has begun execution.
96
  static FlutterCommand get current => context.get<FlutterCommand>();
97

98 99 100 101 102 103
  /// The option name for a custom observatory port.
  static const String observatoryPortOption = 'observatory-port';

  /// The flag name for whether or not to use ipv6.
  static const String ipv6Flag = 'ipv6';

104 105
  @override
  ArgParser get argParser => _argParser;
106 107 108 109
  final ArgParser _argParser = ArgParser(
    allowTrailingOptions: false,
    usageLineLength: outputPreferences.wrapText ? outputPreferences.wrapColumn : null,
  );
110

111
  @override
112 113
  FlutterCommandRunner get runner => super.runner;

114 115
  bool _requiresPubspecYaml = false;

116 117 118 119
  /// Whether this command uses the 'target' option.
  bool _usesTargetOption = false;

  bool _usesPubOption = false;
120

121 122 123 124
  bool _usesPortOption = false;

  bool _usesIpv6Flag = false;

125 126
  bool get shouldRunPub => _usesPubOption && argResults['pub'];

127 128
  bool get shouldUpdateCache => true;

129 130
  bool _excludeDebug = false;

131 132
  BuildMode _defaultBuildMode;

133 134 135 136
  void requiresPubspecYaml() {
    _requiresPubspecYaml = true;
  }

137
  void usesWebOptions({ bool hide = true }) {
138
    argParser.addOption('web-hostname',
139 140 141 142
      defaultsTo: 'localhost',
      help: 'The hostname to serve web application on.',
      hide: hide,
    );
143
    argParser.addOption('web-port',
144 145 146 147 148
      defaultsTo: null,
      help: 'The host port to serve the web application from. If not provided, the tool '
        'will select a random open port on the host.',
      hide: hide,
    );
149 150 151 152 153 154 155 156
    argParser.addFlag('web-browser-launch',
      defaultsTo: true,
      negatable: true,
      help: 'Whether to automatically launch browsers for web devices '
        'that do so. Setting this to true allows using the Dart debug extension '
        'on Chrome and other browsers which support extensions.',
      hide: hide,
    );
157 158
  }

159 160 161
  void usesTargetOption() {
    argParser.addOption('target',
      abbr: 't',
162
      defaultsTo: bundle.defaultMainPath,
163
      help: 'The main entry-point file of the application, as run on the device.\n'
164
            'If the --target option is omitted, but a file name is provided on '
165 166
            'the command line, then that is used instead.',
      valueHelp: 'path');
167 168 169
    _usesTargetOption = true;
  }

170
  String get targetFile {
171
    if (argResults.wasParsed('target')) {
172
      return argResults['target'];
173 174
    }
    if (argResults.rest.isNotEmpty) {
175
      return argResults.rest.first;
176 177
    }
    return bundle.defaultMainPath;
178 179
  }

180 181 182
  void usesPubOption() {
    argParser.addFlag('pub',
      defaultsTo: true,
183
      help: 'Whether to run "flutter pub get" before executing this command.');
184 185 186
    _usesPubOption = true;
  }

187 188 189 190
  /// Adds flags for using a specific filesystem root and scheme.
  ///
  /// [hide] indicates whether or not to hide these options when the user asks
  /// for help.
191
  void usesFilesystemOptions({ @required bool hide }) {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    argParser
      ..addOption('output-dill',
        hide: hide,
        help: 'Specify the path to frontend server output kernel file.',
      )
      ..addMultiOption(FlutterOptions.kFileSystemRoot,
        hide: hide,
        help: 'Specify the path, that is used as root in a virtual file system\n'
            'for compilation. Input file name should be specified as Uri in\n'
            'filesystem-scheme scheme. Use only in Dart 2 mode.\n'
            'Requires --output-dill option to be explicitly specified.\n',
      )
      ..addOption(FlutterOptions.kFileSystemScheme,
        defaultsTo: 'org-dartlang-root',
        hide: hide,
        help: 'Specify the scheme that is used for virtual file system used in\n'
            'compilation. See more details on filesystem-root option.\n',
      );
  }

212 213 214 215
  /// Adds options for connecting to the Dart VM observatory port.
  void usesPortOptions() {
    argParser.addOption(observatoryPortOption,
        help: 'Listen to the given port for an observatory debugger connection.\n'
216
              'Specifying port 0 (the default) will find a random free port.',
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    );
    _usesPortOption = true;
  }

  /// Gets the observatory port provided to in the 'observatory-port' option.
  ///
  /// If no port is set, returns null.
  int get observatoryPort {
    if (!_usesPortOption || argResults['observatory-port'] == null) {
      return null;
    }
    try {
      return int.parse(argResults['observatory-port']);
    } catch (error) {
      throwToolExit('Invalid port for `--observatory-port`: $error');
    }
    return null;
  }

  void usesIpv6Flag() {
    argParser.addFlag(ipv6Flag,
      hide: true,
      negatable: false,
      help: 'Binds to IPv6 localhost instead of IPv4 when the flutter tool '
            'forwards the host port to a device port. Not used when the '
            '--debug-port flag is not set.',
    );
    _usesIpv6Flag = true;
  }

  bool get ipv6 => _usesIpv6Flag ? argResults['ipv6'] : null;

249 250
  void usesBuildNumberOption() {
    argParser.addOption('build-number',
251 252
        help: 'An identifier used as an internal version number.\n'
              'Each build must have a unique identifier to differentiate it from previous builds.\n'
253 254 255
              'It is used to determine whether one build is more recent than another, with higher numbers indicating more recent build.\n'
              'On Android it is used as \'versionCode\'.\n'
              'On Xcode builds it is used as \'CFBundleVersion\'',
256
    );
257 258 259 260 261 262 263 264 265 266 267
  }

  void usesBuildNameOption() {
    argParser.addOption('build-name',
        help: 'A "x.y.z" string used as the version number shown to users.\n'
              'For each new version of your app, you will provide a version number to differentiate it from previous versions.\n'
              'On Android it is used as \'versionName\'.\n'
              'On Xcode builds it is used as \'CFBundleShortVersionString\'',
        valueHelp: 'x.y.z');
  }

268
  void usesIsolateFilterOption({ @required bool hide }) {
269 270 271 272 273 274 275
    argParser.addOption('isolate-filter',
      defaultsTo: null,
      hide: hide,
      help: 'Restricts commands to a subset of the available isolates (running instances of Flutter).\n'
            'Normally there\'s only one, but when adding Flutter to a pre-existing app it\'s possible to create multiple.');
  }

276 277 278 279
  void addBuildModeFlags({ bool defaultToRelease = true, bool verboseHelp = false, bool excludeDebug = false }) {
    // A release build must be the default if a debug build is not possible.
    assert(defaultToRelease || !excludeDebug);
    _excludeDebug = excludeDebug;
280
    defaultBuildMode = defaultToRelease ? BuildMode.release : BuildMode.debug;
281

282 283 284 285 286
    if (!excludeDebug) {
      argParser.addFlag('debug',
        negatable: false,
        help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
    }
287 288
    argParser.addFlag('profile',
      negatable: false,
289
      help: 'Build a version of your app specialized for performance profiling.');
290
    argParser.addFlag('release',
291
      negatable: false,
292
      help: 'Build a release version of your app${defaultToRelease ? ' (default mode)' : ''}.');
293 294
  }

Emmanuel Garcia's avatar
Emmanuel Garcia committed
295 296 297 298 299 300 301 302 303
  void addShrinkingFlag() {
    argParser.addFlag('shrink',
      negatable: true,
      defaultsTo: true,
      help: 'Whether to enable code shrinking on release mode.'
            'When enabling shrinking, you also benefit from obfuscation, '
            'which shortens the names of your app’s classes and members, '
            'and optimization, which applies more aggressive strategies to '
            'further reduce the size of your app.'
304
            'To learn more, see: https://developer.android.com/studio/build/shrink-code',
Emmanuel Garcia's avatar
Emmanuel Garcia committed
305 306 307
      );
  }

308
  void usesFuchsiaOptions({ bool hide = false }) {
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    argParser.addOption(
      'target-model',
      help: 'Target model that determines what core libraries are available',
      defaultsTo: 'flutter',
      hide: hide,
      allowed: const <String>['flutter', 'flutter_runner'],
    );
    argParser.addOption(
      'module',
      abbr: 'm',
      hide: hide,
      help: 'The name of the module (required if attaching to a fuchsia device)',
      valueHelp: 'module-name',
    );
  }

325 326
  set defaultBuildMode(BuildMode value) {
    _defaultBuildMode = value;
327 328
  }

329
  BuildMode getBuildMode() {
330 331
    final bool debugResult = _excludeDebug ? false : argResults['debug'];
    final List<bool> modeFlags = <bool>[debugResult, argResults['profile'], argResults['release']];
332
    if (modeFlags.where((bool flag) => flag).length > 1) {
333
      throw UsageException('Only one of --debug, --profile, or --release can be specified.', null);
334
    }
335
    if (debugResult) {
336
      return BuildMode.debug;
337
    }
338 339 340 341 342 343
    if (argResults['profile']) {
      return BuildMode.profile;
    }
    if (argResults['release']) {
      return BuildMode.release;
    }
344
    return _defaultBuildMode;
345 346
  }

347 348 349 350
  void usesFlavorOption() {
    argParser.addOption(
      'flavor',
      help: 'Build a custom app flavor as defined by platform-specific build setup.\n'
351 352 353 354 355 356 357 358 359 360 361 362
            'Supports the use of product flavors in Android Gradle scripts, and '
            'the use of custom Xcode schemes.',
    );
  }

  void usesTrackWidgetCreation({ bool hasEffect = true, @required bool verboseHelp }) {
    argParser.addFlag(
      'track-widget-creation',
      hide: !hasEffect && !verboseHelp,
      defaultsTo: false, // this will soon be changed to true
      help: 'Track widget creation locations. This enables features such as the widget inspector. '
            'This parameter is only functional in debug mode (i.e. when compiling JIT, not AOT).',
363 364 365 366
    );
  }

  BuildInfo getBuildInfo() {
367 368 369 370
    final bool trackWidgetCreation = argParser.options.containsKey('track-widget-creation')
        ? argResults['track-widget-creation']
        : false;

371 372 373
    final String buildNumber = argParser.options.containsKey('build-number') && argResults['build-number'] != null
        ? argResults['build-number']
        : null;
374

375
    String extraFrontEndOptions =
376 377 378 379 380 381 382 383
        argParser.options.containsKey(FlutterOptions.kExtraFrontEndOptions)
            ? argResults[FlutterOptions.kExtraFrontEndOptions]
            : null;
    if (argParser.options.containsKey(FlutterOptions.kEnableExperiment) &&
        argResults[FlutterOptions.kEnableExperiment] != null) {
      for (String expFlag in argResults[FlutterOptions.kEnableExperiment]) {
        final String flag = '--enable-experiment=' + expFlag;
        if (extraFrontEndOptions != null) {
384
          extraFrontEndOptions += ',' + flag;
385
        } else {
386
          extraFrontEndOptions = flag;
387 388 389 390
        }
      }
    }

391
    return BuildInfo(getBuildMode(),
392 393 394
      argParser.options.containsKey('flavor')
        ? argResults['flavor']
        : null,
395
      trackWidgetCreation: trackWidgetCreation,
396
      extraFrontEndOptions: extraFrontEndOptions,
397
      extraGenSnapshotOptions: argParser.options.containsKey(FlutterOptions.kExtraGenSnapshotOptions)
398
          ? argResults[FlutterOptions.kExtraGenSnapshotOptions]
399
          : null,
400 401 402 403
      fileSystemRoots: argParser.options.containsKey(FlutterOptions.kFileSystemRoot)
          ? argResults[FlutterOptions.kFileSystemRoot] : null,
      fileSystemScheme: argParser.options.containsKey(FlutterOptions.kFileSystemScheme)
          ? argResults[FlutterOptions.kFileSystemScheme] : null,
404 405 406 407
      buildNumber: buildNumber,
      buildName: argParser.options.containsKey('build-name')
          ? argResults['build-name']
          : null,
408
    );
409 410
  }

411
  void setupApplicationPackages() {
412
    applicationPackages ??= ApplicationPackageStore();
413 414
  }

415
  /// The path to send to Google Analytics. Return null here to disable
416
  /// tracking of the command.
417 418 419 420 421 422 423 424 425 426
  Future<String> get usagePath async {
    if (parent is FlutterCommand) {
      final FlutterCommand commandParent = parent;
      final String path = await commandParent.usagePath;
      // Don't report for parents that return null for usagePath.
      return path == null ? null : '$path/$name';
    } else {
      return name;
    }
  }
427

428
  /// Additional usage values to be sent with the usage ping.
429 430
  Future<Map<CustomDimensions, String>> get usageValues async =>
      const <CustomDimensions, String>{};
431

432 433 434 435 436 437
  /// Runs this command.
  ///
  /// Rather than overriding this method, subclasses should override
  /// [verifyThenRunCommand] to perform any verification
  /// and [runCommand] to execute the command
  /// so that this method can record and report the overall time to analytics.
438
  @override
439
  Future<void> run() {
440
    final DateTime startTime = systemClock.now();
Devon Carew's avatar
Devon Carew committed
441

442
    return context.run<void>(
443 444 445
      name: 'command',
      overrides: <Type, Generator>{FlutterCommand: () => this},
      body: () async {
446
        if (flutterUsage.isFirstRun) {
447
          flutterUsage.printWelcome();
448
        }
449
        final String commandPath = await usagePath;
450 451
        FlutterCommandResult commandResult;
        try {
452
          commandResult = await verifyThenRunCommand(commandPath);
453 454 455 456
        } on ToolExit {
          commandResult = const FlutterCommandResult(ExitStatus.fail);
          rethrow;
        } finally {
457
          final DateTime endTime = systemClock.now();
458
          printTrace(userMessages.flutterElapsedTime(name, getElapsedAsMilliseconds(endTime.difference(startTime))));
459
          _sendPostUsage(commandPath, commandResult, startTime, endTime);
460 461 462
        }
      },
    );
Devon Carew's avatar
Devon Carew committed
463 464
  }

465 466 467 468
  /// Logs data about this command.
  ///
  /// For example, the command path (e.g. `build/apk`) and the result,
  /// as well as the time spent running it.
469 470
  void _sendPostUsage(String commandPath, FlutterCommandResult commandResult,
                      DateTime startTime, DateTime endTime) {
471 472 473 474
    if (commandPath == null) {
      return;
    }

475
    // Send command result.
476
    CommandResultEvent(commandPath, commandResult).send();
477 478

    // Send timing.
479 480 481 482 483 484
    final List<String> labels = <String>[
      if (commandResult?.exitStatus != null)
        getEnumName(commandResult.exitStatus),
      if (commandResult?.timingLabelParts?.isNotEmpty ?? false)
        ...commandResult.timingLabelParts,
    ];
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

    final String label = labels
        .where((String label) => !isBlank(label))
        .join('-');
    flutterUsage.sendTiming(
      'flutter',
      name,
      // If the command provides its own end time, use it. Otherwise report
      // the duration of the entire execution.
      (commandResult?.endTimeOverride ?? endTime).difference(startTime),
      // Report in the form of `success-[parameter1-parameter2]`, all of which
      // can be null if the command doesn't provide a FlutterCommandResult.
      label: label == '' ? null : label,
    );
  }

501 502 503 504 505 506 507 508
  /// Perform validation then call [runCommand] to execute the command.
  /// Return a [Future] that completes with an exit code
  /// indicating whether execution was successful.
  ///
  /// Subclasses should override this method to perform verification
  /// then call this method to execute the command
  /// rather than calling [runCommand] directly.
  @mustCallSuper
509
  Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
510
    await validateCommand();
511

512 513
    // Populate the cache. We call this before pub get below so that the sky_engine
    // package is available in the flutter cache for pub to find.
514
    if (shouldUpdateCache) {
515
      await cache.updateAll(await requiredArtifacts);
516
    }
517

518
    if (shouldRunPub) {
519
      await pubGet(context: PubContext.getVerifyContext(name));
520
      final FlutterProject project = FlutterProject.current();
521
      await project.ensureReadyForPlatformSpecificTooling(checkProjects: true);
522
    }
523

524
    setupApplicationPackages();
Devon Carew's avatar
Devon Carew committed
525

526
    if (commandPath != null) {
527 528 529 530 531 532
      final Map<CustomDimensions, String> additionalUsageValues =
        <CustomDimensions, String>{
          ...?await usageValues,
          CustomDimensions.commandHasTerminal: io.stdout.hasTerminal ? 'true' : 'false',
        };
      Usage.command(commandPath, parameters: additionalUsageValues);
533 534
    }

535
    return await runCommand();
536 537
  }

538 539
  /// The set of development artifacts required for this command.
  ///
540
  /// Defaults to [DevelopmentArtifact.universal].
541
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
542 543 544
    DevelopmentArtifact.universal,
  };

545
  /// Subclasses must implement this to execute the command.
546
  /// Optionally provide a [FlutterCommandResult] to send more details about the
547 548
  /// execution for analytics.
  Future<FlutterCommandResult> runCommand();
549

550
  /// Find and return all target [Device]s based upon currently connected
551
  /// devices and criteria entered by the user on the command line.
552
  /// If no device can be found that meets specified criteria,
553
  /// then print an error message and return null.
554
  Future<List<Device>> findAllTargetDevices() async {
555
    if (!doctor.canLaunchAnything) {
556
      printError(userMessages.flutterNoDevelopmentDevice);
557 558 559
      return null;
    }

560
    List<Device> devices = await deviceManager.findTargetDevices(FlutterProject.current());
561 562

    if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) {
563
      printStatus(userMessages.flutterNoMatchingDevice(deviceManager.specifiedDeviceId));
564
      return null;
565
    } else if (devices.isEmpty && deviceManager.hasSpecifiedAllDevices) {
566
      printStatus(userMessages.flutterNoDevicesFound);
567
      return null;
568
    } else if (devices.isEmpty) {
569
      printStatus(userMessages.flutterNoSupportedDevices);
570
      return null;
571
    } else if (devices.length > 1 && !deviceManager.hasSpecifiedAllDevices) {
572
      if (deviceManager.hasSpecifiedDeviceId) {
573
        printStatus(userMessages.flutterFoundSpecifiedDevices(devices.length, deviceManager.specifiedDeviceId));
574
      } else {
575
        printStatus(userMessages.flutterSpecifyDeviceWithAllOption);
576
        devices = await deviceManager.getAllConnectedDevices().toList();
577 578
      }
      printStatus('');
579
      await Device.printDevices(devices);
580 581
      return null;
    }
582 583 584 585 586 587
    return devices;
  }

  /// Find and return the target [Device] based upon currently connected
  /// devices and criteria entered by the user on the command line.
  /// If a device cannot be found that meets specified criteria,
588
  /// then print an error message and return null.
589 590
  Future<Device> findTargetDevice() async {
    List<Device> deviceList = await findAllTargetDevices();
591
    if (deviceList == null) {
592
      return null;
593
    }
594
    if (deviceList.length > 1) {
595
      printStatus(userMessages.flutterSpecifyDevice);
596 597 598 599 600 601
      deviceList = await deviceManager.getAllConnectedDevices().toList();
      printStatus('');
      await Device.printDevices(deviceList);
      return null;
    }
    return deviceList.single;
602 603
  }

604 605
  @protected
  @mustCallSuper
606
  Future<void> validateCommand() async {
607
    if (_requiresPubspecYaml && !PackageMap.isUsingCustomPackagesPath) {
608
      // Don't expect a pubspec.yaml file if the user passed in an explicit .packages file path.
609
      if (!fs.isFileSync('pubspec.yaml')) {
610
        throw ToolExit(userMessages.flutterNoPubspec);
611
      }
612

613
      if (fs.isFileSync('flutter.yaml')) {
614
        throw ToolExit(userMessages.flutterMergeYamlFiles);
615
      }
616 617

      // Validate the current package map only if we will not be running "pub get" later.
618
      if (parent?.name != 'pub' && !(_usesPubOption && argResults['pub'])) {
619
        final String error = PackageMap(PackageMap.globalPackagesPath).checkValid();
620
        if (error != null) {
621
          throw ToolExit(error);
622
        }
623
      }
624
    }
625 626

    if (_usesTargetOption) {
627
      final String targetPath = targetFile;
628
      if (!fs.isFileSync(targetPath)) {
629
        throw ToolExit(userMessages.flutterTargetFileMissing(targetPath));
630
      }
631 632
    }
  }
633

634 635
  ApplicationPackageStore applicationPackages;
}
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/// A mixin which applies an implementation of [requiredArtifacts] that only
/// downloads artifacts corresponding to an attached device.
mixin DeviceBasedDevelopmentArtifacts on FlutterCommand {
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async {
    // If there are no attached devices, use the default configuration.
    // Otherwise, only add development artifacts which correspond to a
    // connected device.
    final List<Device> devices = await deviceManager.getDevices().toList();
    if (devices.isEmpty) {
      return super.requiredArtifacts;
    }
    final Set<DevelopmentArtifact> artifacts = <DevelopmentArtifact>{
      DevelopmentArtifact.universal,
    };
    for (Device device in devices) {
      final TargetPlatform targetPlatform = await device.targetPlatform;
654 655 656
      final DevelopmentArtifact developmentArtifact = _artifactFromTargetPlatform(targetPlatform);
      if (developmentArtifact != null) {
        artifacts.add(developmentArtifact);
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
      }
    }
    return artifacts;
  }
}

/// A mixin which applies an implementation of [requiredArtifacts] that only
/// downloads artifacts corresponding to a target device.
mixin TargetPlatformBasedDevelopmentArtifacts on FlutterCommand {
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async {
    // If there is no specified target device, fallback to the default
    // confiugration.
    final String rawTargetPlatform = argResults['target-platform'];
    final TargetPlatform targetPlatform = getTargetPlatformForName(rawTargetPlatform);
    if (targetPlatform == null) {
      return super.requiredArtifacts;
    }

    final Set<DevelopmentArtifact> artifacts = <DevelopmentArtifact>{
      DevelopmentArtifact.universal,
    };
679 680 681
    final DevelopmentArtifact developmentArtifact = _artifactFromTargetPlatform(targetPlatform);
    if (developmentArtifact != null) {
      artifacts.add(developmentArtifact);
682 683 684 685 686
    }
    return artifacts;
  }
}

687 688 689 690 691 692 693 694
// Returns the development artifact for the target platform, or null
// if none is supported
DevelopmentArtifact _artifactFromTargetPlatform(TargetPlatform targetPlatform) {
  switch (targetPlatform) {
    case TargetPlatform.android_arm:
    case TargetPlatform.android_arm64:
    case TargetPlatform.android_x64:
    case TargetPlatform.android_x86:
695
      return DevelopmentArtifact.androidGenSnapshot;
696
    case TargetPlatform.web_javascript:
697 698 699 700
      return DevelopmentArtifact.web;
    case TargetPlatform.ios:
      return DevelopmentArtifact.iOS;
    case TargetPlatform.darwin_x64:
701
      if (featureFlags.isMacOSEnabled) {
702 703 704 705
        return DevelopmentArtifact.macOS;
      }
      return null;
    case TargetPlatform.windows_x64:
706
      if (featureFlags.isWindowsEnabled) {
707 708 709 710
        return DevelopmentArtifact.windows;
      }
      return null;
    case TargetPlatform.linux_x64:
711
      if (featureFlags.isLinuxEnabled) {
712 713 714 715 716 717 718 719 720 721 722
        return DevelopmentArtifact.linux;
      }
      return null;
    case TargetPlatform.fuchsia:
    case TargetPlatform.tester:
      // No artifacts currently supported.
      return null;
  }
  return null;
}

723 724 725 726 727 728 729 730 731 732 733
/// A command which runs less analytics and checks to speed up startup time.
abstract class FastFlutterCommand extends FlutterCommand {
  @override
  Future<void> run() {
    return context.run<void>(
      name: 'command',
      overrides: <Type, Generator>{FlutterCommand: () => this},
      body: runCommand,
    );
  }
}