writePubspecUpdates function

void writePubspecUpdates(
  1. Iterable<PubspecUpdate> updates
)

Writes every changed update. Each file is written to a temporary file first and then renamed, so an error never leaves a half-written pubspec.

Implementation

void writePubspecUpdates(Iterable<PubspecUpdate> updates) {
  final changed = updates.where((update) => update.changed).toList();
  final temporary = <String, File>{};
  try {
    for (final update in changed) {
      temporary[update.path] = File('${update.path}.dpk-tmp')
        ..writeAsStringSync(update.after);
    }
    for (final MapEntry(key: path, value: file) in temporary.entries) {
      file.renameSync(path);
    }
  } finally {
    for (final file in temporary.values) {
      if (file.existsSync()) {
        file.deleteSync();
      }
    }
  }
}