Skip to content

Commit

Permalink
eclipse-tycho#114 Create a 'maven-p2-site' plugin
Browse files Browse the repository at this point in the history
- add a PublisherApplication
- add first version of mojo

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed May 5, 2021
1 parent 75ded10 commit dd1c956
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 2 deletions.
Expand Up @@ -3,7 +3,7 @@ Bundle-Version: 2.4.0.qualifier
Bundle-Name: Tycho p2 Tools Bundle
Bundle-ManifestVersion: 2
Bundle-Description: Implementation of tools for generating, copying, and using p2 repositories in Tycho
Bundle-SymbolicName: org.eclipse.tycho.p2.tools.impl
Bundle-SymbolicName: org.eclipse.tycho.p2.tools.impl;singleton:=true
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.equinox.p2.director.app,
org.eclipse.equinox.p2.core,
Expand Down
Expand Up @@ -13,5 +13,6 @@ bin.includes = META-INF/,\
.,\
OSGI-INF/,\
about.html,\
plugin.properties
plugin.properties,\
plugin.xml
source.. = src/main/java/
16 changes: 16 additions & 0 deletions tycho-bundles/org.eclipse.tycho.p2.tools.impl/plugin.xml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
id="org.eclipse.tycho.p2.tools.publisher.TychoFeaturesAndBundlesPublisher"
point="org.eclipse.core.runtime.applications">
<application
cardinality="1"
thread="any"
visible="true">
<run
class="org.eclipse.tycho.p2.tools.publisher.TychoFeaturesAndBundlesPublisherApplication">
</run>
</application>
</extension>
</plugin>
@@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2021 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2.tools.publisher;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.equinox.internal.p2.updatesite.CategoryXMLAction;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.publisher.AbstractPublisherAction;
import org.eclipse.equinox.p2.publisher.AbstractPublisherApplication;
import org.eclipse.equinox.p2.publisher.AdviceFileAdvice;
import org.eclipse.equinox.p2.publisher.IPublisherAction;
import org.eclipse.equinox.p2.publisher.IPublisherInfo;
import org.eclipse.equinox.p2.publisher.IPublisherResult;
import org.eclipse.equinox.p2.publisher.PublisherInfo;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.osgi.framework.BundleException;

@SuppressWarnings("restriction")
public class TychoFeaturesAndBundlesPublisherApplication extends AbstractPublisherApplication {

private static final String MAVEN_PREFIX = "maven-";
private BundleDescription[] bundles;
private File[] advices;
private URI categoryDefinition;

@Override
protected void processParameter(String arg, String parameter, PublisherInfo publisherInfo)
throws URISyntaxException {
super.processParameter(arg, parameter, publisherInfo);
if (arg.equalsIgnoreCase("-bundles")) {
bundles = Arrays.stream(AbstractPublisherAction.getArrayFromString(parameter, ",")).map(File::new)
.map(t -> {
try {
return BundlesAction.createBundleDescription(t);
} catch (IOException | BundleException e) {
//ignoring files that are "not bundles" they will be skipped on the later steps
return null;
}
}).toArray(BundleDescription[]::new);
}
if (arg.equalsIgnoreCase("-advices")) {
advices = Arrays.stream(AbstractPublisherAction.getArrayFromString(parameter, ","))
.map(str -> str.isBlank() ? null : new File(str)).toArray(File[]::new);
}
if (arg.equalsIgnoreCase("-categoryDefinition")) {
categoryDefinition = URIUtil.fromString(parameter);
}

}

@Override
protected IPublisherAction[] createActions() {
List<IPublisherAction> result = new ArrayList<>();
if (advices != null) {
List<AdviceFileAdvice> advicesList = new ArrayList<>();
for (int i = 0; i < advices.length; i++) {
File adviceFile = advices[i];
BundleDescription bundleDescription;
if (i >= bundles.length - 1 || (bundleDescription = bundles[i]) == null) {
continue;
}
String symbolicName = bundleDescription.getSymbolicName();
if (symbolicName == null) {
//not a bundle... no advice...
continue;
}
advicesList.add(new AdviceFileAdvice(symbolicName,
PublisherHelper.fromOSGiVersion(bundleDescription.getVersion()),
new Path(adviceFile.getParentFile().getAbsolutePath()), new Path(adviceFile.getName())) {

@Override
public Map<String, String> getArtifactProperties(IInstallableUnit iu,
IArtifactDescriptor descriptor) {

// workaround Bug 539672
Map<String, String> properties = super.getInstallableUnitProperties(null);
if (properties != null) {
if (descriptor instanceof ArtifactDescriptor) {
ArtifactDescriptor artifactDescriptor = (ArtifactDescriptor) descriptor;
for (Map.Entry<String, String> entry : properties.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.startsWith(MAVEN_PREFIX)) {
String key2 = "maven." + key.substring(MAVEN_PREFIX.length());
artifactDescriptor.setProperty(key2, value);
}
}
}
}
return null;
}

@Override
public Map<String, String> getInstallableUnitProperties(InstallableUnitDescription iu) {
Map<String, String> properties = super.getInstallableUnitProperties(iu);
if (properties == null) {
return null;
}
Map<String, String> installableUnitProperties = new LinkedHashMap<>(properties);
Set<String> keySet = installableUnitProperties.keySet();
for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string.startsWith(MAVEN_PREFIX)) {
iterator.remove();
}
}
return installableUnitProperties.isEmpty() ? null : installableUnitProperties;
}

});
result.add(new AdviceFilePublisherAction(advicesList));
}
}
if (bundles != null) {
result.add(new BundlesAction(bundles));
}
if (categoryDefinition != null) {
result.add(new CategoryXMLAction(categoryDefinition, "category"));
}
return result.toArray(IPublisherAction[]::new);
}

private static final class AdviceFilePublisherAction extends AbstractPublisherAction {

private List<AdviceFileAdvice> advicesList;

public AdviceFilePublisherAction(List<AdviceFileAdvice> advicesList) {
this.advicesList = advicesList;
}

@Override
public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) {
for (AdviceFileAdvice advice : advicesList) {
publisherInfo.addAdvice(advice);
}
return Status.OK_STATUS;
}

}

}

0 comments on commit dd1c956

Please sign in to comment.