Search in sources :

Example 11 with Template

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

the class TemplatesConfigurationReaderTest method testIncrementComposition_combiningAllPossibleReferences.

/**
 * Tests the correct resolution of references of templates / templateScans / increments.
 */
@Test
public void testIncrementComposition_combiningAllPossibleReferences() {
    // given
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_increment_composition");
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    Map<String, Increment> increments = target.loadIncrements(templates, trigger);
    // validation
    assertThat(templates).containsOnlyKeys("templateDecl", "prefix_scanned", "scanned", "prefix_scanned2", "scanned2");
    assertThat(increments).containsOnlyKeys("0", "1", "2");
    assertThat(increments.values()).hasSize(3);
    assertThat(increments.get("0").getTemplates()).extracting("name").containsOnly("templateDecl");
    assertThat(increments.get("1").getTemplates()).extracting("name").containsOnly("templateDecl", "prefix_scanned", "scanned", "scanned2");
    assertThat(increments.get("2").getTemplates()).extracting("name").containsOnly("templateDecl", "prefix_scanned", "scanned", "prefix_scanned2");
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Increment(com.devonfw.cobigen.impl.config.entity.Increment) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 12 with Template

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

the class TemplatesConfigurationReaderTest method testRelocate_overlappingExplicitTemplateDestinationPathAndRelocatedScanPath.

/**
 * Tests an overlapping configuration according to the destination paths of a relocated folder within a template scan
 * and a explicitly defined destination path of a template configuration XML node. The destination path of a template
 * configuration should not be affected by any relocation of any template scan.
 */
@Test
public void testRelocate_overlappingExplicitTemplateDestinationPathAndRelocatedScanPath() {
    // given
    String templateScanDestinationPath = "src/main/java/";
    String templatesConfigurationRoot = testFileRootPath + "valid_relocate_template_vs_scan/";
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_template_vs_scan/");
    Trigger trigger = new Trigger("id", "type", "valid_relocate", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    assertThat(templates).hasSize(2);
    // validation
    String staticRelocationPrefix = "../api/";
    String scanRelTemplatePath = "$_rootpackage_$/$_component_$/common/api/";
    Template template = verifyScannedTemplate(templates, "$_EntityName_$.java", scanRelTemplatePath, templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
    template = templates.get("ExplicitlyDefined");
    assertThat(template).isNotNull();
    assertThat(template.getRelativeTemplatePath()).isEqualTo("OuterTemplate.java");
    assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "OuterTemplate.java");
    // the destination path has designed to match a relocated path during the scan by intention
    String destinationPath = "src/main/java/$_rootpackage_$/$_component_$/common/api/ExplicitlyDefined.java";
    assertThat(template.getUnresolvedTemplatePath()).isEqualTo(destinationPath);
    assertThat(template.getUnresolvedTargetPath()).isEqualTo(destinationPath);
    assertThat(template.getVariables().asMap()).hasSize(0);
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 13 with Template

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

the class TemplatesConfigurationReaderTest method testInvalidIncrementRefOutsideCurrentFile.

/**
 * Tests the correct detection of invalid external increment reference.
 *
 * @throws InvalidConfigurationException expected
 */
@Test(expected = InvalidConfigurationException.class)
public void testInvalidIncrementRefOutsideCurrentFile() {
    Path config = Paths.get(new File(testFileRootPath).toURI());
    new ContextConfigurationReader(config);
    // given
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_invalid_external_incrementref", configurationHolder);
    Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    target.loadIncrements(templates, trigger);
}
Also used : Path(java.nio.file.Path) ContextConfigurationReader(com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) Template(com.devonfw.cobigen.impl.config.entity.Template) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 14 with Template

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

the class TemplatesConfigurationReader method loadExternalTemplate.

/**
 * Tries to load an external template, returning the reference template
 *
 * @param ref The reference to the template
 * @return the referenced template
 */
private Template loadExternalTemplate(TemplateRef ref) {
    String[] split = splitExternalRef(ref.getRef());
    String refTrigger = split[0];
    String refTemplate = split[1];
    com.devonfw.cobigen.impl.config.TemplatesConfiguration externalTemplatesConfiguration = loadExternalConfig(refTrigger);
    Template template = externalTemplatesConfiguration.getTemplate(refTemplate);
    if (template == null) {
        throw new InvalidConfigurationException("No Template found for ref=" + ref.getRef());
    }
    return template;
}
Also used : Template(com.devonfw.cobigen.impl.config.entity.Template) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 15 with Template

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

Aggregations

Template (com.devonfw.cobigen.impl.config.entity.Template)26 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)19 File (java.io.File)17 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)16 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)16 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)16 Test (org.junit.Test)16 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)15 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)5 Increment (com.devonfw.cobigen.impl.config.entity.Increment)5 Path (java.nio.file.Path)5 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)4 TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)4 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)3 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)3 HashMap (java.util.HashMap)3 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)2 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)2 TemplateFolder (com.devonfw.cobigen.impl.config.entity.TemplateFolder)2 TemplateExtension (com.devonfw.cobigen.impl.config.entity.io.TemplateExtension)2