Skip to content

Commit

Permalink
Support specify a version range
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Apr 7, 2022
1 parent 89ab234 commit 582f1d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 SAP SE and others.
* Copyright (c) 2014, 2022 SAP SE 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* SAP SE - initial API and implementation
* Christoph Läubrich - Issue #845 - Feature restrictions are not taken into account when using emptyVersion
*******************************************************************************/
package org.eclipse.tycho.p2.target;

Expand All @@ -24,15 +25,14 @@

public class ArtifactMatcher {

public static IInstallableUnit resolveReference(String type, String id, Version version,
public static IInstallableUnit resolveReference(String type, String id, VersionRange versionRange,
LinkedHashSet<IInstallableUnit> candidateUnits) throws IllegalArtifactReferenceException {
if (id == null) {
throw new IllegalArtifactReferenceException("ID is required");
}

VersionRange versionRange = getVersionRangeFromReference(version);
IQuery<IInstallableUnit> query = QueryUtil.createLatestQuery(ArtifactTypeHelper.createQueryFor(type, id,
versionRange));
IQuery<IInstallableUnit> query = QueryUtil
.createLatestQuery(ArtifactTypeHelper.createQueryFor(type, id, versionRange));

IQueryResult<IInstallableUnit> matchingIUs = query.perform(candidateUnits.iterator());
if (matchingIUs.isEmpty()) {
Expand All @@ -43,14 +43,17 @@ public static IInstallableUnit resolveReference(String type, String id, Version
}

public static Version parseAsOSGiVersion(String version) throws IllegalArtifactReferenceException {
if (version == null) {
return Version.emptyVersion;
}
try {
return Version.parseVersion(version);
} catch (IllegalArgumentException e) {
throw new IllegalArtifactReferenceException("The string \"" + version + "\" is not a valid OSGi version");
}
}

private static VersionRange getVersionRangeFromReference(Version version) {
public static VersionRange getVersionRangeFromReference(Version version) {
VersionRange range;
if (version.getSegmentCount() > 3 && "qualifier".equals(version.getSegment(3))) {
range = getRangeOfEquivalentVersions(version);
Expand All @@ -74,8 +77,8 @@ private static VersionRange getRangeOfEquivalentVersions(Version version) {
Integer major = (Integer) version.getSegment(0);
Integer minor = (Integer) version.getSegment(1);
Integer micro = (Integer) version.getSegment(2);
VersionRange range = new VersionRange(Version.createOSGi(major, minor, micro), true, Version.createOSGi(major,
minor, micro + 1), false);
VersionRange range = new VersionRange(Version.createOSGi(major, minor, micro), true,
Version.createOSGi(major, minor, micro + 1), false);
return range;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2015 SAP SE and others.
* Copyright (c) 2011, 2022 SAP SE 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* SAP SE - initial API and implementation
* Christoph Läubrich - Issue #845 - Feature restrictions are not taken into account when using emptyVersion
*******************************************************************************/
package org.eclipse.tycho.p2.target;

Expand All @@ -20,6 +21,7 @@
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.artifacts.DependencyResolutionException;
Expand Down Expand Up @@ -82,7 +84,10 @@ public interface P2TargetPlatform extends TargetPlatform {
* Note: "artifact" in "resolveArtifact" refers to a Tycho artifact, which technically represent
* a p2 installable unit and optionally the associated p2 artifact.
*/
IInstallableUnit resolveUnit(String type, String id, Version version) throws IllegalArtifactReferenceException,
DependencyResolutionException;
IInstallableUnit resolveUnit(String type, String id, Version version)
throws IllegalArtifactReferenceException, DependencyResolutionException;

IInstallableUnit resolveUnit(String type, String id, VersionRange versionRange)
throws IllegalArtifactReferenceException, DependencyResolutionException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2014 SAP SE and others.
* Copyright (c) 2011, 2022 SAP SE 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* SAP SE - initial API and implementation
* Christoph Läubrich - Issue #845 - Feature restrictions are not taken into account when using emptyVersion
*******************************************************************************/
package org.eclipse.tycho.p2.target;

Expand All @@ -22,6 +23,7 @@
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.metadata.VersionRange;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.artifacts.DependencyResolutionException;
Expand Down Expand Up @@ -90,23 +92,29 @@ public final Set<IInstallableUnit> getInstallableUnits() {
@Override
public final org.eclipse.tycho.ArtifactKey resolveArtifact(String type, String id, String version)
throws IllegalArtifactReferenceException, DependencyResolutionException {
IInstallableUnit resolvedUnit = resolveUnit(type, id, ArtifactMatcher.parseAsOSGiVersion(version));
IInstallableUnit resolvedUnit;
if (version != null && (version.startsWith("[") || version.startsWith("("))) {
resolvedUnit = resolveUnit(type, id, VersionRange.create(version));
} else {
resolvedUnit = resolveUnit(type, id, ArtifactMatcher.parseAsOSGiVersion(version));
}
return new DefaultArtifactKey(type, id, resolvedUnit.getVersion().toString());
}

@Override
public final IInstallableUnit resolveUnit(String type, String id, Version version)
throws IllegalArtifactReferenceException, DependencyResolutionException {
VersionRange versionRange = ArtifactMatcher.getVersionRangeFromReference(version);
return resolveUnit(type, id, versionRange);
}

IInstallableUnit matchingUnit = ArtifactMatcher.resolveReference(type, id, version, installableUnits);
@Override
public IInstallableUnit resolveUnit(String type, String id, VersionRange versionRange)
throws IllegalArtifactReferenceException, DependencyResolutionException {
IInstallableUnit matchingUnit = ArtifactMatcher.resolveReference(type, id, versionRange, installableUnits);
if (matchingUnit == null) {
String message;
if (version == null) {
message = type + " artifact with ID \"" + id + "\" was not found in the target platform";
} else {
message = type + " artifact with ID \"" + id + "\" and version matching \"" + version
+ "\" was not found in the target platform";
}
String message = type + " artifact with ID \"" + id + "\" and version matching \"" + versionRange
+ "\" was not found in the target platform";
String candidates = installableUnits.stream()
.sorted(Comparator.comparing(IInstallableUnit::getId).thenComparing(IInstallableUnit::getVersion))
.filter(iu -> iu.getId().contains(id)).map(iu -> iu.getId() + ":" + iu.getVersion())
Expand Down

0 comments on commit 582f1d9

Please sign in to comment.