precache.dart 6.48 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
import '../base/common.dart';
6 7
import '../base/logger.dart';
import '../base/platform.dart';
8
import '../cache.dart';
9
import '../features.dart';
10
import '../runner/flutter_command.dart';
11

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

61
  final Cache _cache;
62 63 64 65
  final Logger _logger;
  final Platform _platform;
  final FeatureFlags _featureFlags;

66 67 68 69
  @override
  final String name = 'precache';

  @override
70 71 72
  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';
73

74 75 76
  @override
  final String category = FlutterCommandCategory.sdk;

77 78 79
  @override
  bool get shouldUpdateCache => false;

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
  /// 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)
95
          childArtifactName: entry.key,
96 97 98 99 100 101 102 103 104
    };
  }

  /// 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>{};
105
    bool explicitlySelected(String name) => boolArg(name) && argResults!.wasParsed(name);
106
    for (final DevelopmentArtifact artifact in DevelopmentArtifact.values) {
107
      final String? umbrellaName = umbrellaForArtifact[artifact.name];
108 109 110 111 112 113 114 115
      if (explicitlySelected(artifact.name) ||
          (umbrellaName != null && explicitlySelected(umbrellaName))) {
        selections.add(artifact.name);
      }
    }
    return selections;
  }

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

    return super.validateCommand();
  }

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

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

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

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