Search in sources :

Example 11 with Template

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()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ProgressMonitorReporter(org.bndtools.utils.progress.ProgressMonitorReporter) HashMap(java.util.HashMap) Resource(org.bndtools.templating.Resource) Template(org.bndtools.templating.Template) ResourceMap(org.bndtools.templating.ResourceMap) Entry(java.util.Map.Entry) List(java.util.List)

Example 12 with Template

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);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) InputStream(java.io.InputStream) SubMonitor(org.eclipse.core.runtime.SubMonitor) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) IOException(java.io.IOException) IOException(java.io.IOException) BuiltInTemplate(org.bndtools.core.ui.wizards.shared.BuiltInTemplate) Template(org.bndtools.templating.Template) ResourceMap(org.bndtools.templating.ResourceMap) List(java.util.List) IFolder(org.eclipse.core.resources.IFolder)

Example 13 with Template

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")));
}
Also used : BuiltInTemplate(org.bndtools.core.ui.wizards.shared.BuiltInTemplate) StringResource(org.bndtools.templating.StringResource) TemplateParamsWizardPage(org.bndtools.core.ui.wizards.shared.TemplateParamsWizardPage) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) BuiltInTemplate(org.bndtools.core.ui.wizards.shared.BuiltInTemplate) Template(org.bndtools.templating.Template)

Example 14 with Template

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);
        }
    });
}
Also used : TemplateSelectionWizardPage(org.bndtools.core.ui.wizards.shared.TemplateSelectionWizardPage) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) Template(org.bndtools.templating.Template)

Example 15 with 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);
}
Also used : Parameters(aQute.bnd.header.Parameters) Attrs(aQute.bnd.header.Attrs) Template(org.bndtools.templating.Template)

Aggregations

Template (org.bndtools.templating.Template)20 List (java.util.List)8 HashMap (java.util.HashMap)7 Resource (org.bndtools.templating.Resource)6 ResourceMap (org.bndtools.templating.ResourceMap)6 ProgressMonitorReporter (org.bndtools.utils.progress.ProgressMonitorReporter)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Entry (java.util.Map.Entry)4 Category (org.bndtools.templating.Category)4 BuiltInTemplate (org.bndtools.core.ui.wizards.shared.BuiltInTemplate)3 StringResource (org.bndtools.templating.StringResource)3 Attrs (aQute.bnd.header.Attrs)2 Parameters (aQute.bnd.header.Parameters)2 PromiseCollectors.toPromise (aQute.lib.promise.PromiseCollectors.toPromise)2 Reporter (aQute.service.reporter.Reporter)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)2 PropertyChangeListener (java.beans.PropertyChangeListener)2 DigestInputStream (java.security.DigestInputStream)2 Collections (java.util.Collections)2 Objects (java.util.Objects)2