use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class EnrouteProjectTemplate method generateOutputs.
@Override
public ResourceMap generateOutputs(Map<String, List<Object>> params, IProgressMonitor monitor) throws Exception {
String projectName = param(PROP_PROJECT_NAME, params);
String pkg = param(PROP_BASE_PACKAGE_NAME, params);
String srcDir = param(PROP_SRC_DIR, params);
String testSrcDir = param(PROP_TEST_SRC_DIR, params);
ResourceMap resources = new ResourceMap();
resources.put("bnd.bnd", generateBndFile(projectName, pkg));
generateSources(resources, projectName, pkg, srcDir, testSrcDir);
return resources;
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class BndRunFileWizard method getTemplateContents.
private InputStream getTemplateContents(String fileName) throws Exception {
// Load properties
Map<String, List<Object>> params = new HashMap<>();
params.put(PROP_FILE_NAME, Collections.<Object>singletonList(fileName));
params.put(PROP_FILE_BASE_NAME, Collections.<Object>singletonList(baseName(fileName)));
IPath containerPath = mainPage.getContainerFullPath();
if (containerPath != null) {
IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(containerPath);
if (container != null) {
String projectName = container.getProject().getName();
params.put(PROP_PROJECT_NAME, Collections.<Object>singletonList(projectName));
}
}
Map<String, String> editedParams = paramsPage.getValues();
for (Entry<String, String> editedParam : editedParams.entrySet()) {
params.put(editedParam.getKey(), Collections.<Object>singletonList(editedParam.getValue()));
}
// Run the template processor
Template template = templatePage.getTemplate();
ResourceMap outputs;
outputs = template.generateOutputs(params);
Resource output = outputs.get(fileName);
if (output == null) {
throw new IllegalArgumentException(String.format("Template error: file '%s' not found in outputs. Available names: %s", fileName, outputs.getPaths()));
}
// Pull the generated content
return output.getContent();
}
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 ReposTemplateLoaderTest method testReferTemplateDefinitions.
public void testReferTemplateDefinitions() throws Exception {
List<Template> templates = loader.findTemplates("test3", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("name", Collections.<Object>singletonList("Homer Simpson"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(1, outputs.size());
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
Entry<String, Resource> entry;
entry = iter.next();
assertEquals("example.html", entry.getKey());
assertEquals("My name is <i>Homer Simpson</i>!", IO.collect(entry.getValue().getContent()));
}
use of org.bndtools.templating.ResourceMap in project bndtools by bndtools.
the class NewBndProjectWizard method generateProjectContent.
@Override
protected void generateProjectContent(IProject project, IProgressMonitor monitor, Map<String, String> params) throws IOException {
Map<String, List<Object>> templateParams = new HashMap<>();
for (Entry<String, String> param : params.entrySet()) {
templateParams.put(param.getKey(), Collections.<Object>singletonList(param.getValue()));
}
Template template = templatePage.getTemplate();
try {
ResourceMap outputs;
if (template != null) {
outputs = template.generateOutputs(templateParams);
} else {
// empty
outputs = new ResourceMap();
}
SubMonitor progress = SubMonitor.convert(monitor, outputs.size() * 3);
for (Entry<String, Resource> outputEntry : outputs.entries()) {
String path = outputEntry.getKey();
Resource resource = outputEntry.getValue();
// Strip leading slashes from path
while (path.startsWith("/")) path = path.substring(1);
switch(resource.getType()) {
case Folder:
if (!path.isEmpty()) {
IFolder folder = project.getFolder(path);
FileUtils.mkdirs(folder, progress.newChild(1, SubMonitor.SUPPRESS_ALL_LABELS));
}
break;
case File:
IFile file = project.getFile(path);
FileUtils.mkdirs(file.getParent(), progress.newChild(1, SubMonitor.SUPPRESS_ALL_LABELS));
try (InputStream in = resource.getContent()) {
if (file.exists())
file.setContents(in, 0, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
else
file.create(in, 0, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
file.setCharset(resource.getTextEncoding(), progress.newChild(1));
}
break;
default:
throw new IllegalArgumentException("Unknown resource type " + resource.getType());
}
}
} catch (Exception e) {
String message = MessageFormat.format("Error generating project contents from template \"{0}\": {1}", template != null ? template.getName() : "<null>", e.getMessage());
throw new IOException(message);
}
}
Aggregations