Skip to content

Commit

Permalink
Further refactoring: replacing segment index with the Segment enum
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 authored and slawekjaranowski committed Sep 18, 2022
1 parent fc5f092 commit 29394d9
Show file tree
Hide file tree
Showing 23 changed files with 206 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.text.CaseUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
Expand All @@ -55,6 +57,7 @@
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.model.RuleSet;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
Expand All @@ -67,6 +70,12 @@
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.stax2.XMLInputFactory2;

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;

/**
* Abstract base class for Versions Mojos.
*
Expand Down Expand Up @@ -568,43 +577,33 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A
* @param allowIncrementalUpdates Allow incremental updates
* @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1.
*/
protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
boolean allowIncrementalUpdates )
protected Optional<Segment> determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
boolean allowIncrementalUpdates )
{
int segment;
if ( allowMajorUpdates )
{
segment = -1;
getLog().info( "Major version changes allowed" );
}
else if ( allowMinorUpdates )
{
segment = 0;
getLog().info( "Minor version changes allowed" );
}
else if ( allowIncrementalUpdates )
{
segment = 1;
getLog().info( "Incremental version changes allowed" );
}
else
Optional<Segment> unchangedSegment = allowMajorUpdates ? empty()
: allowMinorUpdates ? of( MAJOR )
: allowIncrementalUpdates ? of( MINOR )
: of( INCREMENTAL );
if ( getLog().isInfoEnabled() )
{
segment = 2;
getLog().info( "Subincremental version changes allowed" );
getLog().info(
unchangedSegment.map( s ->
CaseUtils.toCamelCase( Segment.of( s.value() + 1 ).toString(), true ) )
.orElse( "All" ) + " version changes allowed" );
}

return segment;
return unchangedSegment;
}

protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
PropertyVersions version, String currentVersion,
boolean allowDowngrade, int segment )
boolean allowDowngrade,
Optional<Segment> unchangedSegment )
throws XMLStreamException, InvalidVersionSpecificationException,
InvalidSegmentException, MojoExecutionException
{
ArtifactVersion winner =
version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,
this.getHelper(), allowDowngrade, segment );
this.getHelper(), allowDowngrade, unchangedSegment );

if ( winner == null || currentVersion.equals( winner.toString() ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeSet;

import org.apache.maven.artifact.manager.WagonManager;
Expand All @@ -40,6 +41,7 @@
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

Expand Down Expand Up @@ -155,11 +157,12 @@ public void execute()
continue;
}

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment =
determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
try
{
ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots,
this.reactorProjects, this.getHelper(), false, segment );
this.reactorProjects, this.getHelper(), false, unchangedSegment );
if ( winner != null && !currentVersion.equals( winner.toString() ) )
{
StringBuilder buf = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -44,6 +45,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

Expand Down Expand Up @@ -310,11 +312,12 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )

property.setVersion( currentVersion );

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
// TODO: Check if we could add allowDowngrade ?
try
{
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment );
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, unchangedSegment );
}
catch ( InvalidSegmentException | InvalidVersionSpecificationException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.xml.stream.XMLStreamException;

import java.util.Map;
import java.util.Optional;

import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
Expand All @@ -37,6 +38,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

Expand Down Expand Up @@ -179,13 +181,13 @@ protected void update( ModifiedPomXMLEventReader pom )

if ( canUpdateProperty )
{
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
try
{
ArtifactVersion targetVersion =
updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );
allowDowngrade, unchangedSegment );

if ( targetVersion != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.xml.stream.XMLStreamException;

import java.util.Map;
import java.util.Optional;

import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
Expand All @@ -37,6 +38,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactAssociation;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

Expand Down Expand Up @@ -163,11 +165,12 @@ protected void update( ModifiedPomXMLEventReader pom )
continue;
}

int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment =
determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
try
{
ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion,
allowDowngrade, segment );
allowDowngrade, unchangedSegment );

if ( targetVersion != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -47,6 +48,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
Expand Down Expand Up @@ -147,7 +149,8 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
MajorMinorIncrementalFilter majorMinorIncfilter =
new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );

Expand Down Expand Up @@ -182,7 +185,7 @@ private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection<Depend
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
try
{
ArtifactVersion[] newer = versions.getNewerVersions( version, segment, false );
ArtifactVersion[] newer = versions.getNewerVersions( version, unchangedSegment, false );
newer = filterVersionsWithIncludes( newer, artifact );

ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newer );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -45,13 +46,15 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static java.util.Collections.singletonList;
import static org.codehaus.mojo.versions.api.Segment.MAJOR;

/**
* Replaces any release versions with the latest snapshot version (if it has been deployed).
Expand Down Expand Up @@ -146,7 +149,8 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
MajorMinorIncrementalFilter majorMinorIncfilter =
new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );

Expand Down Expand Up @@ -180,15 +184,18 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection<Depen
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
final VersionComparator versionComparator = versions.getVersionComparator();
final DefaultArtifactVersion lowerBound = new DefaultArtifactVersion( version );
if ( segment + 1 > versionComparator.getSegmentCount( lowerBound ) )
if ( unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= versionComparator.getSegmentCount( lowerBound ) )
{
getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" );
continue;
}
try
{
ArtifactVersion upperBound =
segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null;
ArtifactVersion upperBound = unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= MAJOR.value()
? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() )
: null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.util.Collection;
import java.util.Optional;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
Expand All @@ -42,6 +43,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
Expand Down Expand Up @@ -163,7 +165,7 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
int unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
Optional<Segment> unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );
MajorMinorIncrementalFilter majorMinorIncfilter =
new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -44,10 +45,13 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static org.codehaus.mojo.versions.api.Segment.MAJOR;

/**
* Replaces any release versions with the next snapshot version (if it has been deployed).
*
Expand Down Expand Up @@ -132,7 +136,9 @@ protected void update( ModifiedPomXMLEventReader pom )
private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException
{
int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates );
Optional<Segment>
unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates,
allowIncrementalUpdates );

for ( Dependency dep : dependencies )
{
Expand Down Expand Up @@ -162,15 +168,18 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection<Depende
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
final VersionComparator versionComparator = versions.getVersionComparator();
final DefaultArtifactVersion lowerBound = new DefaultArtifactVersion( version );
if ( segment + 1 > versionComparator.getSegmentCount( lowerBound ) )
if ( unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= versionComparator.getSegmentCount( lowerBound ) )
{
getLog().info( "Ignoring " + toString( dep ) + " as the version number is too short" );
continue;
}
try
{
ArtifactVersion upperBound =
segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null;
ArtifactVersion upperBound = unchangedSegment.isPresent()
&& unchangedSegment.get().value() >= MAJOR.value()
? versionComparator.incrementSegment( lowerBound, unchangedSegment.get() )
: null;
getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) );
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );
ArtifactVersion[] newer = versions.getVersions( restriction, true );
Expand Down
Loading

0 comments on commit 29394d9

Please sign in to comment.