|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Christoph Läubrich |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Christoph Läubrich - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.m2e.bnd.ui; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Properties; |
| 21 | + |
| 22 | +import org.apache.maven.artifact.Artifact; |
| 23 | +import org.apache.maven.project.MavenProject; |
| 24 | +import org.eclipse.core.resources.IProject; |
| 25 | +import org.eclipse.core.runtime.AdapterTypes; |
| 26 | +import org.eclipse.core.runtime.Adapters; |
| 27 | +import org.eclipse.core.runtime.IAdapterFactory; |
| 28 | +import org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata; |
| 29 | +import org.eclipse.m2e.core.project.IMavenProjectFacade; |
| 30 | +import org.eclipse.m2e.core.project.configurator.MojoExecutionKey; |
| 31 | +import org.osgi.service.component.annotations.Component; |
| 32 | + |
| 33 | +import aQute.bnd.build.Project; |
| 34 | +import aQute.bnd.build.Workspace; |
| 35 | +import aQute.bnd.maven.lib.configuration.BeanProperties; |
| 36 | +import aQute.bnd.maven.lib.resolve.ImplicitFileSetRepository; |
| 37 | +import aQute.bnd.osgi.Processor; |
| 38 | + |
| 39 | +/** |
| 40 | + * Adapts eclipse projects managed by m2e to bnd projects |
| 41 | + */ |
| 42 | +@Component |
| 43 | +@AdapterTypes(adaptableClass = IProject.class, adapterNames = Project.class) |
| 44 | +public class BndPluginAdapter implements IAdapterFactory { |
| 45 | + |
| 46 | + @Override |
| 47 | + public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { |
| 48 | + try { |
| 49 | + if (adaptableObject instanceof IProject eclipseProject) { |
| 50 | + if (adapterType == Project.class) { |
| 51 | + IMavenProjectFacade mavenProject = Adapters.adapt(eclipseProject, IMavenProjectFacade.class); |
| 52 | + if (isRelevantProject(mavenProject)) { |
| 53 | + System.out.println(eclipseProject.getName() + " uses bnd plugin!"); |
| 54 | + BeanProperties beanProperties = new BeanProperties(); |
| 55 | + MavenProject mp = mavenProject.getMavenProject(); |
| 56 | + if (mp != null) { |
| 57 | + beanProperties.put("project", mp); |
| 58 | + } |
| 59 | + // TODO beanProperties.put("settings", settings); |
| 60 | + Properties processorProperties = new Properties(beanProperties); |
| 61 | + if (mp != null) { |
| 62 | + Properties projectProperties = mp.getProperties(); |
| 63 | + for (String key : projectProperties.stringPropertyNames()) { |
| 64 | + processorProperties.setProperty(key, projectProperties.getProperty(key)); |
| 65 | + } |
| 66 | + } |
| 67 | + Processor processor = new Processor(processorProperties, false); |
| 68 | + Collection<File> list = new ArrayList<File>(); |
| 69 | + if (mp != null) { |
| 70 | + for (Artifact artifact : mp.getArtifacts()) { |
| 71 | + list.add(artifact.getFile()); |
| 72 | + } |
| 73 | + } |
| 74 | + ImplicitFileSetRepository repository = new ImplicitFileSetRepository("Project Artifacts", list); |
| 75 | + // TODO propertiesFile = new BndConfiguration(project, |
| 76 | + // mojoExecution).loadProperties(builder); |
| 77 | + // TODO |
| 78 | + // aQute.bnd.maven.lib.resolve.BndrunContainer.getFileSetRepository(MavenProject) |
| 79 | + |
| 80 | + Workspace standaloneWorkspace = Workspace.createStandaloneWorkspace(processor, |
| 81 | + mavenProject.getPomFile().getParentFile().toURI()); |
| 82 | + standaloneWorkspace.addBasicPlugin(repository); |
| 83 | + if (mp == null) { |
| 84 | + standaloneWorkspace.set("workspaceName", mavenProject.getArtifactKey().toPortableString()); |
| 85 | + } else { |
| 86 | + // TODO make part of IMavenProjectFacade |
| 87 | + standaloneWorkspace.set("workspaceName", mp.getName()); |
| 88 | + String description = mp.getDescription(); |
| 89 | + if (description != null) { |
| 90 | + standaloneWorkspace.set("workspaceDescription", description); |
| 91 | + } |
| 92 | + } |
| 93 | + standaloneWorkspace.refresh(); |
| 94 | + Project bndProject = new Project(standaloneWorkspace, null, null); |
| 95 | + bndProject.setBase(null); |
| 96 | + return adapterType.cast(bndProject); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } catch (Exception e) { |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + private boolean isRelevantProject(IMavenProjectFacade mavenProject) { |
| 107 | + // TODO cache result inside IProject store |
| 108 | + if (mavenProject != null) { |
| 109 | + Map<MojoExecutionKey, List<IPluginExecutionMetadata>> mapping = mavenProject.getMojoExecutionMapping(); |
| 110 | + for (MojoExecutionKey key : mapping.keySet()) { |
| 111 | + if ("biz.aQute.bnd".equals(key.groupId()) && "bnd-maven-plugin".equals(key.artifactId())) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments