Search in sources :

Example 21 with VersionedClause

use of aQute.bnd.build.model.clauses.VersionedClause in project bnd by bndtools.

the class ResourceTest method testResourceToVersionedClause.

public void testResourceToVersionedClause() throws Exception {
    ResourceBuilder rb = new ResourceBuilder();
    rb.addManifest(Domain.domain(IO.getFile("../demo-fragment/generated/demo-fragment.jar")));
    Resource resource = rb.build();
    VersionedClause versionClause = ResourceUtils.toVersionClause(resource, "[===,==+)");
    StringBuilder sb = new StringBuilder();
    versionClause.formatTo(sb);
    assertEquals("demo-fragment;version='[1.0.0,1.0.1)'", sb.toString());
}
Also used : ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Resource(org.osgi.resource.Resource)

Example 22 with VersionedClause

use of aQute.bnd.build.model.clauses.VersionedClause in project intellij-plugins by JetBrains.

the class ResolveAction method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent event) {
    VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
    Project project = event.getProject();
    if (virtualFile == null || project == null)
        return;
    Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
    if (document == null)
        return;
    FileDocumentManager.getInstance().saveAllDocuments();
    new Task.Backgroundable(project, message("bnd.resolve.requirements.title"), true) {

        private Map<Resource, List<Wire>> resolveResult;

        private String updatedText;

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            File file = new File(virtualFile.getPath());
            try (Workspace workspace = Workspace.findWorkspace(file);
                Run run = Run.createRun(workspace, file);
                ProjectResolver projectResolver = new ProjectResolver(run)) {
                resolveResult = projectResolver.resolve();
                List<VersionedClause> versionedClauses = projectResolver.getRunBundles().stream().map(c -> {
                    Attrs attrs = new Attrs();
                    attrs.put(Constants.VERSION_ATTRIBUTE, c.getVersion());
                    return new VersionedClause(c.getBundleSymbolicName(), attrs);
                }).sorted(Comparator.comparing(VersionedClause::getName)).collect(Collectors.toList());
                BndEditModel editModel = new BndEditModel();
                IDocument bndDocument = new aQute.bnd.properties.Document(document.getImmutableCharSequence().toString());
                editModel.loadFrom(bndDocument);
                editModel.setRunBundles(versionedClauses);
                editModel.saveChangesTo(bndDocument);
                updatedText = bndDocument.get();
            } catch (ProcessCanceledException e) {
                throw e;
            } catch (Exception e) {
                throw new WrappingException(e);
            }
            indicator.checkCanceled();
        }

        @Override
        public void onSuccess() {
            if (new ResolutionSucceedDialog(project, resolveResult).showAndGet() && FileModificationService.getInstance().prepareVirtualFilesForWrite(project, Collections.singleton(virtualFile))) {
                writeCommandAction(project).withName("Bndrun Resolve").run(() -> document.setText(updatedText));
            }
        }

        @Override
        public void onThrowable(@NotNull Throwable t) {
            Throwable cause = t instanceof WrappingException ? t.getCause() : t;
            LOG.warn("Resolution failed", cause);
            if (cause instanceof ResolutionException) {
                new ResolutionFailedDialog(project, (ResolutionException) cause).show();
            } else {
                OsmorcBundle.notification(message("bnd.resolve.failed.title"), cause.getMessage(), NotificationType.ERROR).notify(project);
            }
        }
    }.queue();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction.writeCommandAction(com.intellij.openapi.command.WriteCommandAction.writeCommandAction) Constants(aQute.bnd.osgi.Constants) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Task(com.intellij.openapi.progress.Task) Workspace(aQute.bnd.build.Workspace) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) Map(java.util.Map) Project(com.intellij.openapi.project.Project) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) Logger(com.intellij.openapi.diagnostic.Logger) OsmorcBundle.message(org.osmorc.i18n.OsmorcBundle.message) BndEditModel(aQute.bnd.build.model.BndEditModel) OsmorcBundle(org.osmorc.i18n.OsmorcBundle) ProjectResolver(biz.aQute.resolve.ProjectResolver) FileModificationService(com.intellij.codeInsight.FileModificationService) Resource(org.osgi.resource.Resource) AnAction(com.intellij.openapi.actionSystem.AnAction) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Run(aQute.bnd.build.Run) Collectors(java.util.stream.Collectors) File(java.io.File) NotificationType(com.intellij.notification.NotificationType) BndFileType(org.jetbrains.osgi.bnd.BndFileType) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IDocument(aQute.bnd.properties.IDocument) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) Wire(org.osgi.resource.Wire) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) ResolutionException(org.osgi.service.resolver.ResolutionException) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Collections(java.util.Collections) javax.swing(javax.swing) Task(com.intellij.openapi.progress.Task) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) ProjectResolver(biz.aQute.resolve.ProjectResolver) Attrs(aQute.bnd.header.Attrs) Document(com.intellij.openapi.editor.Document) IDocument(aQute.bnd.properties.IDocument) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) BndEditModel(aQute.bnd.build.model.BndEditModel) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Resource(org.osgi.resource.Resource) Run(aQute.bnd.build.Run) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) ResolutionException(org.osgi.service.resolver.ResolutionException) ResolutionException(org.osgi.service.resolver.ResolutionException) Project(com.intellij.openapi.project.Project) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) IDocument(aQute.bnd.properties.IDocument) Workspace(aQute.bnd.build.Workspace)

Example 23 with VersionedClause

use of aQute.bnd.build.model.clauses.VersionedClause in project bndtools by bndtools.

the class ResolutionWizard method performFinish.

@Override
public boolean performFinish() {
    Collection<Resource> resources;
    ResolutionResult result = resultsPage.getResult();
    if (result != null && result.getOutcome() == ResolutionResult.Outcome.Resolved) {
        resources = result.getResourceWirings().keySet();
    } else if (preserveRunBundleUnresolved) {
        return true;
    } else {
        resources = Collections.emptyList();
    }
    // Open stream for physical paths list in target dir
    PrintStream pathsStream = null;
    try {
        File targetDir;
        Project bndProject = model.getProject();
        targetDir = bndProject.getTargetDir();
        if (targetDir == null)
            targetDir = file.getLocation().toFile().getParentFile();
        if (!targetDir.exists() && !targetDir.mkdirs()) {
            throw new IOException("Could not create target directory " + targetDir);
        }
        File pathsFile = new File(targetDir, file.getName() + RESOLVED_PATHS_EXTENSION);
        pathsStream = new PrintStream(pathsFile, "UTF-8");
    } catch (Exception e) {
        logger.logError("Unable to write resolved path list in target directory for project " + file.getProject().getName(), e);
    }
    // Generate -runbundles and path list
    try {
        List<VersionedClause> runBundles = new ArrayList<VersionedClause>(resources.size());
        for (Resource resource : resources) {
            VersionedClause runBundle = resourceToRunBundle(resource);
            // [cs] Skip dups
            if (runBundles.contains(runBundle)) {
                continue;
            }
            runBundles.add(runBundle);
            if (pathsStream != null) {
                URI uri;
                try {
                    uri = ResourceUtils.getURI(ResourceUtils.getContentCapability(resource));
                } catch (IllegalArgumentException e) {
                    logger.logError("Resource has no content capability: " + ResourceUtils.getIdentity(resource), e);
                    continue;
                }
                VersionedClause runBundleWithUri = runBundle.clone();
                runBundleWithUri.getAttribs().put(BndConstants.RESOLUTION_URI_ATTRIBUTE, uri.toString());
                StringBuilder builder = new StringBuilder();
                runBundleWithUri.formatTo(builder, clauseAttributeSorter);
                pathsStream.println(builder.toString());
            }
        }
        Collections.sort(runBundles, new Comparator<VersionedClause>() {

            @Override
            public int compare(VersionedClause vc1, VersionedClause vc2) {
                int diff = vc1.getName().compareTo(vc2.getName());
                if (diff != 0)
                    return diff;
                String r1 = vc1.getVersionRange();
                if (r1 == null)
                    r1 = "";
                String r2 = vc2.getVersionRange();
                if (r2 == null)
                    r2 = "";
                return r1.compareTo(r2);
            }
        });
        // Do not change the order of existing runbundles because they migh have been ordered manually
        List<VersionedClause> diffAddBundles = new ArrayList<>(runBundles);
        List<VersionedClause> oldRunBundles = model.getRunBundles();
        if (oldRunBundles == null)
            oldRunBundles = Collections.emptyList();
        else
            diffAddBundles.removeAll(oldRunBundles);
        List<VersionedClause> diffRemvedBundles = new ArrayList<>(oldRunBundles);
        diffRemvedBundles.removeAll(runBundles);
        List<VersionedClause> updatedRunBundles = new ArrayList<>(oldRunBundles);
        updatedRunBundles.addAll(diffAddBundles);
        updatedRunBundles.removeAll(diffRemvedBundles);
        // do not use getRunBundles().addAll, because it will not reflect in UI or File
        model.setRunBundles(updatedRunBundles);
    } finally {
        if (pathsStream != null) {
            IO.close(pathsStream);
        }
    }
    return true;
}
Also used : PrintStream(java.io.PrintStream) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) ResolutionResult(org.bndtools.core.resolve.ResolutionResult) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) Project(aQute.bnd.build.Project) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 24 with VersionedClause

use of aQute.bnd.build.model.clauses.VersionedClause in project bndtools by bndtools.

the class ImportPackageQuickFixProcessorTest method addBundleToRepo_createsCorrectCapability.

@Test
public // Meta-test
void addBundleToRepo_createsCorrectCapability() throws Exception {
    clearRepos();
    Attrs v100 = new Attrs();
    v100.put(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, "1.0.0");
    Attrs v110 = new Attrs();
    v110.put(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, "1.1.0");
    addBundleToRepo("Repo 1", "my.test.bundle", new VersionedClause("org.osgi.framework", v100), new VersionedClause("my.test.pkg", v110));
    Map<String, Collection<Capability>> caps = repoMap.get("Repo 1");
    assertThat(caps).as("map keys").containsKeys("org.osgi.framework", "my.test.pkg");
    assertThat(frameworkBundles).as("framework bundle").containsExactly("my.test.bundle");
    Resource bundleResource = null;
    for (Map.Entry<String, Collection<Capability>> entry : caps.entrySet()) {
        Collection<Capability> packageCaps = entry.getValue();
        assertThat(packageCaps).isNotNull().hasSize(1);
        for (Capability pkg : packageCaps) {
            assertThat(pkg).as("pkg").isNotNull();
            assertThat(pkg.getResource()).as("resource").isNotNull();
            if (bundleResource == null) {
                bundleResource = pkg.getResource();
            } else {
                assertThat(pkg.getResource()).as("resource").isSameAs(bundleResource);
            }
        }
    }
    assertThat(bundleResource).as("bundleResource").isNotNull();
}
Also used : Capability(org.osgi.resource.Capability) VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) Resource(org.osgi.resource.Resource) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 25 with VersionedClause

use of aQute.bnd.build.model.clauses.VersionedClause in project bndtools by bndtools.

the class ImportPackageQuickFixProcessorTest method getCorrections_forBundleInTwoRepos_describesBundles.

@Test
public void getCorrections_forBundleInTwoRepos_describesBundles() {
    addBundleToRepo("Repo 2", "my.test.bundle", new VersionedClause("org.osgi.framework", new Attrs()));
    AddBundleCompletionProposal[] props = proposalsForImport("`org.osgi.framework'.*");
    assertThat(props).haveExactly(1, suggestsBundle("my.test.bundle", "Repo 1", "Repo 2"));
}
Also used : VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Attrs(aQute.bnd.header.Attrs) AddBundleCompletionProposal(org.bndtools.core.editors.ImportPackageQuickFixProcessor.AddBundleCompletionProposal) Test(org.junit.Test)

Aggregations

VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)26 Attrs (aQute.bnd.header.Attrs)16 Resource (org.osgi.resource.Resource)7 File (java.io.File)6 BndEditModel (aQute.bnd.build.model.BndEditModel)5 ArrayList (java.util.ArrayList)5 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)4 WizardDialog (org.eclipse.jface.wizard.WizardDialog)4 Test (org.junit.Test)4 Workspace (aQute.bnd.build.Workspace)3 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)3 Document (aQute.bnd.properties.Document)3 AndFilter (aQute.libg.filters.AndFilter)3 Filter (aQute.libg.filters.Filter)3 SimpleFilter (aQute.libg.filters.SimpleFilter)3 ProjectBundle (bndtools.model.repo.ProjectBundle)3 RepositoryBundle (bndtools.model.repo.RepositoryBundle)3 RepositoryBundleVersion (bndtools.model.repo.RepositoryBundleVersion)3 RepoBundleSelectionWizard (bndtools.wizards.repo.RepoBundleSelectionWizard)3 LinkedHashSet (java.util.LinkedHashSet)3