Search in sources :

Example 1 with TemplateFolder

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

the class TemplatesConfigurationReader method createTemplate.

/**
 * @param templateFile the {@link TemplateFile}.
 * @param templateName the {@link Template#getName() template name} (ID).
 * @param unresolvedTemplatePath the {@link Template#getUnresolvedTemplatePath() unresolved template path}.
 * @param mergeStratgey the {@link Template#getMergeStrategy() merge strategy}.
 * @param outputCharset the {@link Template#getTargetCharset() target charset}.
 * @param scanSourcePath {@link TemplateScan#getTemplatePath() root path} of the {@link TemplateScan}
 * @return the new template instance.
 */
private Template createTemplate(TemplateFile templateFile, String templateName, String unresolvedTemplatePath, String mergeStratgey, String outputCharset, String scanSourcePath) {
    String unresolvedDestinationPath = unresolvedTemplatePath;
    TemplateFolder templateFolder = templateFile.getParent();
    String relocate = templateFolder.getVariables().get(PROPERTY_RELOCATE);
    if (relocate != null) {
        if (scanSourcePath != null) {
            // The relative template path has to be specifically parsed to string and back to a path so
            // the templateFile and scanSourcePath are using the same file system. More info can be found
            // at https://github.com/devonfw/cobigen/issues/715
            String templateFilePath = templateFile.getRootRelativePath().toString();
            Path destinationPath = Paths.get(scanSourcePath).relativize(Paths.get(templateFilePath));
            unresolvedDestinationPath = relocate.replace(VARIABLE_CWD, destinationPath.toString().replace("\\", "/"));
        }
    }
    return new Template(templateFile, templateName, stripTemplateFileending(unresolvedDestinationPath), unresolvedTemplatePath, mergeStratgey, outputCharset);
}
Also used : Path(java.nio.file.Path) TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) TemplateFolder(com.devonfw.cobigen.impl.config.entity.TemplateFolder) Template(com.devonfw.cobigen.impl.config.entity.Template)

Example 2 with TemplateFolder

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

the class TemplatePathTest method testEmptyFolder.

/**
 * Test {@link TemplateFolder#create(Path)} for empty folder.
 *
 * @throws IOException test fails
 */
@Test
public void testEmptyFolder() throws IOException {
    String filename = "empty";
    Path path = Paths.get(TEST_FILES_ROOT_PATH + filename);
    if (!Files.exists(path)) {
        Files.createDirectory(path);
    }
    TemplateFolder folder = TemplateFolder.create(path);
    verifyRootFolder(folder, path);
    assertThat(folder.getVariables().asMap()).isEmpty();
    verifyEmptyFolder(folder);
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) Path(java.nio.file.Path) TemplateFolder(com.devonfw.cobigen.impl.config.entity.TemplateFolder) Test(org.junit.Test) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)

Example 3 with TemplateFolder

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

Example 4 with TemplateFolder

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

the class TemplatePathTest method testFolderTree.

/**
 * Test {@link TemplateFolder#create(Path)} for folder tree with property overloading.
 */
@Test
public void testFolderTree() {
    String filename = "tree";
    String emptyFile = "empty-file";
    Path path = Paths.get(TEST_FILES_ROOT_PATH + filename);
    TemplateFolder folder = TemplateFolder.create(path);
    verifyRootFolder(folder, path);
    assertThat(folder).isNotNull();
    assertThat(folder.getVariables().asMap()).isEmpty();
    TemplateFolder having = verifyChildFolder(folder, path, "having");
    TemplateFolder or = verifyChildFolder(folder, path, "or");
    verifyChildren(folder, having, or);
    assertThat(having.getVariables().asMap()).isEmpty();
    TemplateFolder property = verifyChildFolder(having, having.getPath(), "property");
    verifyChildren(having, property);
    assertThat(property.getChild("cobigen.properties")).isNull();
    assertThat(property.getChild("COBIGEN.PROPERTIES")).isNull();
    assertThat(property.getVariables().asMap()).hasSize(2).containsEntry("foo", "foo").containsEntry("bar", "bar");
    TemplateFolder override = verifyChildFolder(property, property.getPath(), "override");
    verifyChildren(property, override);
    assertThat(override.getVariables().asMap()).hasSize(2).containsEntry("foo", "bar").containsEntry("bar", "bar");
    TemplateFile emptyFileInOverride = verifyChildFile(override, override.getPath(), emptyFile);
    verifyChildren(override, emptyFileInOverride);
    TemplateFolder not = verifyChildFolder(or, or.getPath(), "not");
    assertThat(not.getVariables().asMap()).isEmpty();
    TemplateFile emptyFileInNot = verifyChildFile(not, not.getPath(), emptyFile);
    verifyChildren(not, emptyFileInNot);
}
Also used : TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) Path(java.nio.file.Path) TemplateFolder(com.devonfw.cobigen.impl.config.entity.TemplateFolder) TemplateFile(com.devonfw.cobigen.impl.config.entity.TemplateFile) Test(org.junit.Test) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)

Aggregations

TemplateFolder (com.devonfw.cobigen.impl.config.entity.TemplateFolder)4 TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)4 Path (java.nio.file.Path)3 TemplateFile (com.devonfw.cobigen.impl.config.entity.TemplateFile)2 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)2 Test (org.junit.Test)2 Template (com.devonfw.cobigen.impl.config.entity.Template)1 ArrayList (java.util.ArrayList)1