generateTemplate function

Future<void> generateTemplate({
  1. required ClayConfig config,
  2. required String referencePath,
  3. required String targetPath,
  4. void onIgnoredFile(
    1. String relativePath
    )?,
})

Copies referencePath to targetPath and applies config transforms.

Existing content under targetPath is removed before copying. Files matching ClayConfig.ignore are deleted from the target tree; empty parent directories are pruned. Remaining files receive path renames and content transforms.

Implementation

Future<void> generateTemplate({
  required ClayConfig config,
  required String referencePath,
  required String targetPath,
  void Function(String relativePath)? onIgnoredFile,
}) async {
  if (!Directory(referencePath).existsSync()) {
    throw GenerationException(
      'Reference directory not found ($referencePath).',
    );
  }

  assertSafeTargetPath(targetPath: targetPath);
  assertDistinctReferenceAndTargetPaths(
    referencePath: referencePath,
    targetPath: targetPath,
  );

  final normalizedReferencePath = p.normalize(p.absolute(referencePath));
  final normalizedTargetPath = p.normalize(p.absolute(targetPath));
  final referenceDir = Directory(normalizedReferencePath);
  final targetDir = Directory(normalizedTargetPath);
  if (targetDir.existsSync()) {
    await targetDir.delete(recursive: true);
  }

  await copyDirectory(
    source: referenceDir,
    destination: targetDir,
  );

  final targetEntities = targetDir
      .listSync(recursive: true, followLinks: false)
      .where((entity) => entity is File || entity is Link)
      .toList();

  await processCopiedTargetEntities(
    targetEntities: targetEntities,
    normalizedTargetPath: normalizedTargetPath,
    config: config,
    onIgnoredFile: onIgnoredFile,
  );
}