Skip to content

Commit

Permalink
Resolves mojohaus#921: Refactoring segments; WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed May 21, 2023
1 parent fd68edb commit 603c917
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ public Restriction restrictionFor(Optional<Segment> scope) throws InvalidSegment
}

final ArtifactVersion currentVersion = highestLowerBound;
ArtifactVersion nextVersion = scope.filter(s -> s.isMajorTo(SUBINCREMENTAL))
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, Segment.of(s.value() + 1)))
.orElse(currentVersion);
return new Restriction(
nextVersion,
currentVersion,
false,
scope.filter(MAJOR::isMajorTo)
.map(s -> (ArtifactVersion) new BoundArtifactVersion(currentVersion, s))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* under the License.
*/

import java.util.Optional;

/**
* Indicates the segment along with its 0-based index
*
Expand All @@ -40,12 +42,35 @@ public int value() {
}

public static Segment of(int index) {
if (index < 0 || index > 3) {
if (index < 0) {
return null;
}
if (index > 3) {
throw new IllegalArgumentException("Wrong segment index: " + index);
}
return values()[index];
}

/**
* Creates a segment that has a greater scope than the given segment or {@code null}
* if the segment is already {@link #MAJOR}
* @param other segment that the new segment is to be based on
* @return that has a greater scope than the given segment or {@code null}
* if the segment is already {@link #MAJOR}
*/
public static Segment majorTo(Segment other) {
return Optional.ofNullable(other).map(s -> of(s.value() - 1)).orElse(null);
}

/**
* Creates a segment that has a lesser scope than the given segment
* @param other segment that the new segment is to be based on
* @return that has a lesser scope than the given segment
*/
public static Segment minorTo(Segment other) {
return Optional.ofNullable(other).map(s -> of(s.value() + 1)).orElse(MAJOR);
}

/**
* Returns true if the given segment is more major than the other
* @param other other segment to compare
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@

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

/**
* Utility class for manipulating with {@link Segment} objects
Expand Down Expand Up @@ -61,7 +59,9 @@ public class SegmentUtils {
* @param allowIncrementalUpdates if major and minor updates are disallowed, incremental updates are allowed
* @param log If not null, the {@linkplain Log} object to log the selected scope
* @return Returns the segment (0-based) that is unchangeable. If any segment can change, returns -1.
* @deprecated please use {@link #determineUpdateScope} instead
*/
@Deprecated
public static Optional<Segment> determineUnchangedSegment(
boolean allowMajorUpdates, boolean allowMinorUpdates, boolean allowIncrementalUpdates, Log log) {
if (log != null && !allowIncrementalUpdates) {
Expand All @@ -84,4 +84,53 @@ public static Optional<Segment> determineUnchangedSegment(
}
return unchangedSegment;
}

/**
* <p>Based on the passed flags, determines the most major segment to receive updates.</p>
*
* <p>Also, logs the enriched values of the {@code allowMajorUpdates}, {@code allowMinorUpdates},
* and {@code allowIncrementalUpdates} options so that {@code allowMinorUpdates} equal to {@code false}
* implies that {@code allowMajorUpdates} is also {@code false}.</p>
* <p>Also, {@code allowIncrementalUpdates} equal to {@code false}
* implies that both {@code allowMajorUpdates} and {@code allowMinorUpdates} are also {@code false}.</p>
*
* <table>
* <caption>Effective values for update options</caption>
* <thead>
* <tr><th>allowMajorUpdates</th><th>allowMinorUpdates</th><th>allowIncrementalUpdates</th></tr>
* </thead>
* <tbody>
* <tr><td>true</td><td>true</td><td>true</td></tr>
* <tr><td></td><td>true</td><td>true</td></tr>
* <tr><td></td><td></td><td>true</td></tr>
* <tr><td></td><td></td><td></td></tr>
* </tbody>
* </table>
*
* @param allowMajorUpdates whether all updates should be allowed
* @param allowMinorUpdates if major updates are disallowed, minor, incremental updates should be allowed
* @param allowIncrementalUpdates if major and minor updates are disallowed, incremental updates are allowed
* @param log If not null, the {@linkplain Log} object to log the selected scope
* @return Returns the segment that is to be updated. If any segment can change, returns Segment.MAJOR
*/
public static Segment determineUpdateScope(
boolean allowMajorUpdates, boolean allowMinorUpdates, boolean allowIncrementalUpdates, Log log) {
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.");
}

Segment updateScope = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates
? MAJOR
: allowMinorUpdates && allowIncrementalUpdates
? MINOR
: allowIncrementalUpdates ? INCREMENTAL : SUBINCREMENTAL;
if (log != null && log.isDebugEnabled()) {
log.debug(updateScope + " version changes allowed");
}
return updateScope;
}
}
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,8 @@
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 @@ -210,9 +208,6 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo {
/**
* Whether to allow the major version number to be changed.
*
* <p><b>Note: {@code false} also implies {@linkplain #allowAnyUpdates}
* to be {@code false}</b></p>
*
* @since 2.5
*/
@Parameter(property = "allowMajorUpdates", defaultValue = "true")
Expand All @@ -221,8 +216,7 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo {
/**
* <p>Whether to allow the minor version number to be changed.</p>
*
* <p><b>Note: {@code false} also implies {@linkplain #allowAnyUpdates}
* and {@linkplain #allowMajorUpdates} to be {@code false}</b></p>
* <p><b>Note: {@code false} also implies {@linkplain #allowMajorUpdates} to be {@code false}</b></p>
*
* @since 2.5
*/
Expand All @@ -232,32 +226,14 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo {
/**
* <p>Whether to allow the incremental version number to be changed.</p>
*
* <p><b>Note: {@code false} also implies {@linkplain #allowAnyUpdates},
* {@linkplain #allowMajorUpdates}, and {@linkplain #allowMinorUpdates}
* to be {@code false}</b></p>
* <p><b>Note: {@code false} also implies {@linkplain #allowMajorUpdates}
* and {@linkplain #allowMinorUpdates} to be {@code false}</b></p>
*
* @since 2.5
*/
@Parameter(property = "allowIncrementalUpdates", defaultValue = "true")
private boolean allowIncrementalUpdates = true;

/**
* <p>Ignored -- largely replaced by {@linkplain #allowMajorUpdates},
* {@linkplain #allowMinorUpdates} and {@linkplain #allowIncrementalUpdates},
* which are equal to {@code true} by default.</p>
*
* <p><b>Please note: Prior to version 2.16.0, leaving this parameter at its default
* value ({@code true}) would mean that the plugin would <u>ignore</u>
* {@linkplain #allowMajorUpdates}, {@linkplain #allowMinorUpdates}, and {@linkplain #allowIncrementalUpdates},
* which confused many users.</b></p>
*
* @since 2.5
* @deprecated This will be removed with version 3.0.0
*/
@Deprecated
@Parameter(property = "allowAnyUpdates", defaultValue = "true")
private boolean allowAnyUpdates = true;

/**
* Whether to show additional information such as dependencies that do not need updating. Defaults to false.
*
Expand Down Expand Up @@ -488,11 +464,6 @@ protected void validateInput() throws MojoExecutionException {
validateGAVList(pluginDependencyExcludes, 3, "pluginDependencyExcludes");
validateGAVList(pluginManagementDependencyIncludes, 3, "pluginManagementDependencyIncludes");
validateGAVList(pluginManagementDependencyExcludes, 3, "pluginManagementDependencyExcludes");
if (getLog() != null
&& allowAnyUpdates
&& !(allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates)) {
getLog().warn("Assuming allowAnyUpdates false because one or more other \"allow\" switches is false.");
}
}

/**
Expand All @@ -509,32 +480,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;
Segment updateScope = SegmentUtils.determineUpdateScope(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
if (versions.isCurrentVersionDefined()) {
current = versions.getCurrentVersion().toString();
latest = versions.getNewestUpdate(calculateUpdateScope(), allowSnapshots);
latest = versions.getNewestUpdate(of(updateScope), 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, of(updateScope), 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.maven.model.Dependency;
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 @@ -48,7 +49,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.*;

/**
* Attempts to resolve dependency version ranges to the specific version being used in the build. For example a version
Expand Down Expand Up @@ -288,8 +292,26 @@ private void resolvePropertyRanges(ModifiedPomXMLEventReader pom)

property.setVersion(currentVersion);

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;
// TODO: Check if we could add allowDowngrade ?
try {
updatePropertyToNewestVersion(pom, property, version, currentVersion, false, unchangedSegment);
Expand Down
Loading

0 comments on commit 603c917

Please sign in to comment.