rewriteExternalPathDependencies method

void rewriteExternalPathDependencies(
  1. String workspaceDir
)

Relative path: dependencies that point outside the project (e.g. a shared package next to it) would break in the copy; make them absolute.

Implementation

void rewriteExternalPathDependencies(String workspaceDir) {
  final file = File(p.join(workspaceDir, 'pubspec.yaml'));
  if (!file.existsSync()) return;
  final content = file.readAsStringSync();
  final yaml = loadYaml(content);
  if (yaml is! YamlMap) return;

  final editor = YamlEditor(content);
  var changed = false;
  for (final section in [
    'dependencies',
    'dev_dependencies',
    'dependency_overrides'
  ]) {
    final deps = yaml[section];
    if (deps is! YamlMap) continue;
    for (final entry in deps.entries) {
      final spec = entry.value;
      if (spec is! YamlMap || spec['path'] is! String) continue;
      final path = spec['path'] as String;
      if (p.isAbsolute(path)) continue;
      final resolved = p.normalize(p.join(projectDir, path));
      if (p.isWithin(projectDir, resolved)) continue;
      editor.update([section, entry.key, 'path'], resolved);
      changed = true;
    }
  }
  if (changed) file.writeAsStringSync(editor.toString());
}