AFGeneratedFile.fromTemplate constructor

AFGeneratedFile.fromTemplate({
  1. required AFCommandContext context,
  2. required List<String> projectPath,
  3. required AFSourceTemplate template,
  4. required AFGeneratedFileAction action,
})

Implementation

factory AFGeneratedFile.fromTemplate({
  required AFCommandContext context,
  required List<String> projectPath,
  required AFSourceTemplate template,
  required AFGeneratedFileAction action
}) {

  // see if an override exists, if it does, use it.
  var effective = template.toBuffer(context);
  var effectivePath = projectPath;
  if(template is AFFileSourceTemplate) {
    final originalPath = template.templatePath;

    // this is the command-line override, which switches the template path dynamically.
    final overridePath = context.findOverrideTemplate(originalPath);
    final hasOverride = overridePath != originalPath;
    if(context.isExportTemplates) {
      effectivePath = AFProjectPaths.generateFolderFor(overridePath);
    }

    if(AFProjectPaths.generateFileExists(overridePath)) {
      action = AFGeneratedFileAction.skip;
    }


    // if there is an override for that path on the filesystem, use it.
    if(AFProjectPaths.generateFileExists(overridePath)) {
      effective = AFCodeBuffer.fromGeneratePath(overridePath);
    } else {
      // if the path changed, and the override path is not on the filesystem, see if it
      // is one of our predefined paths.
      if(hasOverride) {
        final overrideTemplate = context.findEmbeddedTemplateFile(overridePath);
        if(overrideTemplate == null) {
          throw AFException("The override ${joinAll(overridePath)} was not found on the file system, or in the AFTemplateRegistry, for ${joinAll(originalPath)}");
        }
        effective = overrideTemplate.toBuffer(context);
      }
    }

  }

  return AFGeneratedFile(
    projectPath: effectivePath,
    action: action,
    buffer: effective,
  );
}