Search in sources :

Example 1 with StringResource

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

the class MustacheTemplateEngine method generateOutputs.

@Override
public ResourceMap generateOutputs(ResourceMap inputs, Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    TemplateSettings settings = readSettings(inputs);
    Properties defaults = readDefaults(inputs);
    ResourceMap outputs = new ResourceMap();
    final Map<String, Object> flattenedParams = flattenParameters(parameters);
    applyDefaults(defaults, flattenedParams);
    DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory();
    mustacheFactory.setObjectHandler(new CheckMissingObjectHandler(mustacheFactory.getObjectHandler()));
    for (Entry<String, Resource> entry : inputs.entries()) {
        String inputPath = entry.getKey();
        Resource source = entry.getValue();
        StringWriter writer = new StringWriter();
        mustacheFactory.compile(new StringReader(inputPath), "mapping", settings.leftDelim, settings.rightDelim).execute(writer, flattenedParams);
        String outputPath = writer.toString();
        if (settings.ignore == null || !settings.ignore.matches(inputPath)) {
            Resource output;
            switch(source.getType()) {
                case Folder:
                    output = source;
                    break;
                case File:
                    if (settings.preprocessMatch.matches(inputPath)) {
                        // This file should be processed with the template engine
                        InputStreamReader reader = new InputStreamReader(source.getContent(), source.getTextEncoding());
                        StringWriter rendered = new StringWriter();
                        mustacheFactory.compile(reader, outputPath, settings.leftDelim, settings.rightDelim).execute(rendered, flattenedParams);
                        output = new StringResource(rendered.toString());
                    } else {
                        // This file should be directly copied
                        output = source;
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unknown resource type " + source.getType());
            }
            outputs.put(outputPath, output);
        }
    }
    return outputs;
}
Also used : InputStreamReader(java.io.InputStreamReader) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties) ResourceMap(org.bndtools.templating.ResourceMap) StringResource(org.bndtools.templating.StringResource) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

Example 2 with StringResource

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

the class StringTemplateEngine method getTemplateParameters.

@Override
public Map<String, String> getTemplateParameters(ResourceMap inputs, IProgressMonitor monitor) throws Exception {
    Map<String, String> params = new HashMap<>();
    // Initialise the engine
    TemplateSettings settings = readSettings(inputs);
    STGroup stg = new STGroup(settings.leftDelim, settings.rightDelim);
    // Assemble a mapping properties file of outputPath=sourcePath
    String mappingTemplate = loadMappingTemplate(inputs, settings, stg);
    extractAttrs(compile(stg, "_mapping", new StringResource(mappingTemplate)), params);
    // Iterate the entries
    Properties contentProps = new Properties();
    contentProps.load(new StringReader(mappingTemplate));
    @SuppressWarnings("unchecked") Enumeration<String> contentEnum = (Enumeration<String>) contentProps.propertyNames();
    while (contentEnum.hasMoreElements()) {
        String outputPath = contentEnum.nextElement().trim();
        String sourcePath = contentProps.getProperty(outputPath);
        Resource source = inputs.get(sourcePath);
        if (source == null)
            throw new RuntimeException(String.format("Internal error in template engine: could not find input resource '%s'", sourcePath));
        if (settings.ignore == null || !settings.ignore.matches(sourcePath)) {
            if (source.getType() == ResourceType.File) {
                if (settings.preprocessMatch.matches(sourcePath)) {
                    extractAttrs(compile(stg, sourcePath, source), params);
                }
            }
        }
    }
    return params;
}
Also used : Enumeration(java.util.Enumeration) STGroup(org.stringtemplate.v4.STGroup) HashMap(java.util.HashMap) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties) StringResource(org.bndtools.templating.StringResource) StringReader(java.io.StringReader)

Example 3 with StringResource

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

the class StringTemplateEngine method generateOutputs.

@Override
public ResourceMap generateOutputs(ResourceMap inputs, Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    TemplateSettings settings = readSettings(inputs);
    STGroup stg = new STGroup(settings.leftDelim, settings.rightDelim);
    // Assemble a mapping properties file of outputPath=sourcePath
    String mappingTemplate = loadMappingTemplate(inputs, settings, stg);
    String renderedMapping = render(compile(stg, "_mapping", new StringResource(mappingTemplate)), parameters);
    Properties contentProps = new Properties();
    contentProps.load(new StringReader(renderedMapping));
    // Iterate the content entries
    ResourceMap outputs = new ResourceMap();
    @SuppressWarnings("unchecked") Enumeration<String> contentEnum = (Enumeration<String>) contentProps.propertyNames();
    while (contentEnum.hasMoreElements()) {
        String outputName = contentEnum.nextElement().trim();
        String sourceName = contentProps.getProperty(outputName, "").trim();
        Resource source = inputs.get(sourceName);
        if (source == null)
            throw new RuntimeException(String.format("Internal error in template engine: could not find input resource '%s'", sourceName));
        Resource output;
        if (settings.ignore == null || !settings.ignore.matches(sourceName)) {
            if (source.getType() == ResourceType.Folder) {
                output = source;
            } else if (settings.preprocessMatch.matches(sourceName)) {
                // This file is a candidate for preprocessing with ST
                String rendered = render(compile(stg, sourceName, source), parameters);
                output = new StringResource(rendered);
            } else {
                // This file should be directly copied
                output = source;
            }
            outputs.put(outputName, output);
        }
    }
    return outputs;
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) StringResource(org.bndtools.templating.StringResource) Enumeration(java.util.Enumeration) STGroup(org.stringtemplate.v4.STGroup) StringReader(java.io.StringReader) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) Properties(java.util.Properties)

Example 4 with StringResource

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

the class MustacheTemplateEngineTest method testGetParamNames.

@Test
public void testGetParamNames() throws Exception {
    MustacheTemplateEngine engine = new MustacheTemplateEngine();
    ResourceMap input = new ResourceMap();
    input.put("readme.txt", new StringResource("Blah {{fish}} blah {{battleship}} blah {{antidisestablishmentarianism}}"));
    Map<String, String> params = engine.getTemplateParameters(input, new NullProgressMonitor());
    assertTrue(params.containsKey("fish"));
    assertTrue(params.containsKey("battleship"));
    assertTrue(params.containsKey("antidisestablishmentarianism"));
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StringResource(org.bndtools.templating.StringResource) Test(org.junit.Test)

Example 5 with StringResource

use of org.bndtools.templating.StringResource 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)

Aggregations

StringResource (org.bndtools.templating.StringResource)15 ResourceMap (org.bndtools.templating.ResourceMap)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 Test (org.junit.Test)9 HashMap (java.util.HashMap)7 List (java.util.List)7 FolderResource (org.bndtools.templating.FolderResource)4 StringReader (java.io.StringReader)3 Properties (java.util.Properties)3 Resource (org.bndtools.templating.Resource)3 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 Enumeration (java.util.Enumeration)2 BuiltInTemplate (org.bndtools.core.ui.wizards.shared.BuiltInTemplate)2 TemplateParamsWizardPage (org.bndtools.core.ui.wizards.shared.TemplateParamsWizardPage)2 STGroup (org.stringtemplate.v4.STGroup)2 BndEditModel (aQute.bnd.build.model.BndEditModel)1 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)1 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)1 Attrs (aQute.bnd.header.Attrs)1