downloadCachedPackages method

Future<LockFile> downloadCachedPackages(
  1. SystemCache cache
)

Downloads all the cached packages selected by this version resolution.

If some already cached package differs from what is provided by the server (according to the content-hash) a warning is printed and the package is redownloaded.

Returns the LockFile representing the packages selected by this version resolution. Any resolved PackageIds will correspond to those in the cache (and thus to the one provided by the server).

If there is a mismatch between the previous content-hash from pubspec.lock and the new one a warning will be printed but the new one will be returned.

Implementation

Future<LockFile> downloadCachedPackages(SystemCache cache) async {
  final resolvedPackageIds = await progress('Downloading packages', () async {
    return await Future.wait(
      packages.map((id) async {
        if (id.source is CachedSource) {
          return await withDependencyType(
              _root.pubspec.dependencyType(id.name), () async {
            return (await cache.downloadPackage(
              id,
            ))
                .packageId;
          });
        }
        return id;
      }),
    );
  });
  // Invariant: the content-hashes in PUB_CACHE matches those provided by the
  // server.

  // Don't factor in overridden dependencies' SDK constraints, because we'll
  // accept those packages even if their constraints don't match.
  final nonOverrides = pubspecs.values
      .where((pubspec) => !_overriddenPackages.contains(pubspec.name))
      .toList();

  final sdkConstraints = <String, VersionConstraint>{};
  for (var pubspec in nonOverrides) {
    pubspec.sdkConstraints.forEach((identifier, constraint) {
      sdkConstraints[identifier] = constraint.effectiveConstraint
          .intersect(sdkConstraints[identifier] ?? VersionConstraint.any);
    });
  }
  return LockFile(
    resolvedPackageIds,
    sdkConstraints: {
      for (final MapEntry(:key, :value) in sdkConstraints.entries)
        key: SdkConstraint(value),
    },
    mainDependencies: MapKeySet(_root.dependencies),
    devDependencies: MapKeySet(_root.devDependencies),
    overriddenDependencies: _overriddenPackages,
  );
}