use of org.bndtools.templating.Template 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.Template 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);
}
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class NewBndProjectWizard method init.
@Override
public void init(IWorkbench workbench, IStructuredSelection currentSelection) {
super.init(workbench, currentSelection);
BuiltInTemplate baseTemplate = new BuiltInTemplate(EMPTY_TEMPLATE_NAME, DEFAULT_TEMPLATE_ENGINE);
// $NON-NLS-1$
baseTemplate.addInputResource(Project.BNDFILE, new StringResource(""));
// $NON-NLS-1$
baseTemplate.setHelpPath("docs/empty_project.xml");
templatePage = new ProjectTemplateSelectionWizardPage("projectTemplateSelection", "project", baseTemplate);
templatePage.setTitle("Select Project Template");
paramsPage = new TemplateParamsWizardPage(ProjectTemplateParam.valueStrings());
templatePage.addPropertyChangeListener(TemplateSelectionWizardPage.PROP_TEMPLATE, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Template template = templatePage.getTemplate();
pageOne.setTemplate(template);
paramsPage.setTemplate(template);
}
});
setDefaultPageImageDescriptor(ImageDescriptor.createFromURL(Plugin.getDefault().getBundle().getEntry("icons/bndtools-wizban.png")));
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class WorkspaceSetupWizard method init.
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
this.workbench = workbench;
setupPage = new WorkspaceSetupWizardPage();
updateSetupPageForExistingProjects();
templatePage = new TemplateSelectionWizardPage("workspaceTemplateSelection", "workspace", null);
templatePage.setTitle("Select Workspace Template");
previewPage = new WorkspacePreviewPage();
previewPage.setTargetDir(setupPage.getLocation().toFile());
setupPage.addPropertyChangeListener(WorkspaceLocationPart.PROP_LOCATION, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
previewPage.setTargetDir(setupPage.getLocation().toFile());
}
});
templatePage.addPropertyChangeListener(TemplateSelectionWizardPage.PROP_TEMPLATE, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
Template template = templatePage.getTemplate();
previewPage.setTemplate(template);
}
});
}
use of org.bndtools.templating.Template in project bndtools by bndtools.
the class PlainGitWorkspaceTemplateLoader method findTemplates.
@Override
public Promise<List<Template>> findTemplates(String type, Reporter reporter) {
if (!TEMPLATE_TYPE.equals(type)) {
return promiseFactory.resolved(Collections.emptyList());
}
Parameters gitRepos = new GitRepoPreferences().getGitRepos();
List<Template> templates = gitRepos.entrySet().stream().map(entry -> {
String cloneUrl = GitRepoPreferences.removeDuplicateMarker(entry.getKey());
Attrs attribs = entry.getValue();
try {
GitCloneTemplateParams params = new GitCloneTemplateParams();
params.cloneUrl = cloneUrl;
params.category = "Git Repositories";
params.name = attribs.get("name");
params.iconUri = iconUrl.toURI();
params.branch = attribs.get("branch");
return (Template) new GitCloneTemplate(params);
} catch (Exception e) {
reporter.exception(e, "Error loading template from Git clone URL %s", cloneUrl);
return null;
}
}).filter(Objects::nonNull).collect(Collectors.toList());
return promiseFactory.resolved(templates);
}
Aggregations