fingerprintSources function

LibrarySurface fingerprintSources(
  1. List<File> files, {
  2. required String rootDir,
})

Fingerprints files, rendering each relative to rootDir.

Public so the guard's reactivity can be tested against a handful of purpose-written files rather than only against the real model, where a test could assert that the fingerprint is stable but never that it moves for the right reasons.

Implementation

LibrarySurface fingerprintSources(List<File> files, {required String rootDir}) {
  final sorted = [...files]..sort((a, b) => a.path.compareTo(b.path));

  final buffer = StringBuffer();
  var declarations = 0;
  for (final file in sorted) {
    // Relative, so the hash does not depend on where the workspace is checked
    // out — nor, for the tests, on the temp directory of the day.
    final relative = p.relative(file.path, from: rootDir).replaceAll(r'\', '/');
    final rendered = _renderUnit(file);
    declarations += rendered.declarationCount;
    if (rendered.text.isEmpty) continue;
    buffer
      ..writeln('// $relative')
      ..write(rendered.text);
  }

  return LibrarySurface(
    _sha256(buffer.toString()),
    sorted.length,
    declarations,
  );
}