Search in sources :

Example 1 with TemplateScans

use of com.devonfw.cobigen.impl.config.entity.io.TemplateScans in project cobigen by devonfw.

the class TemplatesConfigurationReader method loadTemplates.

/**
 * Loads all templates of the static configuration into the local representation
 *
 * @param trigger {@link Trigger} for which the templates should be loaded
 * @return the mapping of template names to the corresponding {@link Template}
 * @throws UnknownContextVariableException if the destination path contains an undefined context variable
 * @throws UnknownExpressionException if there is an unknown variable modifier
 * @throws InvalidConfigurationException if there are multiple templates with the same name
 */
public Map<String, Template> loadTemplates(Trigger trigger) throws UnknownExpressionException, UnknownContextVariableException, InvalidConfigurationException {
    Map<String, Template> templates = new HashMap<>();
    Templates templatesNode = this.configNode.getTemplates();
    if (templatesNode != null) {
        for (com.devonfw.cobigen.impl.config.entity.io.Template t : templatesNode.getTemplate()) {
            if (templates.get(t.getName()) != null) {
                throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Multiple template definitions found for ref='" + t.getName() + "'");
            }
            TemplatePath child = this.rootTemplateFolder.navigate(t.getTemplateFile());
            if ((child == null) || (child.isFolder())) {
                throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "no template file found for '" + t.getTemplateFile() + "'");
            }
            Template template = createTemplate((TemplateFile) child, t.getName(), t.getDestinationPath(), t.getMergeStrategy(), t.getTargetCharset(), null);
            templates.put(t.getName(), template);
        }
    }
    TemplateScans templateScans = this.configNode.getTemplateScans();
    if (templateScans != null) {
        List<TemplateScan> scans = templateScans.getTemplateScan();
        if (scans != null) {
            for (TemplateScan scan : scans) {
                scanTemplates(scan, templates, trigger);
            }
        }
    }
    // override existing templates with extension definitions
    Set<String> observedExtensionNames = Sets.newHashSet();
    if (templatesNode != null && templatesNode.getTemplateExtension() != null) {
        for (TemplateExtension ext : this.configNode.getTemplates().getTemplateExtension()) {
            // detection of duplicate templateExtensions
            if (observedExtensionNames.contains(ext.getRef())) {
                throw new InvalidConfigurationException("Two templateExtensions declared for ref='" + ext.getRef() + "'. Don't know what to do.");
            }
            observedExtensionNames.add(ext.getRef());
            // overriding properties if defined
            if (templates.containsKey(ext.getRef())) {
                Template template = templates.get(ext.getRef());
                if (ext.getDestinationPath() != null) {
                    template.setUnresolvedTargetPath(ext.getDestinationPath());
                }
                if (ext.getMergeStrategy() != null) {
                    template.setMergeStrategy(ext.getMergeStrategy());
                }
                if (ext.getTargetCharset() != null) {
                    template.setTargetCharset(ext.getTargetCharset());
                }
            } else {
                throw new InvalidConfigurationException("The templateExtension with ref='" + ext.getRef() + "' does not reference any template!");
            }
        }
    }
    return templates;
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) HashMap(java.util.HashMap) Templates(com.devonfw.cobigen.impl.config.entity.io.Templates) TemplateScan(com.devonfw.cobigen.impl.config.entity.io.TemplateScan) Template(com.devonfw.cobigen.impl.config.entity.Template) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) TemplateScans(com.devonfw.cobigen.impl.config.entity.io.TemplateScans) TemplateExtension(com.devonfw.cobigen.impl.config.entity.io.TemplateExtension)

Aggregations

InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1 TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)1 TemplateExtension (com.devonfw.cobigen.impl.config.entity.io.TemplateExtension)1 TemplateScan (com.devonfw.cobigen.impl.config.entity.io.TemplateScan)1 TemplateScans (com.devonfw.cobigen.impl.config.entity.io.TemplateScans)1 Templates (com.devonfw.cobigen.impl.config.entity.io.Templates)1 HashMap (java.util.HashMap)1