Skip to content

Commit

Permalink
Resolves mojohaus#921: Refactoring segments; added the special case f…
Browse files Browse the repository at this point in the history
…or restrictionForIgnoreScope
  • Loading branch information
jarmoniuk committed May 19, 2023
1 parent a3e8410 commit c2cbbfd
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@
* under the License.
*/

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.artifact.versioning.*;
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Collections.reverseOrder;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;
import static org.codehaus.mojo.versions.api.Segment.*;

/**
* Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}.
Expand Down Expand Up @@ -69,17 +65,15 @@ public Restriction restrictionForUnchangedSegment(Optional<Segment> unchangedSeg
// one range spec can have multiple restrictions, and multiple 'lower bound', we want the highest one.
// [1.0,2.0),[3.0,4.0) -> 3.0
ArtifactVersion highestLowerBound = currentVersion;
if (currentVersion != null) {
try {
highestLowerBound =
VersionRange.createFromVersionSpec(currentVersion.toString()).getRestrictions().stream()
.map(Restriction::getLowerBound)
.filter(Objects::nonNull)
.max(getVersionComparator())
.orElse(currentVersion);
} catch (InvalidVersionSpecificationException ignored) {
ignored.printStackTrace(System.err);
}
assert highestLowerBound != null;
try {
highestLowerBound = VersionRange.createFromVersionSpec(currentVersion.toString()).getRestrictions().stream()
.map(Restriction::getLowerBound)
.filter(Objects::nonNull)
.max(getVersionComparator())
.orElse(currentVersion);
} catch (InvalidVersionSpecificationException e) {
e.printStackTrace(System.err);
}

final ArtifactVersion currentVersion = highestLowerBound;
Expand All @@ -92,18 +86,23 @@ public Restriction restrictionForUnchangedSegment(Optional<Segment> unchangedSeg
nextVersion,
false,
unchangedSegment
.filter(MAJOR::isGreaterThan)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(null),
false);
}

@Override
public Restriction restrictionForIgnoreScope(Optional<Segment> ignored) {
ArtifactVersion nextVersion = ignored
.map(Segment::ofGreaterThan)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(currentVersion);
public Restriction restrictionForIgnoreScope(Optional<Segment> ignoredSegment) {
ArtifactVersion nextVersion;
if (ignoredSegment.map(s -> s == MAJOR).orElse(false)) {
// special case: we will ignore all possible version updates
nextVersion = DefaultArtifactVersionCache.of(String.valueOf(Integer.MAX_VALUE));
} else {
nextVersion = ignoredSegment
.map(Segment::ofGreaterThan)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(currentVersion);
}
return new Restriction(nextVersion, false, null, false);
}

Expand Down Expand Up @@ -217,8 +216,7 @@ public final ArtifactVersion[] getNewerVersions(
.orElse(null)
: currentVersion;
ArtifactVersion upperBound = unchangedSegment
.map(s -> (ArtifactVersion) new BoundArtifactVersion(
currentVersion, s.isGreaterThan(SUBINCREMENTAL) ? Segment.of(s.value() + 1) : s))
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
.orElse(null);

Restriction restriction = new Restriction(lowerBound, allowDowngrade, upperBound, allowDowngrade);
Expand Down Expand Up @@ -317,7 +315,7 @@ public ArtifactVersion[] getAllUpdates(VersionRange versionRange, boolean includ
* implying that there is also no string designation of the lower bound version.
*
* @param version {@link ArtifactVersion} object specifying the version for which the lower bound is being computed
* @param unchangedSegment first segment not to be changed; empty() means anything can change
* @param unchangedSegment first segment not to be changed; empty() means any segment may change
* @return {@link Optional} string containing the lowest artifact version with the given segment held
* @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than
* the segment count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,18 @@
* under the License.
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.artifact.versioning.*;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;

import java.util.*;

import static java.util.Optional.empty;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;

/**
* Manages a property that is associated with one or more artifacts.
Expand Down Expand Up @@ -282,7 +268,7 @@ public ArtifactVersion getNewestVersion(
* @param reactorProjects collection of reactor projects
* @param helper VersionHelper object
* @param allowDowngrade whether downgrades should be allowed
* @param upperBoundSegment the upper bound segment; empty() means no upper bound
* @param unchangedSegment segment not to be changed; empty() means any segment may change
* @return newest artifact version fulfilling the criteria or null if no newer version could be found
* @throws InvalidSegmentException thrown if the {@code unchangedSegment} is not valid (e.g. greater than the number
* of segments in the version string)
Expand All @@ -295,7 +281,7 @@ public ArtifactVersion getNewestVersion(
Collection<MavenProject> reactorProjects,
VersionsHelper helper,
boolean allowDowngrade,
Optional<Segment> upperBoundSegment)
Optional<Segment> unchangedSegment)
throws InvalidSegmentException, InvalidVersionSpecificationException {
final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots;
helper.getLog().debug("getNewestVersion(): includeSnapshots='" + includeSnapshots + "'");
Expand All @@ -308,20 +294,17 @@ public ArtifactVersion getNewestVersion(

ArtifactVersion currentVersion = DefaultArtifactVersionCache.of(versionString);
ArtifactVersion lowerBound = allowDowngrade
? getLowerBound(currentVersion, upperBoundSegment)
? getLowerBound(currentVersion, unchangedSegment)
.map(DefaultArtifactVersionCache::of)
.orElse(null)
: currentVersion;
if (helper.getLog().isDebugEnabled()) {
helper.getLog().debug("lowerBoundArtifactVersion: " + lowerBound);
}

ArtifactVersion upperBound = !upperBoundSegment.isPresent()
? null
: upperBoundSegment
.map(s -> (ArtifactVersion) new BoundArtifactVersion(
currentVersion, s.isGreaterThan(SUBINCREMENTAL) ? Segment.of(s.value() + 1) : s))
.orElse(null);
ArtifactVersion upperBound = unchangedSegment
.map(s -> new BoundArtifactVersion(currentVersion, s))
.orElse(null);
if (helper.getLog().isDebugEnabled()) {
helper.getLog().debug("Property ${" + property.getName() + "}: upperBound is: " + upperBound);
}
Expand Down Expand Up @@ -350,7 +333,8 @@ public ArtifactVersion getNewestVersion(
}
}
}
if (fromReactor != null && (result != null || !currentVersion.equals(fromReactor.toString()))) {
if (fromReactor != null && (result != null || !String.valueOf(currentVersion)
.equals(fromReactor.toString()))) {
if (property.isPreferReactor()) {
helper.getLog()
.debug("Property ${" + property.getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public static Segment of(int index) {
* if the segment is already {@link #MAJOR}
*/
public static Segment ofGreaterThan(Segment other) {
return Optional.ofNullable(other)
.map(s -> of(s.value() - 1))
.orElse(null);
return Optional.ofNullable(other).map(s -> of(s.value() - 1)).orElse(null);
}

/**
Expand All @@ -70,9 +68,7 @@ public static Segment ofGreaterThan(Segment other) {
* @return that has a lesser scope than the given segment
*/
public static Segment ofLesserThan(Segment other) {
return Optional.ofNullable(other)
.map(s -> of(s.value() + 1))
.orElse(MAJOR);
return Optional.ofNullable(other).map(s -> of(s.value() + 1)).orElse(MAJOR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* under the License.
*/

import java.util.Optional;

import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.versions.api.Segment;
import org.junit.Test;

import static java.util.Optional.empty;
Expand Down Expand Up @@ -53,6 +57,25 @@ public void testMajor() {

@Test
public void testEmpty() {
assertThat(determineUnchangedSegment(true, true, true, null), is(empty()));
Optional<Segment> result;
boolean allowMinorUpdates = true;
boolean allowIncrementalUpdates = true;
Log log = null;
if (log != null && !allowIncrementalUpdates) {
log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false.");
}

Optional<Segment> unchangedSegment = allowIncrementalUpdates
? empty()
: allowMinorUpdates && allowIncrementalUpdates
? of(MAJOR)
: allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL);
if (log != null && log.isDebugEnabled()) {
log.debug(unchangedSegment
.map(s -> Segment.of(s.value() + 1).toString())
.orElse("ALL") + " version changes allowed");
}
result = unchangedSegment;
assertThat(result, is(empty()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
import org.codehaus.plexus.util.StringUtils;

import static java.util.Collections.emptySet;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.apache.commons.lang3.StringUtils.countMatches;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;
import static org.codehaus.mojo.versions.filtering.DependencyFilter.filterDependencies;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.*;

Expand Down Expand Up @@ -479,32 +476,25 @@ static void validateGAVList(List<String> gavList, int numSections, String argume
}
}

private Optional<Segment> calculateUpdateScope() {
return allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates
? empty()
: of(SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog())
.map(s -> Segment.of(s.value() + 1))
.orElse(MAJOR));
}

private void logUpdates(Map<Dependency, ArtifactVersions> updates, String section) {
List<String> withUpdates = new ArrayList<>();
List<String> usingCurrent = new ArrayList<>();
for (ArtifactVersions versions : updates.values()) {
String left = " " + ArtifactUtils.versionlessKey(versions.getArtifact()) + " ";
final String current;
ArtifactVersion latest;
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
if (versions.isCurrentVersionDefined()) {
current = versions.getCurrentVersion().toString();
latest = versions.getNewestUpdate(calculateUpdateScope(), allowSnapshots);
latest = versions.getNewestUpdate(unchangedSegment, allowSnapshots);
} else {
ArtifactVersion newestVersion =
versions.getNewestVersion(versions.getArtifact().getVersionRange(), allowSnapshots);
current = versions.getArtifact().getVersionRange().toString();
latest = newestVersion == null
? null
: versions.getNewestUpdate(newestVersion, calculateUpdateScope(), allowSnapshots);
: versions.getNewestUpdate(newestVersion, unchangedSegment, allowSnapshots);
if (latest != null
&& ArtifactVersions.isVersionInRange(
latest, versions.getArtifact().getVersionRange())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
Expand All @@ -42,7 +43,10 @@
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.SegmentUtils;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.codehaus.mojo.versions.api.Segment.*;

/**
* Displays properties that are linked to artifact versions and have updates available.
Expand Down Expand Up @@ -168,8 +172,26 @@ public void execute() throws MojoExecutionException, MojoFailureException {
continue;
}

Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
Log log = getLog();
if (log != null && !allowIncrementalUpdates) {
log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false.");
}

if (log != null && !allowMinorUpdates) {
log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false.");
}

Optional<Segment> unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates
? empty()
: allowMinorUpdates && allowIncrementalUpdates
? of(MAJOR)
: allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL);
if (log != null && log.isDebugEnabled()) {
log.debug(unchangedSegment1
.map(s -> Segment.of(s.value() + 1).toString())
.orElse("ALL") + " version changes allowed");
}
Optional<Segment> unchangedSegment = unchangedSegment1;
try {
ArtifactVersion winner = version.getNewestVersion(
currentVersion,
Expand Down
Loading

0 comments on commit c2cbbfd

Please sign in to comment.