Search in sources :

Example 1 with FolderResource

use of org.bndtools.templating.FolderResource in project bndtools by bndtools.

the class MustacheTemplateEngineTest method testNoProcessDefaultPattern.

@Test
public void testNoProcessDefaultPattern() throws Exception {
    MustacheTemplateEngine engine = new MustacheTemplateEngine();
    ResourceMap input = new ResourceMap();
    input.put("{{srcDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
    input.put("{{srcDir}}/{{packageDir}}/package-info.jpg", new StringResource("package {{packageName}};"));
    Map<String, List<Object>> params = new HashMap<>();
    params.put("srcDir", Collections.<Object>singletonList("src"));
    params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
    params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
    ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
    assertEquals(4, output.size());
    assertEquals("package org.example.foo;", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
    assertEquals("package {{packageName}};", IO.collect(output.get("src/org/example/foo/package-info.jpg").getContent()));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) FolderResource(org.bndtools.templating.FolderResource) List(java.util.List) Test(org.junit.Test)

Example 2 with FolderResource

use of org.bndtools.templating.FolderResource in project bndtools by bndtools.

the class MustacheTemplateEngineTest method testNoProcessExtendedPattern.

@Test
public void testNoProcessExtendedPattern() throws Exception {
    MustacheTemplateEngine engine = new MustacheTemplateEngine();
    ResourceMap input = new ResourceMap();
    input.put("_template.properties", new StringResource("process.before=!*.java"));
    input.put("{{srcDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
    Map<String, List<Object>> params = new HashMap<>();
    params.put("srcDir", Collections.<Object>singletonList("src"));
    params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
    params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
    ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
    assertEquals(3, output.size());
    assertEquals("package {{packageName}};", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) FolderResource(org.bndtools.templating.FolderResource) List(java.util.List) Test(org.junit.Test)

Example 3 with FolderResource

use of org.bndtools.templating.FolderResource in project bndtools by bndtools.

the class GitCloneTemplate method recurse.

private static void recurse(String prefix, File file, FileFilter filter, ResourceMap resourceMap) {
    if (file.isDirectory()) {
        String path = prefix + file.getName() + "/";
        resourceMap.put(path, new FolderResource());
        File[] children = file.listFiles(filter);
        for (File child : children) {
            recurse(path, child, filter, resourceMap);
        }
    } else {
        String path = prefix + file.getName();
        // TODO: WTF is the encoding?
        resourceMap.put(path, new FileResource(file, "UTF-8"));
    }
}
Also used : FileResource(org.bndtools.templating.FileResource) FolderResource(org.bndtools.templating.FolderResource) File(java.io.File)

Example 4 with FolderResource

use of org.bndtools.templating.FolderResource in project bndtools by bndtools.

the class CapabilityBasedTemplate method getInputSources.

private synchronized ResourceMap getInputSources() throws IOException {
    File bundleFile = fetchBundle();
    _inputResources = new ResourceMap();
    try (JarInputStream in = new JarInputStream(IO.stream(bundleFile))) {
        JarEntry jarEntry = in.getNextJarEntry();
        while (jarEntry != null) {
            String entryPath = jarEntry.getName().trim();
            if (entryPath.startsWith(dir)) {
                String relativePath = entryPath.substring(dir.length());
                if (!relativePath.isEmpty()) {
                    // skip the root folder
                    Resource resource;
                    if (relativePath.endsWith("/")) {
                        // strip the trailing slash
                        relativePath.substring(0, relativePath.length());
                        resource = new FolderResource();
                    } else {
                        // cannot use IO.collect() because it closes the whole JarInputStream
                        resource = BytesResource.loadFrom(in);
                    }
                    _inputResources.put(relativePath, resource);
                }
            }
            jarEntry = in.getNextJarEntry();
        }
    }
    return _inputResources;
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) JarInputStream(java.util.jar.JarInputStream) BytesResource(org.bndtools.templating.BytesResource) Resource(org.bndtools.templating.Resource) FolderResource(org.bndtools.templating.FolderResource) FolderResource(org.bndtools.templating.FolderResource) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 5 with FolderResource

use of org.bndtools.templating.FolderResource in project bndtools by bndtools.

the class MustacheTemplateEngineTest method testIgnore.

@Test
public void testIgnore() throws Exception {
    MustacheTemplateEngine engine = new MustacheTemplateEngine();
    ResourceMap input = new ResourceMap();
    input.put("_template.properties", new StringResource("ignore=*/donotcopy.*"));
    input.put("{{srcDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/", new FolderResource());
    input.put("{{srcDir}}/{{packageDir}}/package-info.java", new StringResource("package {{packageName}};"));
    input.put("{{srcDir}}/{{packageDir}}/donotcopy.txt", new StringResource(""));
    Map<String, List<Object>> params = new HashMap<>();
    params.put("srcDir", Collections.<Object>singletonList("src"));
    params.put("packageDir", Collections.<Object>singletonList("org/example/foo"));
    params.put("packageName", Collections.<Object>singletonList("org.example.foo"));
    ResourceMap output = engine.generateOutputs(input, params, new NullProgressMonitor());
    assertEquals(3, output.size());
    assertEquals("package org.example.foo;", IO.collect(output.get("src/org/example/foo/package-info.java").getContent()));
    assertNull(output.get("src/org/example/foo/donotcopy.txt"));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) HashMap(java.util.HashMap) FolderResource(org.bndtools.templating.FolderResource) List(java.util.List) Test(org.junit.Test)

Aggregations

FolderResource (org.bndtools.templating.FolderResource)6 ResourceMap (org.bndtools.templating.ResourceMap)5 HashMap (java.util.HashMap)4 List (java.util.List)4 StringResource (org.bndtools.templating.StringResource)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 Test (org.junit.Test)4 File (java.io.File)2 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 JarInputStream (java.util.jar.JarInputStream)1 BytesResource (org.bndtools.templating.BytesResource)1 FileResource (org.bndtools.templating.FileResource)1 Resource (org.bndtools.templating.Resource)1