writeAll method

Future<OkfWriteResult> writeAll(
  1. String rootPath,
  2. Map<String, String> files, {
  3. bool checkOnly = false,
})

Writes all files, keyed by bundle-relative path.

When checkOnly is true the filesystem is not changed; the returned paths identify files that would be written.

Implementation

Future<OkfWriteResult> writeAll(
  String rootPath,
  Map<String, String> files, {
  bool checkOnly = false,
}) async {
  final root = await _validatedRoot(
    rootPath,
    createIfMissing: !checkOnly,
  );
  final normalizedFiles = SplayTreeMap<String, String>();
  for (final entry in files.entries) {
    final relativePath = _normalizeRelativePath(entry.key);
    if (normalizedFiles.containsKey(relativePath)) {
      throw ArgumentError.value(
        entry.key,
        'files',
        'Multiple paths normalize to $relativePath',
      );
    }
    normalizedFiles[relativePath] = entry.value;
  }

  final changed = <String>[];
  for (final entry in normalizedFiles.entries) {
    final destination = File(
      p.joinAll(<String>[root.path, ...p.posix.split(entry.key)]),
    );
    await _rejectLinks(root, entry.key);

    final desiredBytes = utf8.encode(entry.value);
    final currentBytes =
        await destination.exists() ? await destination.readAsBytes() : null;
    if (_bytesEqual(currentBytes, desiredBytes)) {
      continue;
    }

    changed.add(entry.key);
    if (!checkOnly) {
      await destination.parent.create(recursive: true);
      await _rejectLinks(root, entry.key);
      await _atomicWrite(destination, desiredBytes);
    }
  }

  return OkfWriteResult(changed);
}