renderTemplate method

Future<void> renderTemplate({
  1. required String templateSubPath,
  2. required String targetPath,
  3. required Map<String, String> replacements,
})

Recursively copies a template folder/file to a target directory, interpolating placeholders.

Implementation

Future<void> renderTemplate({
  required String templateSubPath,
  required String targetPath,
  required Map<String, String> replacements,
}) async {
  final templatesRoot = await getTemplatePath();
  var sourcePath = p.join(templatesRoot, templateSubPath);

  // Check if sourcePath exists. If not, try adding '.tmpl' extension
  if (!FileSystemEntity.isFileSync(sourcePath) && !FileSystemEntity.isDirectorySync(sourcePath)) {
    if (FileSystemEntity.isFileSync('$sourcePath.tmpl')) {
      sourcePath = '$sourcePath.tmpl';
    }
  }

  if (FileSystemEntity.isFileSync(sourcePath)) {
    _renderFile(sourcePath, targetPath, replacements);
  } else if (FileSystemEntity.isDirectorySync(sourcePath)) {
    _renderDirectory(sourcePath, targetPath, replacements);
  } else {
    throw CliException('Template resource not found at: $sourcePath');
  }
}