Search in sources :

Example 46 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testBasicFindProviders.

public static void testBasicFindProviders() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/repo1.index.xml")));
    BndEditModel runModel = new BndEditModel();
    BndrunResolveContext context = new BndrunResolveContext(runModel, registry, log);
    Requirement req = new CapReqBuilder("osgi.wiring.package").addDirective("filter", "(osgi.wiring.package=org.apache.felix.gogo.api)").buildSyntheticRequirement();
    List<Capability> providers = context.findProviders(req);
    assertEquals(1, providers.size());
    Resource resource = providers.get(0).getResource();
    assertEquals(IO.getFile("testdata/repo1/org.apache.felix.gogo.runtime-0.10.0.jar").toURI(), findContentURI(resource));
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) Resource(org.osgi.resource.Resource) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 47 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class BndrunResolveContextTest method testDontResolveBuildOnlyLibraries.

public static void testDontResolveBuildOnlyLibraries() {
    MockRegistry registry = new MockRegistry();
    registry.addPlugin(createRepo(IO.getFile("testdata/buildrepo.index.xml")));
    BndEditModel runModel = new BndEditModel();
    BndrunResolveContext context;
    context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers1 = context.findProviders(CapReqBuilder.createPackageRequirement("org.osgi.framework", null).buildSyntheticRequirement());
    assertEquals(0, providers1.size());
    context = new BndrunResolveContext(runModel, registry, log);
    List<Capability> providers2 = context.findProviders(CapReqBuilder.createPackageRequirement("java.security", null).buildSyntheticRequirement());
    assertEquals(0, providers2.size());
}
Also used : Capability(org.osgi.resource.Capability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) MockRegistry(test.lib.MockRegistry) BndEditModel(aQute.bnd.build.model.BndEditModel)

Example 48 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bndtools by bndtools.

the class PkgRenameParticipant method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final Map<IFile, TextChange> fileChanges = new HashMap<IFile, TextChange>();
    IResourceProxyVisitor visitor = new IResourceProxyVisitor() {

        @Override
        public boolean visit(IResourceProxy proxy) throws CoreException {
            if ((proxy.getType() == IResource.FOLDER) || (proxy.getType() == IResource.PROJECT)) {
                return true;
            }
            if (!((proxy.getType() == IResource.FILE) && proxy.getName().toLowerCase().endsWith(".bnd"))) {
                return false;
            }
            /* we're dealing with a *.bnd file */
            /* get the proxied file */
            IFile resource = (IFile) proxy.requestResource();
            /* read the file as a single string */
            String bndFileText = null;
            try {
                bndFileText = FileUtils.readFully(resource).get();
            } catch (Exception e) {
                String str = "Could not read file " + proxy.getName();
                logger.logError(str, e);
                throw new OperationCanceledException(str);
            }
            /*
                 * get the previous change for this file if it exists, or otherwise create a new change for it, but do
                 * not store it yet: wait until we know if there are actually changes in the file
                 */
            TextChange fileChange = getTextChange(resource);
            final boolean fileChangeIsNew = (fileChange == null);
            if (fileChange == null) {
                fileChange = new TextFileChange(proxy.getName(), resource);
                fileChange.setEdit(new MultiTextEdit());
            }
            TextEdit rootEdit = fileChange.getEdit();
            BndEditModel model = new BndEditModel();
            Document document = new Document(bndFileText);
            try {
                model.loadFrom(document);
            } catch (IOException e) {
                String str = "Could not load document " + proxy.getName();
                logger.logError(str, e);
                throw new OperationCanceledException(str);
            }
            /* loop over all renames to perform */
            for (Map.Entry<IPackageFragment, RenameArguments> entry : pkgFragments.entrySet()) {
                IPackageFragment pkgFragment = entry.getKey();
                RenameArguments arguments = entry.getValue();
                final String oldName = pkgFragment.getElementName();
                final String newName = arguments.getNewName();
                List<ImportPattern> newImportedPackages = makeNewHeaders(model.getImportPatterns(), oldName, newName);
                if (newImportedPackages != null) {
                    model.setImportPatterns(newImportedPackages);
                }
                List<ExportedPackage> newExportedPackages = makeNewHeaders(model.getExportedPackages(), oldName, newName);
                if (newExportedPackages != null) {
                    model.setExportedPackages(newExportedPackages);
                }
                List<String> newPrivatePackages = makeNewHeaders(model.getPrivatePackages(), oldName, newName);
                if (newPrivatePackages != null) {
                    model.setPrivatePackages(newPrivatePackages);
                }
                Map<String, String> changes = model.getDocumentChanges();
                for (Iterator<Entry<String, String>> iter = changes.entrySet().iterator(); iter.hasNext(); ) {
                    Entry<String, String> change = iter.next();
                    String propertyName = change.getKey();
                    String stringValue = change.getValue();
                    addEdit(document, rootEdit, propertyName, stringValue);
                    iter.remove();
                }
                Pattern pattern = Pattern.compile(/* match start boundary */
                "(^|" + grammarSeparator + ")" + /* match bundle activator */
                "(Bundle-Activator\\s*:\\s*)" + /* match itself / package name */
                "(" + Pattern.quote(oldName) + ")" + /* match class name */
                "(\\.[^\\.]+)" + /* match end boundary */
                "(" + grammarSeparator + "|" + Pattern.quote("\\") + "|$)");
                /* find all matches to replace and add them to the root edit */
                Matcher matcher = pattern.matcher(bndFileText);
                while (matcher.find()) {
                    rootEdit.addChild(new ReplaceEdit(matcher.start(3), matcher.group(3).length(), newName));
                }
            }
            /*
                 * only store the changes when no changes were stored before for this file and when there are actually
                 * changes.
                 */
            if (fileChangeIsNew && rootEdit.hasChildren()) {
                fileChanges.put(resource, fileChange);
            }
            return false;
        }
    };
    /* determine which projects have to be visited */
    Set<IProject> projectsToVisit = new HashSet<IProject>();
    for (IPackageFragment pkgFragment : pkgFragments.keySet()) {
        projectsToVisit.add(pkgFragment.getResource().getProject());
        for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencingProjects()) {
            projectsToVisit.add(projectToVisit);
        }
        for (IProject projectToVisit : pkgFragment.getResource().getProject().getReferencedProjects()) {
            projectsToVisit.add(projectToVisit);
        }
    }
    /* visit the projects */
    for (IProject projectToVisit : projectsToVisit) {
        projectToVisit.accept(visitor, IContainer.NONE);
    }
    if (fileChanges.isEmpty()) {
        /* no changes at all */
        return null;
    }
    /* build a composite change with all changes */
    CompositeChange cs = new CompositeChange(changeTitle);
    for (TextChange fileChange : fileChanges.values()) {
        cs.add(fileChange);
    }
    return cs;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) HashMap(java.util.HashMap) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) Matcher(java.util.regex.Matcher) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IDocument(aQute.bnd.properties.IDocument) Document(aQute.bnd.properties.Document) Entry(java.util.Map.Entry) IResourceProxy(org.eclipse.core.resources.IResourceProxy) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) BndEditModel(aQute.bnd.build.model.BndEditModel) HashSet(java.util.HashSet) ImportPattern(aQute.bnd.build.model.clauses.ImportPattern) Pattern(java.util.regex.Pattern) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IOException(java.io.IOException) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) ImportPattern(aQute.bnd.build.model.clauses.ImportPattern) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ExportedPackage(aQute.bnd.build.model.clauses.ExportedPackage) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) HashMap(java.util.HashMap) Map(java.util.Map) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 49 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bndtools by bndtools.

the class ReleaseHelper method updateBundleVersion.

private static void updateBundleVersion(ReleaseContext context, Baseline current, Builder builder) throws IOException, CoreException {
    Version bundleVersion = current.getSuggestedVersion();
    if (bundleVersion != null) {
        File file = builder.getPropertiesFile();
        Properties properties = builder.getProperties();
        if (file == null) {
            file = context.getProject().getPropertiesFile();
            properties = context.getProject().getProperties();
        }
        final IFile resource = (IFile) ReleaseUtils.toResource(file);
        final Document document;
        if (resource.exists()) {
            byte[] bytes = readFully(resource.getContents());
            document = new Document(new String(bytes, resource.getCharset()));
        } else {
            //$NON-NLS-1$
            document = new Document("");
        }
        final BndEditModel model;
        BndEditModel model2;
        try {
            model2 = new BndEditModel(Central.getWorkspace());
        } catch (Exception e) {
            System.err.println("Unable to create BndEditModel with Workspace, defaulting to without Workspace");
            model2 = new BndEditModel();
        }
        model = model2;
        model.loadFrom(document);
        String currentVersion = model.getBundleVersionString();
        String templateVersion = updateTemplateVersion(currentVersion, bundleVersion);
        model.setBundleVersion(templateVersion);
        properties.setProperty(Constants.BUNDLE_VERSION, templateVersion);
        final Document finalDoc = document;
        Runnable run = new Runnable() {

            @Override
            public void run() {
                model.saveChangesTo(finalDoc);
                try {
                    writeFully(finalDoc.get(), resource, false);
                    resource.refreshLocal(IResource.DEPTH_ZERO, null);
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        if (Display.getCurrent() == null) {
            Display.getDefault().syncExec(run);
        } else
            run.run();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) Version(aQute.bnd.version.Version) Properties(java.util.Properties) Document(aQute.bnd.properties.Document) IFile(org.eclipse.core.resources.IFile) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 50 with BndEditModel

use of aQute.bnd.build.model.BndEditModel in project bnd by bndtools.

the class JpmRepoTest method testScr.

public void testScr() {
    Repository repo = ws.getPlugin(Repository.class);
    BndEditModel model = new BndEditModel();
    model.setRunFw("org.apache.felix.framework");
    List<Requirement> requires = new ArrayList<Requirement>();
    CapReqBuilder capReq = CapReqBuilder.createSimpleRequirement("osgi.extender", "osgi.component", "[1.1,2)");
    requires.add(capReq.buildSyntheticRequirement());
    Map<Requirement, Collection<Capability>> shell = repo.findProviders(requires);
    assertNotNull(shell);
    assertEquals(1, shell.size());
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) Requirement(org.osgi.resource.Requirement) InfoRepository(aQute.bnd.service.repository.InfoRepository) Repository(org.osgi.service.repository.Repository) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BndEditModel(aQute.bnd.build.model.BndEditModel)

Aggregations

BndEditModel (aQute.bnd.build.model.BndEditModel)54 Requirement (org.osgi.resource.Requirement)35 MockRegistry (test.lib.MockRegistry)34 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)32 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)25 Capability (org.osgi.resource.Capability)25 Resource (org.osgi.resource.Resource)15 File (java.io.File)14 ArrayList (java.util.ArrayList)12 Document (aQute.bnd.properties.Document)8 List (java.util.List)6 Resolver (org.osgi.service.resolver.Resolver)6 Workspace (aQute.bnd.build.Workspace)5 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)5 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)5 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4 CoreException (org.eclipse.core.runtime.CoreException)4 ResolutionException (org.osgi.service.resolver.ResolutionException)4