Skip to content

Commit

Permalink
Extract DependencyVersionSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Feb 19, 2024
1 parent fc4714a commit ce6e1f3
Show file tree
Hide file tree
Showing 6 changed files with 337 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.gradle.dependency.DependencyVersionSelector;
import org.openrewrite.gradle.internal.InsertDependencyComparator;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
Expand All @@ -38,7 +39,6 @@
import org.openrewrite.maven.internal.MavenPomDownloader;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.*;
import org.openrewrite.semver.*;
import org.openrewrite.tree.ParseError;

import java.util.*;
Expand Down Expand Up @@ -231,8 +231,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
resolvedVersion = version;
} else {
try {
resolvedVersion = resolveDependencyVersion(groupId, artifactId, "0", version, versionPattern, gp.getMavenRepositories(), metadataFailures, ctx)
.orElse(null);
resolvedVersion = new DependencyVersionSelector(metadataFailures, gp)
.select(new GroupArtifact(groupId, artifactId), configuration, version, versionPattern, ctx);
} catch (MavenDownloadingException e) {
return e.warn(m);
}
Expand Down Expand Up @@ -365,42 +365,4 @@ private DependencyStyle autodetectDependencyStyle(List<Statement> statements) {

return string >= map ? DependencyStyle.String : DependencyStyle.Map;
}

public static Optional<String> resolveDependencyVersion(String groupId, String artifactId, String currentVersion, @Nullable String newVersion, @Nullable String versionPattern,
List<MavenRepository> repositories, @Nullable MavenMetadataFailures metadataFailures, ExecutionContext ctx) throws MavenDownloadingException {
VersionComparator versionComparator = StringUtils.isBlank(newVersion) ?
new LatestRelease(versionPattern) :
requireNonNull(Semver.validate(newVersion, versionPattern).getValue());

Optional<String> version;
if (versionComparator instanceof ExactVersion) {
version = versionComparator.upgrade(currentVersion, singletonList(newVersion));
} else if (versionComparator instanceof LatestPatch && !versionComparator.isValid(currentVersion, currentVersion)) {
// in the case of "latest.patch", a new version can only be derived if the
// current version is a semantic version
return Optional.empty();
} else {
version = findNewerVersion(groupId, artifactId, currentVersion, versionComparator, repositories, metadataFailures, ctx);
}
return version;
}

private static Optional<String> findNewerVersion(String groupId, String artifactId, String version, VersionComparator versionComparator,
List<MavenRepository> repositories, @Nullable MavenMetadataFailures metadataFailures, ExecutionContext ctx) throws MavenDownloadingException {
try {
MavenMetadata mavenMetadata = metadataFailures == null ?
downloadMetadata(groupId, artifactId, repositories, ctx) :
metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, repositories, ctx));
return versionComparator.upgrade(version, mavenMetadata.getVersioning().getVersions());
} catch (IllegalStateException e) {
// this can happen when we encounter exotic versions
return Optional.empty();
}
}

private static MavenMetadata downloadMetadata(String groupId, String artifactId, List<MavenRepository> repositories, ExecutionContext ctx) throws MavenDownloadingException {
return new MavenPomDownloader(ctx)
.downloadMetadata(new GroupArtifact(groupId, artifactId), null,
repositories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.gradle.dependency.DependencyVersionSelector;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
import org.openrewrite.gradle.marker.GradleProject;
import org.openrewrite.gradle.search.FindGradleProject;
Expand All @@ -34,8 +35,8 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.tree.GroupArtifact;
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
import org.openrewrite.semver.DependencyMatcher;
import org.openrewrite.semver.Semver;
Expand Down Expand Up @@ -83,22 +84,23 @@ public class ChangeDependency extends Recipe {

@Option(displayName = "Version pattern",
description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," +
"Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre",
"Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre",
example = "-jre",
required = false)
@Nullable
String versionPattern;

@Option(displayName = "Override managed version",
description = "If the old dependency has a managed version, this flag can be used to explicitly set the version on the new dependency. " +
"WARNING: No check is done on the NEW dependency to verify if it is managed, it relies on whether the OLD dependency had a managed version. " +
"The default for this flag is `false`.",
"WARNING: No check is done on the NEW dependency to verify if it is managed, it relies on whether the OLD dependency had a managed version. " +
"The default for this flag is `false`.",
required = false)
@Nullable
Boolean overrideManagedVersion;

/**
* Keeping this constructor just for compatibility purposes
*
* @deprecated Use {@link ChangeDependency#ChangeDependency(String, String, String, String, String, String, Boolean)}
*/
@Deprecated
Expand Down Expand Up @@ -187,8 +189,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
if (depArgs.get(0) instanceof J.Literal || depArgs.get(0) instanceof G.GString || depArgs.get(0) instanceof G.MapEntry) {
m = updateDependency(m, ctx);
} else if (depArgs.get(0) instanceof J.MethodInvocation &&
(((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("platform") ||
((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("enforcedPlatform"))) {
(((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("platform") ||
((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("enforcedPlatform"))) {
m = m.withArguments(ListUtils.mapFirst(depArgs, platform -> updateDependency((J.MethodInvocation) platform, ctx)));
}

Expand All @@ -210,13 +212,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
updated = updated.withArtifactId(newArtifactId);
}
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) {
List<MavenRepository> repositories = "classpath".equals(m.getSimpleName()) ?
gradleProject.getMavenPluginRepositories() :
gradleProject.getMavenRepositories();
String resolvedVersion;
try {
resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updated.getGroupId(), updated.getArtifactId(), "0", newVersion, versionPattern, repositories, null, ctx)
.orElse(null);
resolvedVersion = new DependencyVersionSelector(null, gradleProject)
.select(new GroupArtifact(updated.getGroupId(), updated.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx);
} catch (MavenDownloadingException e) {
return e.warn(m);
}
Expand All @@ -233,9 +232,11 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
} else if (m.getArguments().get(0) instanceof G.GString) {
G.GString gstring = (G.GString) depArgs.get(0);
List<J> strings = gstring.getStrings();
if (strings.size() >= 2 && strings.get(0) instanceof J.Literal) {
if (strings.size() >= 2 && strings.get(0) instanceof J.Literal &&
((J.Literal) strings.get(0)).getValue() != null) {

J.Literal literal = (J.Literal) strings.get(0);
Dependency original = DependencyStringNotationConverter.parse((String)literal.getValue());
Dependency original = DependencyStringNotationConverter.parse((String) requireNonNull(literal.getValue()));
if (depMatcher.matches(original.getGroupId(), original.getArtifactId())) {
Dependency updated = original;
if (!StringUtils.isBlank(newGroupId) && !updated.getGroupId().equals(newGroupId)) {
Expand All @@ -245,13 +246,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
updated = updated.withArtifactId(newArtifactId);
}
if (!StringUtils.isBlank(newVersion)) {
List<MavenRepository> repositories = "classpath".equals(m.getSimpleName()) ?
gradleProject.getMavenPluginRepositories() :
gradleProject.getMavenRepositories();
String resolvedVersion;
try {
resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updated.getGroupId(), updated.getArtifactId(), "0", newVersion, versionPattern, repositories, null, ctx)
.orElse(null);
resolvedVersion = new DependencyVersionSelector(null, gradleProject)
.select(new GroupArtifact(updated.getGroupId(), updated.getArtifactId()), m.getSimpleName(), newVersion, versionPattern, ctx);
} catch (MavenDownloadingException e) {
return e.warn(m);
}
Expand Down Expand Up @@ -290,15 +288,19 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
}
String keyValue = (String) key.getValue();
String valueValue = (String) value.getValue();
if ("group".equals(keyValue)) {
groupEntry = arg;
groupId = valueValue;
} else if ("name".equals(keyValue)) {
artifactEntry = arg;
artifactId = valueValue;
} else if ("version".equals(keyValue)) {
versionEntry = arg;
version = valueValue;
switch (keyValue) {
case "group":
groupEntry = arg;
groupId = valueValue;
break;
case "name":
artifactEntry = arg;
artifactId = valueValue;
break;
case "version":
versionEntry = arg;
version = valueValue;
break;
}
}
if (groupId == null || artifactId == null) {
Expand All @@ -317,13 +319,10 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
}
String updatedVersion = version;
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(version) || Boolean.TRUE.equals(overrideManagedVersion))) {
List<MavenRepository> repositories = "classpath".equals(m.getSimpleName()) ?
gradleProject.getMavenPluginRepositories() :
gradleProject.getMavenRepositories();
String resolvedVersion;
try {
resolvedVersion = AddDependencyVisitor.resolveDependencyVersion(updatedGroupId, updatedArtifactId, "0", newVersion, versionPattern, repositories, null, ctx)
.orElse(null);
resolvedVersion = new DependencyVersionSelector(null, gradleProject)
.select(new GroupArtifact(updatedGroupId, updatedArtifactId), m.getSimpleName(), newVersion, versionPattern, ctx);
} catch (MavenDownloadingException e) {
return e.warn(m);
}
Expand Down
Loading

0 comments on commit ce6e1f3

Please sign in to comment.