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

5 6
import 'package:meta/meta.dart';

7
import '../base/common.dart';
8 9
import '../base/logger.dart';
import '../base/platform.dart';
10
import '../cache.dart';
11
import '../features.dart';
12
import '../runner/flutter_command.dart';
13

14 15
/// The flutter precache command allows downloading of cache artifacts without
/// the use of device/artifact autodetection.
16
class PrecacheCommand extends FlutterCommand {
17 18 19 20 21 22 23 24 25
  PrecacheCommand({
    bool verboseHelp = false,
    @required Cache cache,
    @required Platform platform,
    @required Logger logger,
    @required FeatureFlags featureFlags,
  }) : _cache = cache,
       _platform = platform,
       _logger = logger,
26
       _featureFlags = featureFlags {
27
    argParser.addFlag('all-platforms', abbr: 'a', negatable: false,
28
        help: 'Precache artifacts for all host platforms.');
29
    argParser.addFlag('force', abbr: 'f', negatable: false,
30
        help: 'Force re-downloading of artifacts.');
31
    argParser.addFlag('android', negatable: true, defaultsTo: true,
32 33 34 35 36 37 38 39 40 41 42
        help: 'Precache artifacts for Android development.',
        hide: verboseHelp);
    argParser.addFlag('android_gen_snapshot', negatable: true, defaultsTo: true,
        help: 'Precache gen_snapshot for Android development.',
        hide: !verboseHelp);
    argParser.addFlag('android_maven', negatable: true, defaultsTo: true,
        help: 'Precache Gradle dependencies for Android development.',
        hide: !verboseHelp);
    argParser.addFlag('android_internal_build', negatable: true, defaultsTo: false,
        help: 'Precache dependencies for internal Android development.',
        hide: !verboseHelp);
43
    argParser.addFlag('ios', negatable: true, defaultsTo: true,
44
        help: 'Precache artifacts for iOS development.');
45
    argParser.addFlag('web', negatable: true, defaultsTo: false,
46
        help: 'Precache artifacts for web development.');
47
    argParser.addFlag('linux', negatable: true, defaultsTo: false,
48
        help: 'Precache artifacts for Linux desktop development.');
49
    argParser.addFlag('windows', negatable: true, defaultsTo: false,
50
        help: 'Precache artifacts for Windows desktop development.');
51
    argParser.addFlag('macos', negatable: true, defaultsTo: false,
52
        help: 'Precache artifacts for macOS desktop development.');
53
    argParser.addFlag('fuchsia', negatable: true, defaultsTo: false,
54
        help: 'Precache artifacts for Fuchsia development.');
55
    argParser.addFlag('universal', negatable: true, defaultsTo: true,
56
        help: 'Precache artifacts required for any development platform.');
57 58
    argParser.addFlag('flutter_runner', negatable: true, defaultsTo: false,
        help: 'Precache the flutter runner artifacts.', hide: true);
59 60
    argParser.addFlag('use-unsigned-mac-binaries', negatable: true, defaultsTo: false,
        help: 'Precache the unsigned mac binaries when available.', hide: true);
61 62
  }

63 64 65 66 67
  final Cache _cache;
  final Logger _logger;
  final Platform _platform;
  final FeatureFlags _featureFlags;

68 69 70 71
  @override
  final String name = 'precache';

  @override
72
  final String description = "Populate the Flutter tool's cache of binary artifacts.";
73

74 75 76
  @override
  bool get shouldUpdateCache => false;

77 78 79 80 81 82 83 84 85
  /// Some flags are umbrella names that expand to include multiple artifacts.
  static const Map<String, List<String>> _expandedArtifacts = <String, List<String>>{
    'android': <String>[
      'android_gen_snapshot',
      'android_maven',
      'android_internal_build',
    ]
  };

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  /// Returns a reverse mapping of _expandedArtifacts, from child artifact name
  /// to umbrella name.
  Map<String, String> _umbrellaForArtifactMap() {
    return <String, String>{
      for (final MapEntry<String, List<String>> entry in _expandedArtifacts.entries)
        for (final String childArtifactName in entry.value)
          childArtifactName: entry.key
    };
  }

  /// Returns the name of all artifacts that were explicitly chosen via flags.
  ///
  /// If an umbrella is chosen, its children will be included as well.
  Set<String> _explicitArtifactSelections() {
    final Map<String, String> umbrellaForArtifact = _umbrellaForArtifactMap();
    final Set<String> selections = <String>{};
    bool explicitlySelected(String name) => boolArg(name) && argResults.wasParsed(name);
    for (final DevelopmentArtifact artifact in DevelopmentArtifact.values) {
      final String umbrellaName = umbrellaForArtifact[artifact.name];
      if (explicitlySelected(artifact.name) ||
          (umbrellaName != null && explicitlySelected(umbrellaName))) {
        selections.add(artifact.name);
      }
    }
    return selections;
  }

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  @override
  Future<void> validateCommand() {
    _expandedArtifacts.forEach((String umbrellaName, List<String> childArtifactNames) {
      if (!argResults.arguments.contains('--no-$umbrellaName')) {
        return;
      }
      for (final String childArtifactName in childArtifactNames) {
        if (argResults.arguments.contains('--$childArtifactName')) {
          throwToolExit('--$childArtifactName requires --$umbrellaName');
        }
      }
    });

    return super.validateCommand();
  }

129
  @override
130
  Future<FlutterCommandResult> runCommand() async {
131
    // Re-lock the cache.
132
    if (_platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
133 134
      await Cache.lock();
    }
135 136 137
    if (boolArg('force')) {
      _cache.clearStampFiles();
    }
138

139 140
    final bool includeAllPlatforms = boolArg('all-platforms');
    if (includeAllPlatforms) {
141
      _cache.includeAllPlatforms = true;
142
    }
143
    if (boolArg('use-unsigned-mac-binaries')) {
144
      _cache.useUnsignedMacBinaries = true;
145
    }
146
    _cache.platformOverrideArtifacts = _explicitArtifactSelections();
147
    final Map<String, String> umbrellaForArtifact = _umbrellaForArtifactMap();
148
    final Set<DevelopmentArtifact> requiredArtifacts = <DevelopmentArtifact>{};
149
    for (final DevelopmentArtifact artifact in DevelopmentArtifact.values) {
150
      if (artifact.feature != null && !_featureFlags.isEnabled(artifact.feature)) {
151 152
        continue;
      }
153

154 155
      final String argumentName = umbrellaForArtifact[artifact.name] ?? artifact.name;
      if (includeAllPlatforms || boolArg(argumentName)) {
156 157
        requiredArtifacts.add(artifact);
      }
158
    }
159
    if (!await _cache.isUpToDate()) {
160
      await _cache.updateAll(requiredArtifacts);
161
    } else {
162
      _logger.printStatus('Already up-to-date.');
163
    }
164
    return FlutterCommandResult.success();
165 166
  }
}