planPubspecUpdates function

List<PubspecUpdate> planPubspecUpdates(
  1. Project project
)

Plans the pubspec changes dpk get makes, without writing anything:

  • the root pubspec's workspace list, from the dpk.yaml workspace globs;
  • the catalog, for the root and every workspace package;
  • sorting, when sort_pubspec is on.

Returns one update per pubspec that dpk manages, changed or not.

Implementation

List<PubspecUpdate> planPubspecUpdates(Project project) {
  final workspace = project.workspace;
  final config = project.config;
  final updates = <PubspecUpdate>[];

  String read(String directory) {
    final file = File(p.join(directory, 'pubspec.yaml'));
    if (!file.existsSync()) {
      throw DpkException('No pubspec.yaml in $directory.');
    }
    return file.readAsStringSync();
  }

  String finish(String content) =>
      config.sortPubspec ? sortPubspec(content) : content;

  final rootBefore = read(workspace.root.path);
  var root = rootBefore;
  final globs = config.workspace;
  if (globs != null) {
    root = _withWorkspaceList(
      root,
      workspaceGlobMembers(workspace.root.path, globs),
    );
  }
  final catalog = config.catalog;
  if (catalog != null) {
    root = applyCatalog(
      root,
      catalog: catalog,
      role: PubspecRole.root,
      values: TemplateValues(
        packagePath: '.',
        packageName: workspace.root.name,
        packageVersion: catalog.version ?? workspace.root.version,
      ),
    );
  }
  updates.add(
    PubspecUpdate(
      path: p.join(workspace.root.path, 'pubspec.yaml'),
      before: rootBefore,
      after: finish(root),
    ),
  );

  for (final package in _members(workspace, globs)) {
    final before = read(package.path);
    var after = before;
    if (catalog != null) {
      after = applyCatalog(
        after,
        catalog: catalog,
        role: PubspecRole.package,
        values: TemplateValues(
          packagePath: package.relativePath,
          packageName: package.name,
          packageVersion: catalog.version ?? package.version,
        ),
      );
    }
    updates.add(
      PubspecUpdate(
        path: p.join(package.path, 'pubspec.yaml'),
        before: before,
        after: finish(after),
      ),
    );
  }
  return updates;
}