flutter_command.dart 25.1 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/terminal.dart';
17
import '../base/time.dart';
18
import '../base/user_messages.dart';
19
import '../base/utils.dart';
20
import '../build_info.dart';
21
import '../bundle.dart' as bundle;
22
import '../cache.dart';
23
import '../dart/package_map.dart';
24
import '../dart/pub.dart';
25
import '../device.dart';
26
import '../doctor.dart';
27
import '../globals.dart';
28
import '../project.dart';
29
import '../usage.dart';
30
import '../version.dart';
31 32
import 'flutter_command_runner.dart';

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

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

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

  final ExitStatus exitStatus;

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

58
  /// Optional epoch time when the command's non-interactive wait time is
59
  /// complete during the command's execution. Use to measure user perceivable
60 61
  /// latency without measuring user interaction time.
  ///
62
  /// [FlutterCommand] will automatically measure and report the command's
63
  /// complete time if not overridden.
64 65 66
  final DateTime endTimeOverride;
}

67 68 69 70
/// Common flutter command line options.
class FlutterOptions {
  static const String kExtraFrontEndOptions = 'extra-front-end-options';
  static const String kExtraGenSnapshotOptions = 'extra-gen-snapshot-options';
71
  static const String kEnableExperiment = 'enable-experiment';
72 73
  static const String kFileSystemRoot = 'filesystem-root';
  static const String kFileSystemScheme = 'filesystem-scheme';
74 75
}

76
abstract class FlutterCommand extends Command<void> {
77 78 79 80 81
  /// The currently executing command (or sub-command).
  ///
  /// Will be `null` until the top-most command has begun execution.
  static FlutterCommand get current => context[FlutterCommand];

82 83 84 85 86 87
  /// 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';

88 89
  @override
  ArgParser get argParser => _argParser;
90 91 92 93
  final ArgParser _argParser = ArgParser(
    allowTrailingOptions: false,
    usageLineLength: outputPreferences.wrapText ? outputPreferences.wrapColumn : null,
  );
94

95
  @override
96 97
  FlutterCommandRunner get runner => super.runner;

98 99
  bool _requiresPubspecYaml = false;

100 101 102 103
  /// Whether this command uses the 'target' option.
  bool _usesTargetOption = false;

  bool _usesPubOption = false;
104

105 106 107 108
  bool _usesPortOption = false;

  bool _usesIpv6Flag = false;

109 110
  bool get shouldRunPub => _usesPubOption && argResults['pub'];

111 112
  bool get shouldUpdateCache => true;

113 114
  BuildMode _defaultBuildMode;

115 116 117 118
  void requiresPubspecYaml() {
    _requiresPubspecYaml = true;
  }

119 120 121
  void usesTargetOption() {
    argParser.addOption('target',
      abbr: 't',
122
      defaultsTo: bundle.defaultMainPath,
123
      help: 'The main entry-point file of the application, as run on the device.\n'
124
            'If the --target option is omitted, but a file name is provided on '
125 126
            'the command line, then that is used instead.',
      valueHelp: 'path');
127 128 129
    _usesTargetOption = true;
  }

130 131 132 133 134 135
  String get targetFile {
    if (argResults.wasParsed('target'))
      return argResults['target'];
    else if (argResults.rest.isNotEmpty)
      return argResults.rest.first;
    else
136
      return bundle.defaultMainPath;
137 138
  }

139 140 141
  void usesPubOption() {
    argParser.addFlag('pub',
      defaultsTo: true,
142
      help: 'Whether to run "flutter packages get" before executing this command.');
143 144 145
    _usesPubOption = true;
  }

146 147 148 149
  /// 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.
150
  void usesFilesystemOptions({ @required bool hide }) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    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',
      );
  }

171 172 173 174
  /// 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'
175
              'Specifying port 0 (the default) will find a random free port.',
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    );
    _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;

208 209
  void usesBuildNumberOption() {
    argParser.addOption('build-number',
210 211
        help: 'An identifier used as an internal version number.\n'
              'Each build must have a unique identifier to differentiate it from previous builds.\n'
212 213 214
              '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\'',
215
    );
216 217 218 219 220 221 222 223 224 225 226
  }

  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');
  }

227
  void usesIsolateFilterOption({ @required bool hide }) {
228 229 230 231 232 233 234
    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.');
  }

235
  void addBuildModeFlags({ bool defaultToRelease = true, bool verboseHelp = false }) {
236
    defaultBuildMode = defaultToRelease ? BuildMode.release : BuildMode.debug;
237

238 239
    argParser.addFlag('debug',
      negatable: false,
240
      help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
241 242
    argParser.addFlag('profile',
      negatable: false,
243
      help: 'Build a version of your app specialized for performance profiling.');
244
    argParser.addFlag('release',
245
      negatable: false,
246
      help: 'Build a release version of your app${defaultToRelease ? ' (default mode)' : ''}.');
247 248 249
    argParser.addFlag('dynamic',
      hide: !verboseHelp,
      negatable: false,
250
      help: 'Enable dynamic code. Only allowed with --release or --profile.');
251 252
  }

253
  void addDynamicModeFlags({ bool verboseHelp = false }) {
254 255
    argParser.addOption('compilation-trace-file',
        defaultsTo: 'compilation.txt',
256
        hide: !verboseHelp,
257 258 259
        help: 'Filename of Dart compilation trace file. This file will be produced\n'
              'by \'flutter run --dynamic --profile --train\' and consumed by subsequent\n'
              '--dynamic builds such as \'flutter build apk --dynamic\' to precompile\n'
260
              'some code by the offline compiler.',
261 262 263
    );
  }

264
  void usesFuchsiaOptions({ bool hide = false }) {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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',
    );
  }

281 282
  set defaultBuildMode(BuildMode value) {
    _defaultBuildMode = value;
283 284
  }

285
  BuildMode getBuildMode() {
286 287
    final List<bool> modeFlags = <bool>[argResults['debug'], argResults['profile'], argResults['release']];
    if (modeFlags.where((bool flag) => flag).length > 1)
288
      throw UsageException('Only one of --debug, --profile, or --release can be specified.', null);
289 290 291
    final bool dynamicFlag = argParser.options.containsKey('dynamic')
        ? argResults['dynamic']
        : false;
292

293 294
    if (argResults['debug']) {
      if (dynamicFlag)
295
        throw ToolExit('Error: --dynamic requires --release or --profile.');
296
      return BuildMode.debug;
297
    }
298
    if (argResults['profile'])
299
      return dynamicFlag ? BuildMode.dynamicProfile : BuildMode.profile;
300
    if (argResults['release'])
301
      return dynamicFlag ? BuildMode.dynamicRelease : BuildMode.release;
302 303 304 305 306 307 308 309

    if (_defaultBuildMode == BuildMode.debug && dynamicFlag)
      throw ToolExit('Error: --dynamic requires --release or --profile.');
    if (_defaultBuildMode == BuildMode.release && dynamicFlag)
      return BuildMode.dynamicRelease;
    if (_defaultBuildMode == BuildMode.profile && dynamicFlag)
      return BuildMode.dynamicProfile;

310
    return _defaultBuildMode;
311 312
  }

313 314 315 316 317
  void usesFlavorOption() {
    argParser.addOption(
      'flavor',
      help: 'Build a custom app flavor as defined by platform-specific build setup.\n'
        'Supports the use of product flavors in Android Gradle scripts.\n'
318
        'Supports the use of custom Xcode schemes.',
319 320 321 322
    );
  }

  BuildInfo getBuildInfo() {
323 324 325 326 327 328
    TargetPlatform targetPlatform;
    if (argParser.options.containsKey('target-platform') &&
        argResults['target-platform'] != 'default') {
      targetPlatform = getTargetPlatformForName(argResults['target-platform']);
    }

329 330 331 332
    final bool trackWidgetCreation = argParser.options.containsKey('track-widget-creation')
        ? argResults['track-widget-creation']
        : false;

333 334 335
    final String buildNumber = argParser.options.containsKey('build-number') && argResults['build-number'] != null
        ? argResults['build-number']
        : null;
336

337
    String extraFrontEndOptions =
338 339 340 341 342 343 344 345
        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) {
346
          extraFrontEndOptions += ',' + flag;
347
        } else {
348
          extraFrontEndOptions = flag;
349 350 351 352
        }
      }
    }

353
    return BuildInfo(getBuildMode(),
354 355 356
      argParser.options.containsKey('flavor')
        ? argResults['flavor']
        : null,
357
      trackWidgetCreation: trackWidgetCreation,
358 359
      compilationTraceFilePath: argParser.options.containsKey('compilation-trace-file')
          ? argResults['compilation-trace-file']
360
          : null,
361
      extraFrontEndOptions: extraFrontEndOptions,
362
      extraGenSnapshotOptions: argParser.options.containsKey(FlutterOptions.kExtraGenSnapshotOptions)
363
          ? argResults[FlutterOptions.kExtraGenSnapshotOptions]
364
          : null,
365 366
      buildSharedLibrary: argParser.options.containsKey('build-shared-library')
        ? argResults['build-shared-library']
367
        : false,
368 369 370 371 372
      targetPlatform: targetPlatform,
      fileSystemRoots: argParser.options.containsKey(FlutterOptions.kFileSystemRoot)
          ? argResults[FlutterOptions.kFileSystemRoot] : null,
      fileSystemScheme: argParser.options.containsKey(FlutterOptions.kFileSystemScheme)
          ? argResults[FlutterOptions.kFileSystemScheme] : null,
373 374 375 376
      buildNumber: buildNumber,
      buildName: argParser.options.containsKey('build-name')
          ? argResults['build-name']
          : null,
377
    );
378 379
  }

380
  void setupApplicationPackages() {
381
    applicationPackages ??= ApplicationPackageStore();
382 383
  }

384
  /// The path to send to Google Analytics. Return null here to disable
385
  /// tracking of the command.
386 387 388 389 390 391 392 393 394 395
  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;
    }
  }
396

397 398 399 400 401
  /// Whether this feature should not be usable on stable branches.
  ///
  /// Defaults to false, meaning it is usable.
  bool get isExperimental => false;

402 403 404
  /// Additional usage values to be sent with the usage ping.
  Future<Map<String, String>> get usageValues async => const <String, String>{};

405 406 407 408 409 410
  /// 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.
411
  @override
412
  Future<void> run() {
413
    final DateTime startTime = systemClock.now();
Devon Carew's avatar
Devon Carew committed
414

415
    return context.run<void>(
416 417 418 419 420
      name: 'command',
      overrides: <Type, Generator>{FlutterCommand: () => this},
      body: () async {
        if (flutterUsage.isFirstRun)
          flutterUsage.printWelcome();
421
        final String commandPath = await usagePath;
422 423
        FlutterCommandResult commandResult;
        try {
424
          commandResult = await verifyThenRunCommand(commandPath);
425 426 427 428
        } on ToolExit {
          commandResult = const FlutterCommandResult(ExitStatus.fail);
          rethrow;
        } finally {
429
          final DateTime endTime = systemClock.now();
430
          printTrace(userMessages.flutterElapsedTime(name, getElapsedAsMilliseconds(endTime.difference(startTime))));
431 432
          printTrace('"flutter $name" took ${getElapsedAsMilliseconds(endTime.difference(startTime))}.');
          if (commandPath != null) {
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
            final List<String> labels = <String>[];
            if (commandResult?.exitStatus != null)
              labels.add(getEnumName(commandResult.exitStatus));
            if (commandResult?.timingLabelParts?.isNotEmpty ?? false)
              labels.addAll(commandResult.timingLabelParts);

            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,
            );
          }
        }
      },
    );
Devon Carew's avatar
Devon Carew committed
456 457
  }

458 459 460 461 462 463 464 465
  /// 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
466
  Future<FlutterCommandResult> verifyThenRunCommand(String commandPath) async {
467
    await validateCommand();
468

469 470
    // 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.
471
    if (shouldUpdateCache) {
472
      await cache.updateAll(await requiredArtifacts);
473
    }
474

475
    if (shouldRunPub) {
476
      await pubGet(context: PubContext.getVerifyContext(name));
477
      final FlutterProject project = await FlutterProject.current();
478
      await project.ensureReadyForPlatformSpecificTooling(checkProjects: true);
479
    }
480

481
    setupApplicationPackages();
Devon Carew's avatar
Devon Carew committed
482

483 484 485 486 487
    if (commandPath != null) {
      final Map<String, String> additionalUsageValues = await usageValues;
      flutterUsage.sendCommand(commandPath, parameters: additionalUsageValues);
    }

488
    return await runCommand();
489 490
  }

491 492 493 494
  /// The set of development artifacts required for this command.
  ///
  /// Defaults to [DevelopmentArtifact.universal],
  /// [DevelopmentArtifact.android], and [DevelopmentArtifact.iOS].
495
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
496 497 498 499 500
    DevelopmentArtifact.universal,
    DevelopmentArtifact.iOS,
    DevelopmentArtifact.android,
  };

501
  /// Subclasses must implement this to execute the command.
502
  /// Optionally provide a [FlutterCommandResult] to send more details about the
503 504
  /// execution for analytics.
  Future<FlutterCommandResult> runCommand();
505

506
  /// Find and return all target [Device]s based upon currently connected
507
  /// devices and criteria entered by the user on the command line.
508
  /// If no device can be found that meets specified criteria,
509
  /// then print an error message and return null.
510
  Future<List<Device>> findAllTargetDevices() async {
511
    if (!doctor.canLaunchAnything) {
512
      printError(userMessages.flutterNoDevelopmentDevice);
513 514 515
      return null;
    }

516
    List<Device> devices = await deviceManager.getDevices().toList();
517 518

    if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) {
519
      printStatus(userMessages.flutterNoMatchingDevice(deviceManager.specifiedDeviceId));
520
      return null;
521
    } else if (devices.isEmpty && deviceManager.hasSpecifiedAllDevices) {
522
      printStatus(userMessages.flutterNoDevicesFound);
523
      return null;
524 525 526 527 528 529 530 531
    } else if (devices.isEmpty) {
      printNoConnectedDevices();
      return null;
    }

    devices = devices.where((Device device) => device.isSupported()).toList();

    if (devices.isEmpty) {
532
      printStatus(userMessages.flutterNoSupportedDevices);
533
      return null;
534
    } else if (devices.length > 1 && !deviceManager.hasSpecifiedAllDevices) {
535
      if (deviceManager.hasSpecifiedDeviceId) {
536
        printStatus(userMessages.flutterFoundSpecifiedDevices(devices.length, deviceManager.specifiedDeviceId));
537
      } else {
538
        printStatus(userMessages.flutterSpecifyDeviceWithAllOption);
539
        devices = await deviceManager.getAllConnectedDevices().toList();
540 541
      }
      printStatus('');
542
      await Device.printDevices(devices);
543 544
      return null;
    }
545 546 547 548 549 550
    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,
551
  /// then print an error message and return null.
552 553 554 555 556
  Future<Device> findTargetDevice() async {
    List<Device> deviceList = await findAllTargetDevices();
    if (deviceList == null)
      return null;
    if (deviceList.length > 1) {
557
      printStatus(userMessages.flutterSpecifyDevice);
558 559 560 561 562 563
      deviceList = await deviceManager.getAllConnectedDevices().toList();
      printStatus('');
      await Device.printDevices(deviceList);
      return null;
    }
    return deviceList.single;
564 565
  }

566
  void printNoConnectedDevices() {
567
    printStatus(userMessages.flutterNoConnectedDevices);
568 569
  }

570 571
  @protected
  @mustCallSuper
572
  Future<void> validateCommand() async {
573 574 575 576 577 578
    // If we're on a stable branch, then don't allow the usage of
    // "experimental" features.
    if (isExperimental && FlutterVersion.instance.isStable) {
      throwToolExit('Experimental feature $name is not supported on stable branches');
    }

579
    if (_requiresPubspecYaml && !PackageMap.isUsingCustomPackagesPath) {
580
      // Don't expect a pubspec.yaml file if the user passed in an explicit .packages file path.
581
      if (!fs.isFileSync('pubspec.yaml')) {
582
        throw ToolExit(userMessages.flutterNoPubspec);
583
      }
584

585
      if (fs.isFileSync('flutter.yaml')) {
586
        throw ToolExit(userMessages.flutterMergeYamlFiles);
587
      }
588 589

      // Validate the current package map only if we will not be running "pub get" later.
590
      if (parent?.name != 'packages' && !(_usesPubOption && argResults['pub'])) {
591
        final String error = PackageMap(PackageMap.globalPackagesPath).checkValid();
592
        if (error != null)
593
          throw ToolExit(error);
594
      }
595
    }
596 597

    if (_usesTargetOption) {
598
      final String targetPath = targetFile;
599
      if (!fs.isFileSync(targetPath))
600
        throw ToolExit(userMessages.flutterTargetFileMissing(targetPath));
601 602
    }
  }
603

604 605
  ApplicationPackageStore applicationPackages;
}
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
/// 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;
624 625 626
      final DevelopmentArtifact developmentArtifact = _artifactFromTargetPlatform(targetPlatform);
      if (developmentArtifact != null) {
        artifacts.add(developmentArtifact);
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
      }
    }
    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,
    };
649 650 651
    final DevelopmentArtifact developmentArtifact = _artifactFromTargetPlatform(targetPlatform);
    if (developmentArtifact != null) {
      artifacts.add(developmentArtifact);
652 653 654 655 656
    }
    return artifacts;
  }
}

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
// 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:
      return DevelopmentArtifact.android;
    case TargetPlatform.web:
      return DevelopmentArtifact.web;
    case TargetPlatform.ios:
      return DevelopmentArtifact.iOS;
    case TargetPlatform.darwin_x64:
      if (!FlutterVersion.instance.isStable) {
        return DevelopmentArtifact.macOS;
      }
      return null;
    case TargetPlatform.windows_x64:
      if (!FlutterVersion.instance.isStable) {
        return DevelopmentArtifact.windows;
      }
      return null;
    case TargetPlatform.linux_x64:
      if (!FlutterVersion.instance.isStable) {
        return DevelopmentArtifact.linux;
      }
      return null;
    case TargetPlatform.fuchsia:
    case TargetPlatform.tester:
      // No artifacts currently supported.
      return null;
  }
  return null;
}

693 694 695 696 697 698 699 700 701 702 703
/// 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,
    );
  }
}