use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class ReposTemplateLoaderTest method testExtendUnprocessedPatternAndIgnore.
public void testExtendUnprocessedPatternAndIgnore() throws Exception {
List<Template> templates = loader.findTemplates("test4", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(1, outputs.size());
Entry<String, Resource> entry;
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
entry = iter.next();
assertEquals("pic.xxx", entry.getKey());
// Check the digest of the pic to ensure it didn't get damaged by the templating engine
DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
IO.drain(digestStream);
byte[] digest = digestStream.getMessageDigest().digest();
assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
}
use of org.bndtools.templating.ResourceMap 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;
}
use of org.bndtools.templating.ResourceMap 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;
}
use of org.bndtools.templating.ResourceMap 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"));
}
use of org.bndtools.templating.ResourceMap 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()));
}
Aggregations