prepare method

Future<PreparedPackageRef> prepare(
  1. PackageRef ref
)
inherited

prepares given ref. Depending on the type of ref this can include

  • copying the package to a temporary directory
  • running pub get If you use analyze with this result then it will take care to clean up everything (e.g. removing temp directory)

Implementation

Future<PreparedPackageRef> prepare(PackageRef ref) async {
  final stdoutSession = StdoutSession();
  List<SourceItem> sources = [];
  String? packageRelativePath;
  if (ref.isDirectoryPath) {
    await stdoutSession.writeln('Preparing ${ref.ref}');
    String sourceDir = ref.ref;
    if (sourceDir.endsWith(p.separator)) {
      sourceDir =
          sourceDir.substring(0, sourceDir.length - p.separator.length);
    }

    sources.add(SourceItem(
      sourceDir: sourceDir,
      isInCache: false,
    ));
  } else if (ref.isPubRef) {
    await stdoutSession
        .writeln('Preparing ${ref.pubPackage!}:${ref.pubVersion!}');
    await stdoutSession.writeln('Downloading');
    String sourceDir = await PubInteraction.installPackageToCache(
      ref.pubPackage!,
      ref.pubVersion!,
      stdoutSession: stdoutSession,
    );
    sources.add(SourceItem(
      sourceDir: sourceDir,
      isInCache: true,
    ));
  } else {
    throw ArgumentError('Unknown package ref: ${ref.ref}');
  }
  // merge sources to not copy children of a parent separately
  // => remove all sources that have a parent in the list
  sources.removeWhere((sToRemove) =>
      sources.any((s) => p.isWithin(s.sourceDir, sToRemove.sourceDir)));
  final tempDir = await Directory.systemTemp.createTemp();
  await Future.forEach<SourceItem>(sources, (sourceItem) async {
    await stdoutSession
        .writeln('Copying sources from ${sourceItem.sourceDir}');
    await _copyPath(sourceItem.sourceDir,
        sourceItem.destinationPath(forPrefix: tempDir.path));
    if (!sourceItem.isInCache) {
      await stdoutSession.writeln(
          'Preparing package dependencies for local package ${sourceItem.sourceDir}');
      await PubInteraction.runPubGet(sourceItem.sourceDir,
          stdoutSession: stdoutSession);
      final sourcePackageConfig =
          File(_getPackageConfigPathForPackage(sourceItem.sourceDir));
      final targetPackageConfig =
          File(_getPackageConfigPathForPackage(tempDir.path))
            ..createSync(recursive: true);
      await sourcePackageConfig.copy(targetPackageConfig.path);
      await _adaptPackageConfigToAbsolutePaths(
        targetPackageConfigPath: targetPackageConfig.absolute.path,
        sourcePackageConfigPath: sourcePackageConfig.absolute.path,
      );
    } else {
      await stdoutSession.writeln('Cleaning up local copy of pub package');
      // Check if we have a pub package that bundles a pubspec_overrides.yaml (as this most probably destroys pub get)
      final pubspecOverrides = File(p.join(
          sourceItem.destinationPath(forPrefix: tempDir.path),
          'pubspec_overrides.yaml'));
      if (await pubspecOverrides.exists()) {
        await pubspecOverrides.delete();
        await stdoutSession.writeln('- Removed pubspec_overrides.yaml');
      }
    }
  });
  return PreparedPackageRef(
      packageRef: ref,
      tempDirectory: tempDir.path,
      packageRelativePath: packageRelativePath);
}