Skip to content

Commit

Permalink
Fix handling of freestanding scripts which don't contain dependency c…
Browse files Browse the repository at this point in the history
…onfigurations. Include fix for ChangeDependencyClassifier for G.MapLiteral types
  • Loading branch information
shanman190 committed Dec 11, 2024
1 parent e2e20fa commit 630cf5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
G.MapEntry mapEntry = null;
String classifierStringDelimiter = null;
int index = 0;
for (Expression e : depArgs) {
if (!(e instanceof G.MapEntry)) {
continue;
}
G.MapEntry arg = (G.MapEntry) e;
for (G.MapEntry arg : map.getElements()) {
if (!(arg.getKey() instanceof J.Literal) || !(arg.getValue() instanceof J.Literal)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
Expand All @@ -37,7 +38,10 @@
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.semver.*;
import org.openrewrite.semver.ExactVersion;
import org.openrewrite.semver.LatestIntegration;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;

import java.util.*;

Expand Down Expand Up @@ -98,7 +102,6 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
DependencyMatcher dependencyMatcher = new DependencyMatcher(groupPattern == null ? "*" : groupPattern, artifactPattern == null ? "*" : artifactPattern, null);
return Preconditions.check(
new IsBuildGradle<>(),
new GroovyIsoVisitor<ExecutionContext>() {
Expand Down Expand Up @@ -197,14 +200,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

Optional<GradleDependency> maybeGradleDependency = new GradleDependency.Matcher().get(getCursor());
Optional<GradleDependency> maybeGradleDependency = new GradleDependency.Matcher()
.groupId(groupPattern)
.artifactId(artifactPattern)
.get(getCursor());
if (!maybeGradleDependency.isPresent()) {
return m;
}

GradleDependency gradleDependency = maybeGradleDependency.get();
ResolvedDependency d = gradleDependency.getResolvedDependency();
if (!dependencyMatcher.matches(d.getGroupId(), d.getArtifactId())) {
if (StringUtils.isBlank(d.getVersion())) {
return m;
}

Expand All @@ -216,12 +222,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}
}
}
for (GradleDependencyConfiguration configuration : gp.getConfiguration(m.getSimpleName()).allExtendsFrom()) {
if (platforms.containsKey(configuration.getName())) {
for (ResolvedPom platform : platforms.get(configuration.getName())) {
String managedVersion = platform.getManagedVersion(d.getGroupId(), d.getArtifactId(), null, d.getRequested().getClassifier());
if (matchesComparator(managedVersion, d.getVersion())) {
return maybeRemoveVersion(m);
GradleDependencyConfiguration gdc = gp.getConfiguration(m.getSimpleName());
if (gdc != null) {
for (GradleDependencyConfiguration configuration : gdc.allExtendsFrom()) {
if (platforms.containsKey(configuration.getName())) {
for (ResolvedPom platform : platforms.get(configuration.getName())) {
String managedVersion = platform.getManagedVersion(d.getGroupId(), d.getArtifactId(), null, d.getRequested().getClassifier());
if (matchesComparator(managedVersion, d.getVersion())) {
return maybeRemoveVersion(m);
}
}
}
}
Expand Down

0 comments on commit 630cf5f

Please sign in to comment.