Search in sources :

Example 1 with Document

use of aQute.bnd.properties.Document in project bnd by bndtools.

the class BndModelTest method testSetBuildPath.

public void testSetBuildPath() throws Exception {
    BndEditModel model = new BndEditModel();
    File bndFile = getFile(BND_BUILDPATH);
    model.loadFrom(bndFile);
    List<VersionedClause> buildPath = model.getBuildPath();
    // The remove causes the actual problem
    buildPath.remove(0);
    model.setBuildPath(buildPath);
    Document document = new Document(IO.collect(bndFile));
    model.saveChangesTo(document);
    String data = document.get();
    assertEquals(BND_BUILDPATH_EXPECTED, data);
}
Also used : VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Document(aQute.bnd.properties.Document) BndEditModel(aQute.bnd.build.model.BndEditModel) File(java.io.File)

Example 2 with Document

use of aQute.bnd.properties.Document in project bndtools by bndtools.

the class EnableSubBundlesOperation method run.

@Override
public void run(final IProgressMonitor monitor) throws CoreException {
    IResource container = workspace.getRoot().findMember(containerPath);
    if (container == null || !container.exists())
        throw newCoreException("Container path does not exist", null);
    // Create new project model
    BndEditModel newBundleModel;
    try {
        newBundleModel = new BndEditModel(Central.getWorkspace());
    } catch (Exception e) {
        System.err.println("Unable to create BndEditModel with Workspace, defaulting to without Workspace");
        newBundleModel = new BndEditModel();
    }
    // Load project file and model
    IProject project = container.getProject();
    IFile projectFile = project.getFile(Project.BNDFILE);
    BndEditModel projectModel;
    final Document projectDocument;
    try {
        if (projectFile.exists()) {
            byte[] bytes = FileUtils.readFully(projectFile.getContents());
            projectDocument = new Document(new String(bytes, projectFile.getCharset()));
        } else {
            projectDocument = new Document("");
        }
        try {
            projectModel = new BndEditModel(Central.getWorkspace());
        } catch (Exception e) {
            System.err.println("Unable to create BndEditModel with Workspace, defaulting to without Workspace");
            projectModel = new BndEditModel();
        }
        projectModel.loadFrom(projectDocument);
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, e.getMessage(), e));
    }
    // Sub-bundles are meaningless outside of Bndtools
    if (project.hasNature(BndtoolsConstants.NATURE_ID)) {
        // Check if we need to enable sub-bundles on the project file
        boolean enableSubs;
        List<String> subBndFiles = projectModel.getSubBndFiles();
        List<String> availableHeaders = calculateProjectOnlyHeaders(projectModel.getAllPropertyNames());
        Collection<String> bundleSpecificHeaders = calculateBundleSpecificHeaders(availableHeaders);
        if (subBndFiles == null || subBndFiles.isEmpty()) {
            final EnableSubBundlesDialog subBundlesDialog = new EnableSubBundlesDialog(parentShell, availableHeaders, bundleSpecificHeaders);
            if (subBundlesDialog.open() != Window.OK) {
                monitor.setCanceled(true);
                return;
            }
            enableSubs = subBundlesDialog.isEnableSubBundles();
            bundleSpecificHeaders = subBundlesDialog.getSelectedProperties();
        } else {
            enableSubs = false;
        }
        // Enable subs and copy entries from project model to new bundle model
        if (enableSubs) {
            projectModel.setSubBndFiles(Arrays.asList(new String[] { "*.bnd" }));
            for (String propertyName : bundleSpecificHeaders) {
                Object value = projectModel.genericGet(propertyName);
                projectModel.genericSet(propertyName, null);
                newBundleModel.genericSet(propertyName, value);
            }
            // Save the project model
            projectModel.saveChangesTo(projectDocument);
            FileUtils.writeFully(projectDocument.get(), projectFile, false);
        }
    }
    // Generate the new bundle model
    Document newBundleDocument = new Document("");
    newBundleModel.saveChangesTo(newBundleDocument);
    try {
        newBundleInputStream = new ByteArrayInputStream(newBundleDocument.get().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        newBundleInputStream = null;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(aQute.bnd.properties.Document) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BndEditModel(aQute.bnd.build.model.BndEditModel) IResource(org.eclipse.core.resources.IResource)

Example 3 with Document

use of aQute.bnd.properties.Document in project bnd by bndtools.

the class Bndrun method resolve.

public <T> T resolve(boolean failOnChanges, boolean writeOnChanges, Converter<T, Collection<? extends HeaderClause>> runbundlesFormatter) throws Exception {
    try (ProjectResolver projectResolver = new ProjectResolver(this)) {
        try {
            Map<Resource, List<Wire>> resolution = projectResolver.resolve();
            if (!projectResolver.isOk()) {
                return runbundlesFormatter.convert(Collections.<VersionedClause>emptyList());
            }
            Set<Resource> resources = resolution.keySet();
            List<VersionedClause> runBundles = new ArrayList<>();
            for (Resource resource : resources) {
                VersionedClause runBundle = ResourceUtils.toVersionClause(resource, "[===,==+)");
                if (!runBundles.contains(runBundle)) {
                    runBundles.add(runBundle);
                }
            }
            Collections.sort(runBundles, new Comparator<VersionedClause>() {

                @Override
                public int compare(VersionedClause a, VersionedClause b) {
                    int diff = a.getName().compareTo(b.getName());
                    return (diff != 0) ? diff : a.getVersionRange().compareTo(b.getVersionRange());
                }
            });
            File runFile = getPropertiesFile();
            BndEditModel bem = new BndEditModel(getWorkspace());
            Document doc = new Document(IO.collect(runFile));
            bem.loadFrom(doc);
            List<VersionedClause> bemRunBundles = bem.getRunBundles();
            if (bemRunBundles == null)
                bemRunBundles = new ArrayList<>();
            String originalRunbundlesString = runbundlesWrappedFormatter.convert(bemRunBundles);
            logger.debug("Original -runbundles was:\n\t {}", originalRunbundlesString);
            String runbundlesString = runbundlesWrappedFormatter.convert(runBundles);
            logger.debug("Resolved -runbundles is:\n\t {}", runbundlesString);
            List<VersionedClause> deltaAdd = new ArrayList<>(runBundles);
            deltaAdd.removeAll(bemRunBundles);
            List<VersionedClause> deltaRemove = new ArrayList<>(bemRunBundles);
            deltaRemove.removeAll(runBundles);
            boolean added = bemRunBundles.addAll(deltaAdd);
            boolean removed = bemRunBundles.removeAll(deltaRemove);
            if (added || removed) {
                if (failOnChanges && !bemRunBundles.isEmpty()) {
                    error("The runbundles have changed. Failing the build!\nWas: %s\nIs: %s", originalRunbundlesString, runbundlesString);
                    return runbundlesFormatter.convert(Collections.<VersionedClause>emptyList());
                }
                if (writeOnChanges) {
                    bem.setRunBundles(bemRunBundles);
                    String runBundlesProperty = bem.getDocumentChanges().get(Constants.RUNBUNDLES);
                    logger.debug("Writing changes to {}", runFile.getAbsolutePath());
                    logger.debug("{}:{}", Constants.RUNBUNDLES, runBundlesProperty);
                    bem.saveChangesTo(doc);
                    IO.store(doc.get(), runFile);
                }
            }
            return runbundlesFormatter.convert(bemRunBundles);
        } finally {
            getInfo(projectResolver);
        }
    }
}
Also used : VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) Document(aQute.bnd.properties.Document) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 4 with Document

use of aQute.bnd.properties.Document in project bnd by bndtools.

the class BndEditModelTest method testVariableInRunRequirements.

public static void testVariableInRunRequirements() throws Exception {
    Workspace ws = new Workspace(new File("testresources/ws"));
    BndEditModel model = new BndEditModel(ws);
    File f = new File("testresources/ws/p7/reuse.bndrun");
    model.setBndResource(f);
    model.setBndResourceName("reuse.bndrun");
    model.loadFrom(f);
    // VERIFY
    Processor processor = model.getProperties();
    String runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    String[] rrr = runrequirements.split(",");
    assertEquals(4, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable2)'", rrr[1]);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[2]);
    assertEquals("osgi.identity;filter:='(osgi.identity=c)'", rrr[3]);
    // [cs] don't know how to update this.
    List<Requirement> r = model.getRunRequires();
    assertEquals(3, r.size());
    assertEquals(new CapReqBuilder("${var}").buildSyntheticRequirement(), r.get(0));
    assertEquals(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=b)").buildSyntheticRequirement(), r.get(1));
    assertEquals(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=c)").buildSyntheticRequirement(), r.get(2));
    // Test Set with variables
    List<Requirement> rr = new LinkedList<Requirement>();
    rr.add(new CapReqBuilder(IdentityNamespace.IDENTITY_NAMESPACE).addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, "(osgi.identity=b)").buildSyntheticRequirement());
    rr.add(new CapReqBuilder("${var}").buildSyntheticRequirement());
    model.setRunRequires(rr);
    // VERIFY
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(3, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable)'", rrr[1]);
    assertEquals("osgi.identity;filter:='(osgi.identity=variable2)'", rrr[2]);
    // Test SET
    rr = new LinkedList<Requirement>();
    rr.add(getReq("(osgi.identity=b)"));
    rr.add(getReq("(osgi.identity=c)"));
    model.setRunRequires(rr);
    // VERIFY
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(2, rrr.length);
    assertEquals("osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("osgi.identity;filter:='(osgi.identity=c)'", rrr[1]);
    r = model.getRunRequires();
    assertEquals(getReq("(osgi.identity=b)"), r.get(0));
    assertEquals(getReq("(osgi.identity=c)"), r.get(1));
    // TEST Saving changes and those changes persist...
    Document d = new Document("");
    model.saveChangesTo(d);
    processor = model.getProperties();
    runrequirements = processor.mergeProperties(Constants.RUNREQUIRES);
    rrr = runrequirements.split(",");
    assertEquals(2, rrr.length);
    assertEquals("	osgi.identity;filter:='(osgi.identity=b)'", rrr[0]);
    assertEquals("	osgi.identity;filter:='(osgi.identity=c)'", rrr[1]);
    assertEquals(getReq("(osgi.identity=b)"), r.get(0));
    assertEquals(getReq("(osgi.identity=c)"), r.get(1));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Processor(aQute.bnd.osgi.Processor) Document(aQute.bnd.properties.Document) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel) LinkedList(java.util.LinkedList) Workspace(aQute.bnd.build.Workspace)

Example 5 with Document

use of aQute.bnd.properties.Document in project bnd by bndtools.

the class PropertiesTest method testBndEditModel.

public static void testBndEditModel() throws Exception {
    Document doc = new Document("# Hello\nBundle-Description:\tTest ♉\n" + "\n\nBundle-SymbolicName:\ttest.properties\n" + "Private-Package:\tpp1\n");
    BndEditModel model = new BndEditModel();
    model.loadFrom(doc);
    model.addPrivatePackage("pp2");
    model.addPrivatePackage("pp3");
    model.setBundleVersion("1.0.0");
    model.setBundleVersion("1.1.0");
    System.out.println(doc.get());
    File file = File.createTempFile("test", ".properties");
    IO.copy(model.toAsciiStream(doc), file);
    model = new BndEditModel();
    model.loadFrom(file);
    Properties props = new Properties();
    try (InputStream in = new FileInputStream(file)) {
        props.load(in);
    }
    assertEquals(props.getProperty("Bundle-Version"), model.getBundleVersionString());
    List<String> privatePackages = model.getPrivatePackages();
    String s = props.getProperty("Private-Package");
    String[] pkgs = s.split("\\,");
    for (String pkg : pkgs) {
        assertTrue(privatePackages.remove(pkg));
    }
    assertEquals(0, privatePackages.size());
    String desc = props.getProperty("Bundle-Description");
    assertEquals(desc, "Test ♉");
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Document(aQute.bnd.properties.Document) Properties(java.util.Properties) BndEditModel(aQute.bnd.build.model.BndEditModel) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Document (aQute.bnd.properties.Document)9 BndEditModel (aQute.bnd.build.model.BndEditModel)8 File (java.io.File)5 IFile (org.eclipse.core.resources.IFile)4 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)3 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 CoreException (org.eclipse.core.runtime.CoreException)3 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Properties (java.util.Properties)2 IProject (org.eclipse.core.resources.IProject)2 Workspace (aQute.bnd.build.Workspace)1 ImportPattern (aQute.bnd.build.model.clauses.ImportPattern)1 Attrs (aQute.bnd.header.Attrs)1 Processor (aQute.bnd.osgi.Processor)1 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)1 IDocument (aQute.bnd.properties.IDocument)1