Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1182: Bug fix (update scope determination) + slight refactoring #1183

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,29 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.Segment;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.eclipse.aether.RepositorySystem;

import static java.util.Optional.empty;

/**
* Abstract base class for the Display___ mojos.
*
Expand All @@ -46,6 +58,13 @@ public abstract class AbstractVersionsDisplayMojo extends AbstractVersionsUpdate

private static final int DEFAULT_OUTPUT_LINE_WIDTH = 80;

/**
* The width to pad info messages.
*
* @since 1.0-alpha-1
*/
static final int INFO_PAD_SIZE = 72;

/**
* If specified then the display output will be sent to the specified file.
*
Expand Down Expand Up @@ -161,4 +180,88 @@ protected void logLine(boolean error, String line) {
protected int getOutputLineWidthOffset() {
return this.outputLineWidth - DEFAULT_OUTPUT_LINE_WIDTH;
}

/**
* Defines the list of dependencies using current versions and the list of dependencies having updates
*/
protected interface DependencyUpdatesResult {

/**
* @return Dependencies using the latest version
*/
List<String> getUsingLatest();

/**
* @return Dependencies with updates available
*/
List<String> getWithUpdates();
}

/**
* Compiles a {@link DependencyUpdatesResult} object containing dependency updates for the given dependency map
* and the given unchanged segment.
* @param updates map of available versions per dependency
* @param unchangedSegment the most major segment not allowed to be updated or {@code Optional.empty()} if
* all segments are allowed to be updated
* @return a {@link DependencyUpdatesResult} object containing the result
*/
protected DependencyUpdatesResult getDependencyUpdates(
Map<Dependency, ArtifactVersions> updates, Optional<Segment> unchangedSegment) {
List<String> withUpdates = new ArrayList<>();
List<String> usingCurrent = new ArrayList<>();
for (ArtifactVersions versions : updates.values()) {
String left = " " + ArtifactUtils.versionlessKey(versions.getArtifact()) + " ";
String currentVersion;
Optional<ArtifactVersion> latestVersion;
if (versions.getCurrentVersion() != null) {
currentVersion = versions.getCurrentVersion().toString();
try {
latestVersion = versions.getNewestVersion(currentVersion, unchangedSegment, allowSnapshots, false);
} catch (InvalidSegmentException e) {
latestVersion = empty();
}
} else {
currentVersion = versions.getArtifact().getVersionRange().toString();
ArtifactVersion actualVersion =
versions.getNewestVersion(versions.getArtifact().getVersionRange(), allowSnapshots);
Restriction newVersionRestriction;
try {
Restriction segmentRestriction =
versions.restrictionForUnchangedSegment(actualVersion, unchangedSegment, false);
newVersionRestriction = new Restriction(
actualVersion,
false,
segmentRestriction.getUpperBound(),
segmentRestriction.isUpperBoundInclusive());
} catch (InvalidSegmentException e) {
throw new RuntimeException(e);
}
latestVersion = Optional.of(newVersionRestriction)
.map(restriction -> versions.getNewestVersion(restriction, allowSnapshots));
}
String right =
" " + latestVersion.map(v -> currentVersion + " -> " + v).orElse(currentVersion);
List<String> t = latestVersion.isPresent() ? withUpdates : usingCurrent;
if (right.length() + left.length() + 3 > INFO_PAD_SIZE + getOutputLineWidthOffset()) {
t.add(left + "...");
t.add(StringUtils.leftPad(right, INFO_PAD_SIZE + getOutputLineWidthOffset()));

} else {
t.add(StringUtils.rightPad(left, INFO_PAD_SIZE + getOutputLineWidthOffset() - right.length(), ".")
+ right);
}
}

return new DependencyUpdatesResult() {
@Override
public List<String> getUsingLatest() {
return usingCurrent;
}

@Override
public List<String> getWithUpdates() {
return withUpdates;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import javax.inject.Inject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -30,10 +29,7 @@
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -45,14 +41,12 @@
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.filtering.WildcardMatcher;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.MutableXMLStreamReader;
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.mojo.versions.utils.SegmentUtils;
import org.eclipse.aether.RepositorySystem;

import static java.util.Collections.emptySet;
import static java.util.Optional.empty;
import static org.codehaus.mojo.versions.filtering.DependencyFilter.filterDependencies;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.extractDependenciesFromDependencyManagement;
import static org.codehaus.mojo.versions.utils.MavenProjectUtils.extractDependenciesFromPlugins;
Expand All @@ -71,13 +65,6 @@ public class DisplayDependencyUpdatesMojo extends AbstractVersionsDisplayMojo {

// ------------------------------ FIELDS ------------------------------

/**
* The width to pad info messages.
*
* @since 1.0-alpha-1
*/
private static final int INFO_PAD_SIZE = 72;

/**
* Whether to process the dependencyManagement section of the project.
*
Expand Down Expand Up @@ -503,79 +490,32 @@ static void validateGAVList(List<String> gavList, int numSections, String argume
}
}

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()) + " ";
String currentVersion;
Optional<ArtifactVersion> latestVersion;
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
if (versions.getCurrentVersion() != null) {
currentVersion = versions.getCurrentVersion().toString();
try {
latestVersion = versions.getNewestVersion(currentVersion, unchangedSegment, allowSnapshots, false);
} catch (InvalidSegmentException e) {
latestVersion = empty();
}
} else {
currentVersion = versions.getArtifact().getVersionRange().toString();
ArtifactVersion actualVersion =
versions.getNewestVersion(versions.getArtifact().getVersionRange(), allowSnapshots);
Restriction newVersionRestriction;
try {
Restriction segmentRestriction =
versions.restrictionForUnchangedSegment(actualVersion, unchangedSegment, false);
newVersionRestriction = new Restriction(
actualVersion,
false,
segmentRestriction.getUpperBound(),
segmentRestriction.isUpperBoundInclusive());
} catch (InvalidSegmentException e) {
throw new RuntimeException(e);
}
latestVersion = Optional.of(newVersionRestriction)
.map(restriction -> versions.getNewestVersion(restriction, allowSnapshots));
}
String right =
" " + latestVersion.map(v -> currentVersion + " -> " + v).orElse(currentVersion);
List<String> t = latestVersion.isPresent() ? withUpdates : usingCurrent;
if (right.length() + left.length() + 3 > INFO_PAD_SIZE + getOutputLineWidthOffset()) {
t.add(left + "...");
t.add(StringUtils.leftPad(right, INFO_PAD_SIZE + getOutputLineWidthOffset()));

} else {
t.add(StringUtils.rightPad(left, INFO_PAD_SIZE + getOutputLineWidthOffset() - right.length(), ".")
+ right);
}
}
private void logUpdates(Map<Dependency, ArtifactVersions> versionMap, String section) {
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
DependencyUpdatesResult updates = getDependencyUpdates(versionMap, unchangedSegment);

if (isVerbose()) {
if (usingCurrent.isEmpty()) {
if (!withUpdates.isEmpty()) {
if (updates.getUsingLatest().isEmpty()) {
if (!updates.getWithUpdates().isEmpty()) {
logLine(false, "No dependencies in " + section + " are using the newest version.");
logLine(false, "");
}
} else {
logLine(false, "The following dependencies in " + section + " are using the newest version:");
for (String s : usingCurrent) {
logLine(false, s);
}
updates.getUsingLatest().forEach(s -> logLine(false, s));
logLine(false, "");
}
}

if (withUpdates.isEmpty()) {
if (!usingCurrent.isEmpty()) {
if (updates.getWithUpdates().isEmpty()) {
if (!updates.getUsingLatest().isEmpty()) {
logLine(false, "No dependencies in " + section + " have newer versions.");
logLine(false, "");
}
} else {
logLine(false, "The following dependencies in " + section + " have newer versions:");
for (String withUpdate : withUpdates) {
logLine(false, withUpdate);
}
updates.getWithUpdates().forEach(s -> logLine(false, s));
logLine(false, "");
}
}
Expand Down
Loading