Skip to content

Commit

Permalink
Use specific P2 types
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Aug 15, 2022
1 parent d9efa1a commit c638e2a
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.tycho.IDependencyMetadata;
import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;
Expand All @@ -33,9 +34,9 @@ public class ArtifactMock implements IArtifactFacade {

private String classifier;

private Set<Object> dependencyMetadata = new LinkedHashSet<>();
private Set<IInstallableUnit> dependencyMetadata = new LinkedHashSet<>();

private Set<Object> secondaryDependencyMetadata = new LinkedHashSet<>();
private Set<IInstallableUnit> secondaryDependencyMetadata = new LinkedHashSet<>();

public ArtifactMock(File location, String groupId, String artifactId, String version, String packagingType,
String classifier) {
Expand Down Expand Up @@ -90,7 +91,7 @@ public void setClassifier(String classifier) {
this.classifier = classifier;
}

public Set<Object> getDependencyMetadata(boolean primary) {
public Set<IInstallableUnit> getDependencyMetadata(boolean primary) {
return primary ? dependencyMetadata : secondaryDependencyMetadata;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ public void generateSourceBundleMetadataForProjectWithP2Inf() throws Exception {
assertEquals(0, unit.getRequirements().size());
}

private IInstallableUnit getUnit(String id, Set<?> units) {
for (Object obj : units) {
IInstallableUnit unit = (IInstallableUnit) obj;
private IInstallableUnit getUnit(String id, Set<IInstallableUnit> units) {
for (IInstallableUnit unit : units) {
if (id.equals(unit.getId())) {
return unit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public Set<IInstallableUnit> getDependencyMetadata(DependencyMetadataType type)

@Override
public Set<IInstallableUnit> getDependencyMetadata() {
LinkedHashSet<IInstallableUnit> result = new LinkedHashSet<>();
Set<IInstallableUnit> result = new LinkedHashSet<>();
result.addAll(getDependencyMetadata(DependencyMetadataType.SEED));
result.addAll(getDependencyMetadata(DependencyMetadataType.RESOLVE));
return result;
}

@Override
public void setDependencyMetadata(DependencyMetadataType type, Collection<IInstallableUnit> units) {

typeMap.put(type, new LinkedHashSet<>(units));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ public void persistMetadata(Map<String, IP2Artifact> metadata, File unitsXml, Fi
Set<IInstallableUnit> units = new LinkedHashSet<>();
Set<IArtifactDescriptor> artifactDescriptors = new LinkedHashSet<>();
for (IP2Artifact artifact : metadata.values()) {
for (IInstallableUnit unit : artifact.getInstallableUnits()) {
units.add(unit);
}
units.addAll(artifact.getInstallableUnits());
artifactDescriptors.add(artifact.getArtifactDescriptor());
}
new MetadataIO().writeXML(units, unitsXml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ public Map<TargetEnvironment, P2ResolutionResult> resolveTargetDependencies(Targ
// we need a linked hashmap to maintain iteration-order, some of the code relies on it!
Map<TargetEnvironment, P2ResolutionResult> results = new LinkedHashMap<>();
Set<IInstallableUnit> usedTargetPlatformUnits = new LinkedHashSet<>();
Set<?> metadata = project != null ? project.getDependencyMetadata(DependencyMetadataType.SEED)
Set<IInstallableUnit> metadata = project != null ? project.getDependencyMetadata(DependencyMetadataType.SEED)
: Collections.emptySet();
for (TargetEnvironment environment : environments) {
if (isMatchingEnv(metadata, environment, logger::debug)) {
results.put(environment, resolveDependencies(Collections.<IInstallableUnit> emptySet(), project,
results.put(environment, resolveDependencies(Collections.emptySet(), project,
new ProjectorResolutionStrategy(logger), environment, targetPlatform, usedTargetPlatformUnits));
} else {
logger.info(MessageFormat.format(
Expand Down Expand Up @@ -505,19 +505,17 @@ private static void addArtifactFile(DefaultP2ResolutionResult result, IInstallab
* @param environment
* @return
*/
private static boolean isMatchingEnv(Set<?> metadata, TargetEnvironment environment,
private static boolean isMatchingEnv(Set<IInstallableUnit> metadata, TargetEnvironment environment,
Consumer<String> debugConsumer) {
if (metadata != null) {
for (Object meta : metadata) {
if (meta instanceof IInstallableUnit) {
IMatchExpression<IInstallableUnit> filter = ((IInstallableUnit) meta).getFilter();
if (filter != null) {
boolean match = filter.isMatch(InstallableUnit.contextIU(environment.toFilterProperties()));
debugConsumer.accept(MessageFormat.format("{0}: {1} (matches {2})", filter,
Arrays.toString(filter.getParameters()), match));
if (!match) {
return false;
}
for (IInstallableUnit meta : metadata) {
IMatchExpression<IInstallableUnit> filter = meta.getFilter();
if (filter != null) {
boolean match = filter.isMatch(InstallableUnit.contextIU(environment.toFilterProperties()));
debugConsumer.accept(MessageFormat.format("{0}: {1} (matches {2})", filter,
Arrays.toString(filter.getParameters()), match));
if (!match) {
return false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ public void testPublishingWithRootFeatures() {
assertThat(productUnit.getRequirements(), not(hasItem(requirement("org.eclipse.egit.feature.group", "2.0"))));

assertEquals("org.eclipse.help", seeds.get(1).getId());
assertThat((IInstallableUnit) seeds.get(1).getInstallableUnit(),
is(unitWithId("org.eclipse.help.feature.group")));
assertThat(seeds.get(1).getInstallableUnit(), is(unitWithId("org.eclipse.help.feature.group")));
assertEquals("org.eclipse.egit", seeds.get(2).getId());
assertThat((IInstallableUnit) seeds.get(2).getInstallableUnit(),
is(unitWithId("org.eclipse.egit.feature.group")));
Expand All @@ -291,13 +290,13 @@ public void testPublishingWithRootFeatures() {
*/
private static IInstallableUnit getUnit(Collection<DependencySeed> seeds) {
assertEquals(1, seeds.size());
return (IInstallableUnit) seeds.iterator().next().getInstallableUnit();
return seeds.iterator().next().getInstallableUnit();
}

private Set<IInstallableUnit> unitsIn(Collection<DependencySeed> seeds) {
Set<IInstallableUnit> result = new HashSet<>();
for (DependencySeed seed : seeds) {
result.add((IInstallableUnit) seed.getInstallableUnit());
result.add(seed.getInstallableUnit());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void testValidateProfileFile() throws Exception {
private static Map<String, IInstallableUnit> unitsById(Collection<DependencySeed> seeds) {
Map<String, IInstallableUnit> result = new HashMap<>();
for (DependencySeed seed : seeds) {
IInstallableUnit iu = (IInstallableUnit) seed.getInstallableUnit();
IInstallableUnit iu = seed.getInstallableUnit();
result.put(iu.getId(), iu);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ public void setDependencyMetadata(DependencyMetadataType type, Collection<IInsta

@Override
public Set<IInstallableUnit> getDependencyMetadata() {
LinkedHashSet<IInstallableUnit> result = new LinkedHashSet<>(
getDependencyMetadata(DependencyMetadataType.SEED));
Set<IInstallableUnit> result = new LinkedHashSet<>(getDependencyMetadata(DependencyMetadataType.SEED));
result.addAll(getDependencyMetadata(DependencyMetadataType.RESOLVE));
return result;
}

@Override
public Set<IInstallableUnit> getDependencyMetadata(DependencyMetadataType type) {
return Objects.requireNonNullElse((Set<IInstallableUnit>) getContextValue(getDependencyMetadataKey(type)),
Collections.emptySet());
@SuppressWarnings("unchecked")
Set<IInstallableUnit> contextValue = (Set<IInstallableUnit>) getContextValue(getDependencyMetadataKey(type));
return Objects.requireNonNullElse(contextValue, Collections.emptySet());
}

private static String getDependencyMetadataKey(DependencyMetadataType type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.tycho.plugins.p2.extras;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Collections;
import java.util.List;
Expand All @@ -30,6 +29,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.sisu.equinox.EquinoxServiceFactory;
import org.eclipse.tycho.IDependencyMetadata.DependencyMetadataType;
import org.eclipse.tycho.ReactorProject;
Expand Down Expand Up @@ -112,7 +112,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
Set<?> dependencyMetadata = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED);
Set<IInstallableUnit> dependencyMetadata = reactorProject.getDependencyMetadata(DependencyMetadataType.SEED);
if (dependencyMetadata == null || dependencyMetadata.isEmpty()) {
getLog().debug("Skipping baseline version comparison, no p2 artifacts created in build.");
return;
Expand All @@ -134,12 +134,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
TargetPlatform baselineTP = resolverFactory.getTargetPlatformFactory().createTargetPlatform(baselineTPStub,
TychoProjectUtils.getExecutionEnvironmentConfiguration(reactorProject), null);

for (Object item : dependencyMetadata) {
for (IInstallableUnit item : dependencyMetadata) {
try {
Method getIdMethod = item.getClass().getMethod("getId");
Method getVersionMethod = item.getClass().getMethod("getVersion");
String id = (String) getIdMethod.invoke(item);
Version version = new Version(getVersionMethod.invoke(item).toString());
String id = item.getId();
Version version = new Version(item.getVersion().toString());
P2ResolutionResult res = resolver.resolveInstallableUnit(baselineTP, id, "0.0.0");

for (Entry foundInBaseline : res.getArtifacts()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
import org.eclipse.tycho.osgi.adapters.MavenLoggerAdapter;
import org.eclipse.tycho.p2.facade.internal.AttachedArtifact;
import org.eclipse.tycho.p2.metadata.DependencyMetadataGenerator;
import org.eclipse.tycho.p2.metadata.IArtifactFacade;
import org.eclipse.tycho.p2.metadata.PublisherOptions;
import org.eclipse.tycho.p2.repository.LocalRepositoryP2Indices;
import org.eclipse.tycho.p2.resolver.facade.P2ResolutionResult;
Expand Down Expand Up @@ -150,15 +149,13 @@ public void setupProjects(final MavenSession session, final MavenProject project
typeMap.put(type, new LinkedHashSet<>());
}
for (IDependencyMetadata metadata : metadataMap.values()) {
for (Entry<DependencyMetadataType, Set<IInstallableUnit>> map : typeMap.entrySet()) {
map.getValue().addAll(metadata.getDependencyMetadata(map.getKey()));
}
typeMap.forEach((key, value) -> value.addAll(metadata.getDependencyMetadata(key)));
}
Set<IInstallableUnit> initial = new HashSet<>();
for (Entry<DependencyMetadataType, Set<IInstallableUnit>> entry : typeMap.entrySet()) {
reactorProject.setDependencyMetadata(entry.getKey(), entry.getValue());
initial.addAll(entry.getValue());
}
typeMap.forEach((key, value) -> {
reactorProject.setDependencyMetadata(key, value);
initial.addAll(value);
});
reactorProject.setDependencyMetadata(DependencyMetadataType.INITIAL, initial);
}

Expand Down Expand Up @@ -239,8 +236,7 @@ private ReactorProject getThisReactorProject(MavenSession session, MavenProject
Map<String, IDependencyMetadata> dependencyMetadata = getDependencyMetadata(session, project, environments,
optionalAction);
Map<DependencyMetadataType, Set<IInstallableUnit>> typeMap = new TreeMap<>();
for (Map.Entry<String, IDependencyMetadata> entry : dependencyMetadata.entrySet()) {
IDependencyMetadata value = entry.getValue();
for (IDependencyMetadata value : dependencyMetadata.values()) {
for (DependencyMetadataType type : DependencyMetadataType.values()) {
typeMap.computeIfAbsent(type, t -> new LinkedHashSet<>()).addAll(value.getDependencyMetadata(type));
}
Expand Down Expand Up @@ -343,19 +339,15 @@ public PomDependencyCollector resolvePomDependencies(MavenSession session, Maven
if (dependencyArtifacts instanceof ArtifactCollection) {
//enhance the dependency set
ArtifactCollection collection = (ArtifactCollection) dependencyArtifacts;
Map<IInstallableUnit, IArtifactFacade> mavenInstallableUnits = dependencyCollector
.getMavenInstallableUnits();
for (var entry : mavenInstallableUnits.entrySet()) {
IInstallableUnit key = entry.getKey();
IArtifactFacade val = entry.getValue();
dependencyCollector.getMavenInstallableUnits().forEach((key, val) -> {
ArtifactKey artifactKey = dependencyCollector.getArtifactKey(val);
File location = val.getLocation();
try {
collection.addArtifactFile(artifactKey, location, Collections.singleton(key));
} catch (IllegalStateException e) {
//already contained in the collection --> ignore it...
}
}
});
}
return dependencyCollector;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,15 @@ protected void attachP2Metadata() throws MojoExecutionException {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);

Set<IInstallableUnit> installableUnits = new LinkedHashSet<>();
for (Map.Entry<String, IP2Artifact> entry : generatedMetadata.entrySet()) {
String classifier = entry.getKey();
IP2Artifact p2artifact = entry.getValue();

generatedMetadata.forEach((classifier, p2artifact) -> {
installableUnits.addAll(p2artifact.getInstallableUnits());

// attach any new classified artifacts, like feature root files for example
if (classifier != null && !hasAttachedArtifact(project, classifier)) {
projectHelper.attachArtifact(project, getExtension(p2artifact.getLocation()), classifier,
p2artifact.getLocation());
}
}
});

// TODO 353889 distinguish between dependency resolution seed units ("primary") and other units of the project
reactorProject.setDependencyMetadata(DependencyMetadataType.SEED, installableUnits);
Expand Down

0 comments on commit c638e2a

Please sign in to comment.