precache.dart 7.04 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
// @dart = 2.8

7 8
import 'package:meta/meta.dart';

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

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

67 68 69 70 71
  final Cache _cache;
  final Logger _logger;
  final Platform _platform;
  final FeatureFlags _featureFlags;

72 73 74 75
  @override
  final String name = 'precache';

  @override
76 77 78
  final String description = "Populate the Flutter tool's cache of binary artifacts.\n\n"
    'If no explicit platform flags are provided, this command will download the artifacts '
    'for all currently enabled platforms';
79

80 81 82
  @override
  bool get shouldUpdateCache => false;

83 84 85 86 87 88 89 90 91
  /// 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',
    ]
  };

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  /// 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;
  }

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  @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();
  }

135
  @override
136
  Future<FlutterCommandResult> runCommand() async {
137
    // Re-lock the cache.
138
    if (_platform.environment['FLUTTER_ALREADY_LOCKED'] != 'true') {
139
      await _cache.lock();
140
    }
141 142 143
    if (boolArg('force')) {
      _cache.clearStampFiles();
    }
144

145 146
    final bool includeAllPlatforms = boolArg('all-platforms');
    if (includeAllPlatforms) {
147
      _cache.includeAllPlatforms = true;
148
    }
149
    if (boolArg('use-unsigned-mac-binaries')) {
150
      _cache.useUnsignedMacBinaries = true;
151
    }
152 153 154 155 156 157
    final Set<String> explicitlyEnabled = _explicitArtifactSelections();
    _cache.platformOverrideArtifacts = explicitlyEnabled;

    // If the user did not provide any artifact flags, then download
    // all artifacts that correspond to an enabled platform.
    final bool downloadDefaultArtifacts = explicitlyEnabled.isEmpty;
158
    final Map<String, String> umbrellaForArtifact = _umbrellaForArtifactMap();
159
    final Set<DevelopmentArtifact> requiredArtifacts = <DevelopmentArtifact>{};
160
    for (final DevelopmentArtifact artifact in DevelopmentArtifact.values) {
161
      if (artifact.feature != null && !_featureFlags.isEnabled(artifact.feature)) {
162 163
        continue;
      }
164

165
      final String argumentName = umbrellaForArtifact[artifact.name] ?? artifact.name;
166
      if (includeAllPlatforms || boolArg(argumentName) || downloadDefaultArtifacts) {
167 168
        requiredArtifacts.add(artifact);
      }
169
    }
170
    if (!await _cache.isUpToDate()) {
171
      await _cache.updateAll(requiredArtifacts);
172
    } else {
173
      _logger.printStatus('Already up-to-date.');
174
    }
175
    return FlutterCommandResult.success();
176 177
  }
}