prepare method

Future<OkfBundlePreparation> prepare(
  1. String rootPath,
  2. OkfBundleChangeSet changes
)

Builds and validates a complete candidate without writing any file.

Implementation

Future<OkfBundlePreparation> prepare(
  String rootPath,
  OkfBundleChangeSet changes,
) async {
  final sourceBefore = await _snapshot(rootPath);
  final loaded = await const OkfBundleLoader().inspect(sourceBefore.rootPath);
  final overlay = OkfBundleChangeOverlay.of(
    loaded.bundle,
    changes,
    date: clock(),
  );
  final sourceAfter = await _snapshot(
    loaded.rootPath,
    retainedTextPaths: loaded.documents.keys.toSet(),
  );
  if (!sourceBefore.sameContent(sourceAfter)) {
    throw const OkfStalePreparedChangeException();
  }

  final changedFiles = Map<String, String>.unmodifiable(overlay.files);
  final candidate = OkfPreparedCandidate._fromSnapshot(
    loaded: loaded,
    source: sourceAfter,
    changedFiles: changedFiles,
  );
  final validation = loaded.validateCandidate(
    candidate.toBundle(),
    replacedPaths: changedFiles.keys,
  );
  if (!validation.isConformant) {
    return OkfPreparationRefused(validation: validation);
  }
  await _validateCandidatePaths(
    candidate,
    rootPath: loaded.rootPath,
    source: sourceAfter,
    changedPaths: changedFiles.keys,
  );

  return OkfPreparationReady._(
    OkfPreparedChange._(
      candidate: candidate,
      validation: validation,
      plan: _PreparedCommitPlan(
        rootPath: loaded.rootPath,
        source: sourceAfter.withoutText(),
        changedFiles: changedFiles,
      ),
    ),
  );
}