Skip to content

Commit

Permalink
Resolve API baseline with all matching environments
Browse files Browse the repository at this point in the history
Currently the baseline is only resolved against the running target
environment, but this does not work when multiple ones are matching or
there is a platform filter.

This resolves the API baseline against all matching target environments.
  • Loading branch information
laeubi committed Feb 4, 2024
1 parent ce3b3d1 commit dec71ae
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
import org.osgi.framework.Filter;

public final class TargetEnvironment {
private static final Properties EMPTY_PROPERTIES = new Properties();
Expand Down Expand Up @@ -70,6 +71,13 @@ public boolean match(String os, String ws, String arch) {
(arch == null || arch.equals(this.arch));
}

public boolean match(Filter filter) {
if (filter != null) {
return filter.matches(toFilterProperties());
}
return true;
}

/**
* Returns the target environment as string of the form <code>ws.os.arch</code>. This format is
* used by the p2 publishers and in that context called "configuration" or "config spec".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.tycho.DependencyResolutionException;
import org.eclipse.tycho.IllegalArtifactReferenceException;
import org.eclipse.tycho.MavenRepositoryLocation;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
Expand Down Expand Up @@ -134,9 +135,6 @@ public class ApiAnalysisMojo extends AbstractMojo {
@Component
private TychoProjectManager projectManager;

@Component
private ApiApplicationResolver resolver;

@Component
private ApiApplicationResolver applicationResolver;

Expand All @@ -145,8 +143,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
return;
}
Optional<EclipseProject> eclipseProject = projectManager.getEclipseProject(project);
if (eclipseProject.isEmpty() || !eclipseProject.get().hasNature(ApiPlugin.NATURE_ID)) {
Optional<EclipseProject> eclipseProjectValue = projectManager.getEclipseProject(project);
if (eclipseProjectValue.isEmpty() || !eclipseProjectValue.get().hasNature(ApiPlugin.NATURE_ID)) {
return;
}

Expand Down Expand Up @@ -293,12 +291,14 @@ private Collection<Path> getBaselineBundles() throws MojoFailureException {
long start = System.currentTimeMillis();
Collection<Path> baselineBundles;
try {
Collection<TargetEnvironment> targetEnvironments = projectManager.getTargetEnvironments(project);
Optional<ArtifactKey> artifactKey = projectManager.getArtifactKey(project);
getLog().info("Resolve API baseline for " + project.getId());
baselineBundles = resolver.getApiBaselineBundles(
getLog().info("Resolve API baseline for " + project.getId() + " with "
+ targetEnvironments.stream().map(String::valueOf).collect(Collectors.joining(", ")));
baselineBundles = applicationResolver.getApiBaselineBundles(
baselines.stream().filter(repo -> repo.getUrl() != null)
.map(repo -> new MavenRepositoryLocation(repo.getId(), URI.create(repo.getUrl()))).toList(),
artifactKey.get());
artifactKey.get(), targetEnvironments);
getLog().debug("API baseline contains " + baselineBundles.size() + " bundles (resolve takes " + time(start)
+ ").");
} catch (IllegalArtifactReferenceException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.IllegalArtifactReferenceException;
import org.eclipse.tycho.MavenRepositoryLocation;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.core.resolver.P2ResolutionResult;
import org.eclipse.tycho.core.resolver.P2ResolutionResult.Entry;
Expand Down Expand Up @@ -59,8 +60,9 @@ public class ApiApplicationResolver {
private EclipseApplicationManager applicationManager;

public Collection<Path> getApiBaselineBundles(Collection<MavenRepositoryLocation> baselineRepoLocations,
ArtifactKey artifactKey) throws IllegalArtifactReferenceException {
P2Resolver resolver = applicationFactory.createResolver();
ArtifactKey artifactKey, Collection<TargetEnvironment> environment)
throws IllegalArtifactReferenceException {
P2Resolver resolver = applicationFactory.createResolver(environment);
resolver.addDependency(ArtifactType.TYPE_INSTALLABLE_UNIT, artifactKey.getId(), "0.0.0");
List<Path> resolvedBundles = new ArrayList<>();
TargetPlatform targetPlatform = applicationFactory.createTargetPlatform(baselineRepoLocations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.eclipse.tycho.helper.PluginRealmHelper;
import org.eclipse.tycho.model.project.EclipseProject;
import org.eclipse.tycho.targetplatform.TargetDefinition;
import org.osgi.framework.Filter;

import aQute.bnd.osgi.Processor;

Expand Down Expand Up @@ -176,6 +177,16 @@ public TargetPlatformConfiguration getTargetPlatformConfiguration(ReactorProject
return getTargetPlatformConfiguration(project.adapt(MavenProject.class));
}

public Collection<TargetEnvironment> getTargetEnvironments(MavenProject project) {
TychoProject tychoProject = projectTypes.get(project.getPackaging());
if (tychoProject != null) {
Filter environmentFilter = tychoProject.getTargetEnvironmentFilter(project);
return getTargetPlatformConfiguration(project).getEnvironments().stream()
.filter(te -> te.match(environmentFilter)).toList();
}
return List.of(TargetEnvironment.getRunningEnvironment());
}

public Optional<TychoProject> getTychoProject(MavenProject project) {
if (project == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj
List<TargetEnvironment> skipped = new ArrayList<>();
for (Xpp3Dom environmentDom : environmentsDom.getChildren("environment")) {
TargetEnvironment environment = newTargetEnvironment(environmentDom);
if (!matchFilter(environment, filter)) {
skipped.add(environment);
} else {
if (environment.match(filter)) {
result.addEnvironment(environment);
} else {
skipped.add(environment);
}
}
if (!skipped.isEmpty()) {
Expand All @@ -394,13 +394,6 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj
}
}

private static boolean matchFilter(TargetEnvironment environment, Filter filter) {
if (filter != null) {
return filter.matches(environment.toFilterProperties());
}
return true;
}

private static Filter getTargetEnvironmentFilter(TychoProject tychoProject, MavenProject project) {
if (tychoProject != null) {
return tychoProject.getTargetEnvironmentFilter(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.tycho.osgi.framework;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -97,8 +96,11 @@ public TargetPlatform createTargetPlatform(Collection<MavenRepositoryLocation> l
}

public P2Resolver createResolver() {
P2Resolver resolver = resolverFactory
.createResolver(Collections.singletonList(TargetEnvironment.getRunningEnvironment()));
return createResolver(List.of(TargetEnvironment.getRunningEnvironment()));
}

public P2Resolver createResolver(Collection<TargetEnvironment> environments) {
P2Resolver resolver = resolverFactory.createResolver(environments);
return resolver;
}

Expand Down

0 comments on commit dec71ae

Please sign in to comment.