Search in sources :

Example 1 with TemplatePath

use of com.devonfw.cobigen.impl.config.entity.TemplatePath 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)

Example 2 with TemplatePath

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

the class TemplatesConfigurationReader method scanTemplates.

/**
 * Recursively scans the templates specified by the given {@link TemplateScan} and adds them to the given
 * <code>templates</code> {@link Map}.
 *
 * @param templateFolder the {@link TemplateFolder} pointing to the current directory to scan.
 * @param currentPath the current path relative to the top-level directory where we started the scan.
 * @param scan is the {@link TemplateScan} configuration.
 * @param templates is the {@link Map} where to add the templates.
 * @param trigger the templates are from
 * @param observedTemplateNames observed template name during template scan. Needed for conflict detection
 */
private void scanTemplates(TemplateFolder templateFolder, String currentPath, TemplateScan scan, Map<String, Template> templates, Trigger trigger, HashSet<String> observedTemplateNames) {
    String currentPathWithSlash = currentPath;
    if (!currentPathWithSlash.isEmpty()) {
        currentPathWithSlash = currentPathWithSlash + "/";
    }
    for (TemplatePath child : templateFolder.getChildren()) {
        if (child.isFolder()) {
            scanTemplates((TemplateFolder) child, currentPathWithSlash + child.getFileName(), scan, templates, trigger, observedTemplateNames);
        } else {
            String templateFileName = child.getFileName();
            if (StringUtils.isEmpty(currentPath) && templateFileName.equals("templates.xml")) {
                continue;
            }
            String templateNameWithoutExtension = stripTemplateFileending(templateFileName);
            TextTemplateEngine templateEngine = TemplateEngineRegistry.getEngine(getTemplateEngine());
            if (!StringUtils.isEmpty(templateEngine.getTemplateFileEnding()) && templateFileName.endsWith(templateEngine.getTemplateFileEnding())) {
                templateNameWithoutExtension = templateFileName.substring(0, templateFileName.length() - templateEngine.getTemplateFileEnding().length());
            }
            String templateName = (scan.getTemplateNamePrefix() != null ? scan.getTemplateNamePrefix() : "") + templateNameWithoutExtension;
            if (observedTemplateNames.contains(templateName)) {
                throw new InvalidConfigurationException("TemplateScan has detected two files with the same file name (" + child + ") and thus with the same " + "template name. Continuing would result in an indeterministic behavior.\n" + "For now, multiple files with the same name are not supported to be automatically " + "configured with templateScans.");
            }
            observedTemplateNames.add(templateName);
            if (!templates.containsKey(templateName)) {
                String destinationPath = "";
                if (!StringUtils.isEmpty(scan.getDestinationPath())) {
                    destinationPath = scan.getDestinationPath() + "/";
                }
                destinationPath += currentPathWithSlash + templateNameWithoutExtension;
                String mergeStratgey = scan.getMergeStrategy();
                Template template = createTemplate((TemplateFile) child, templateName, destinationPath, mergeStratgey, scan.getTargetCharset(), scan.getTemplatePath());
                templates.put(templateName, template);
                if (this.templateScanTemplates.get(scan.getName()) != null) {
                    this.templateScanTemplates.get(scan.getName()).add(templateName);
                }
            }
        }
    }
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) TextTemplateEngine(com.devonfw.cobigen.api.extension.TextTemplateEngine) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) Template(com.devonfw.cobigen.impl.config.entity.Template)

Example 3 with TemplatePath

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

the class TemplatesConfigurationReader method scanTemplates.

/**
 * Scans the templates specified by the given {@link TemplateScan} and adds them to the given <code>templates</code>
 * {@link Map}.
 *
 * @param scan is the {@link TemplateScan} configuration.
 * @param templates is the {@link Map} where to add the templates.
 * @param trigger the templates are from
 */
private void scanTemplates(TemplateScan scan, Map<String, Template> templates, Trigger trigger) {
    String templatePath = scan.getTemplatePath();
    TemplatePath templateFolder = this.rootTemplateFolder.navigate(templatePath);
    if ((templateFolder == null) || templateFolder.isFile()) {
        throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "The templatePath '" + templatePath + "' of templateScan with name '" + scan.getName() + "' does not describe a directory.");
    }
    if (scan.getName() != null) {
        if (this.templateScanTemplates.containsKey(scan.getName())) {
            throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Two templateScan nodes have been defined with the same @name by mistake.");
        } else {
            this.templateScanTemplates.put(scan.getName(), new ArrayList<String>());
        }
    }
    scanTemplates((TemplateFolder) templateFolder, "", scan, templates, trigger, Sets.<String>newHashSet());
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 4 with TemplatePath

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

the class TemplatePathTest method verifyChildPath.

private static <T extends TemplatePath> T verifyChildPath(TemplateFolder folder, Path path, String childName, Class<T> type) {
    TemplatePath child = folder.getChild(childName);
    T childTyped = verifyPath(child, path.resolve(childName), type);
    assertThat(child.getParent()).isSameAs(folder);
    assertThat(child.getRoot()).isSameAs(folder.getRoot()).isNotNull();
    String parentPath;
    if (folder.getParent() == null) {
        parentPath = "";
    } else {
        parentPath = folder.toString() + "/";
    }
    assertThat(child.toString()).isEqualTo(parentPath + childName);
    return childTyped;
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath)

Example 5 with TemplatePath

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

the class TemplatePathTest method verifyChildren.

private static void verifyChildren(TemplateFolder folder, TemplatePath... children) {
    assertThat(folder.getChildren()).containsOnly(children);
    if (children.length == 0) {
        return;
    }
    List<TemplateFolder> childFolders = new ArrayList<>();
    List<TemplateFile> childFiles = new ArrayList<>();
    for (TemplatePath child : children) {
        if (child.isFolder()) {
            assertThat(child.isFile()).isFalse();
            childFolders.add((TemplateFolder) child);
        } else {
            assertThat(child.isFile()).isTrue();
            childFiles.add((TemplateFile) child);
        }
    }
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) ArrayList(java.util.ArrayList) TemplateFolder(com.devonfw.cobigen.impl.config.entity.TemplateFolder) TemplateFile(com.devonfw.cobigen.impl.config.entity.TemplateFile)

Aggregations

TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)5 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)3 Template (com.devonfw.cobigen.impl.config.entity.Template)2 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)1 TemplateFile (com.devonfw.cobigen.impl.config.entity.TemplateFile)1 TemplateFolder (com.devonfw.cobigen.impl.config.entity.TemplateFolder)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1