Search in sources :

Example 1 with Template

use of org.bndtools.templating.Template 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();
}
Also used : ResourceMap(org.bndtools.templating.ResourceMap) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) StringResource(org.bndtools.templating.StringResource) Resource(org.bndtools.templating.Resource) IResource(org.eclipse.core.resources.IResource) List(java.util.List) IResource(org.eclipse.core.resources.IResource) BuiltInTemplate(org.bndtools.core.ui.wizards.shared.BuiltInTemplate) Template(org.bndtools.templating.Template)

Example 2 with Template

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

the class RepoTemplateContentProvider method getFirstTemplate.

public Template getFirstTemplate() {
    Template result = null;
    if (roots != null && roots.length > 0) {
        if (roots[0] instanceof Category) {
            Category cat = (Category) roots[0];
            Iterator<Template> templateIter = cat.getTemplates().iterator();
            if (templateIter.hasNext())
                result = templateIter.next();
        }
    }
    return result;
}
Also used : Category(org.bndtools.templating.Category) Template(org.bndtools.templating.Template)

Example 3 with Template

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

the class LatestTemplateFilter method filter.

@Override
public Object[] filter(Viewer viewer, Object parent, Object[] elements) {
    Object[] result;
    if (parent instanceof Category) {
        // Preserves the order of names, as they were
        Map<String, Template> selected = new LinkedHashMap<>();
        // already sorted by the content provider.
        for (Object element : elements) {
            Template template = (Template) element;
            Template existing = selected.get(template.getName());
            if (existing == null)
                // no selected template for this name -> add
                selected.put(template.getName(), template);
            else if (template.getVersion().compareTo(existing.getVersion()) > 0)
                // existing selected template for this name is lower -> replace
                selected.put(template.getName(), template);
        }
        result = selected.values().toArray();
    } else {
        result = elements;
    }
    return result;
}
Also used : Category(org.bndtools.templating.Category) Template(org.bndtools.templating.Template) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Template

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

the class RepoTemplateLabelProvider method update.

@Override
public void update(ViewerCell cell) {
    Object element = cell.getElement();
    if (element instanceof Category) {
        Category cat = (Category) element;
        cell.setText(cat.getName());
        cell.setImage(IMG_FOLDER);
    } else if (element instanceof Template) {
        Template template = (Template) element;
        // Name
        StyledString label = new StyledString(template.getName(), BoldStyler.INSTANCE_DEFAULT);
        // Version, with all segments except qualifier in bold
        Version version = template.getVersion();
        if (version != null) {
            label.append(" ");
            label.append(String.format("%d.%d.%d", version.getMajor(), version.getMinor(), version.getMicro()), BoldStyler.INSTANCE_COUNTER);
            String q = version.getQualifier();
            if (q != null && !q.isEmpty())
                label.append("." + q, StyledString.COUNTER_STYLER);
        }
        String description = template.getShortDescription();
        if (description != null) {
            label.append(" \u2014 [", StyledString.QUALIFIER_STYLER).append(template.getShortDescription(), StyledString.QUALIFIER_STYLER).append("]", StyledString.QUALIFIER_STYLER);
        }
        cell.setText(label.toString());
        cell.setStyleRanges(label.getStyleRanges());
        Image image = loadedImages.get(template);
        if (image == null)
            cell.setImage(defaultIcon);
        else
            cell.setImage(image);
    }
}
Also used : Category(org.bndtools.templating.Category) Version(org.osgi.framework.Version) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Image(org.eclipse.swt.graphics.Image) Template(org.bndtools.templating.Template)

Example 5 with Template

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

the class TemplateSelectionWizardPage method setTemplate.

public void setTemplate(final Template template) {
    Template old = this.selected;
    this.selected = template;
    propSupport.firePropertyChange(PROP_TEMPLATE, old, template);
    if (template != null) {
        txtDescription.setText(String.format("<form>Loading help content for template '%s'...</form>", template.getName()));
        Job updateDescJob = new UpdateDescriptionJob(template, txtDescription);
        updateDescJob.setSystem(true);
        updateDescJob.schedule();
    } else {
        txtDescription.setText(NO_HELP_CONTENT);
    }
}
Also used : Job(org.eclipse.core.runtime.jobs.Job) 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