Skip to content

Commit

Permalink
add search marker on dependency closure if direct dependency cannot b…
Browse files Browse the repository at this point in the history
…e determined (openrewrite#4042)

* Add search result marker to the dependency block or root if the marker cannot reliably be places on a specific dependency configuration.

* Add search result marker to the dependency block or root if the marker cannot reliably be places on a specific dependency configuration.

Change marker setting: fixes issues in 2nd cycle.
  • Loading branch information
pstreef authored Feb 23, 2024
1 parent ca65834 commit 3a69b22
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.openrewrite.gradle.search;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.gradle.marker.GradleDependencyConfiguration;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class DependencyInsight extends Recipe {
transient DependenciesInUse dependenciesInUse = new DependenciesInUse(this);

private static final MethodMatcher DEPENDENCY_CONFIGURATION_MATCHER = new MethodMatcher("DependencyHandlerSpec *(..)");
private static final MethodMatcher DEPENDENCY_CLOSURE_MATCHER = new MethodMatcher("RewriteGradleProject dependencies(..)");

@Option(displayName = "Group pattern",
description = "Group glob pattern used to match dependencies.",
Expand Down Expand Up @@ -124,15 +127,15 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
}
for (ResolvedDependency resolvedDependency : c.getResolved()) {
ResolvedDependency dep = resolvedDependency.findDependency(groupIdPattern, artifactIdPattern);
if(dep ==null) {
if (dep == null) {
continue;
}
if(version != null) {
if (version != null) {
VersionComparator versionComparator = Semver.validate(version, null).getValue();
if(versionComparator == null) {
if (versionComparator == null) {
sourceFile = Markup.warn(sourceFile, new IllegalArgumentException("Could not construct a valid version comparator from " + version + "."));
} else {
if(!versionComparator.isValid(null, dep.getVersion())) {
if (!versionComparator.isValid(null, dep.getVersion())) {
continue;
}
}
Expand Down Expand Up @@ -186,21 +189,56 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
}
}
return new MarkIndividualDependency(configurationToDirectDependency, directDependencyToTargetDependency)
.visitNonNull(sourceFile, ctx);
.attachMarkers(sourceFile, ctx);
}
};
}

@EqualsAndHashCode(callSuper = false)
@Value
@RequiredArgsConstructor
@Data
private static class MarkIndividualDependency extends JavaIsoVisitor<ExecutionContext> {
private final Map<String, Set<GroupArtifactVersion>> configurationToDirectDependency;
private final Map<GroupArtifactVersion, Set<GroupArtifactVersion>> directDependencyToTargetDependency;
private boolean attachToDependencyClosure = false;
private boolean hasMarker = false;

Map<String, Set<GroupArtifactVersion>> configurationToDirectDependency;
Map<GroupArtifactVersion, Set<GroupArtifactVersion>> directDependencyToTargetDependency;
public Tree attachMarkers(Tree before, ExecutionContext ctx) {
J after = super.visitNonNull(before, ctx);
if (!hasMarker) {
if (after == before) {
attachToDependencyClosure = true;
after = super.visitNonNull(before, ctx);
}
if (after == before) {
String resultText = directDependencyToTargetDependency.values().stream()
.flatMap(Set::stream)
.distinct()
.map(target -> target.getGroupId() + ":" + target.getArtifactId() + ":" + target.getVersion())
.collect(Collectors.joining(","));
return SearchResult.found(after, resultText);
}
}
return after;
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (DEPENDENCY_CLOSURE_MATCHER.matches(m)) {
String resultText = directDependencyToTargetDependency.values().stream()
.flatMap(Set::stream)
.distinct()
.map(target -> target.getGroupId() + ":" + target.getArtifactId() + ":" + target.getVersion())
.collect(Collectors.joining(","));
J.MethodInvocation after = SearchResult.found(m, resultText);
if (after == m) {
hasMarker = true;
}
if (attachToDependencyClosure) {
return after;
}
}
if (!DEPENDENCY_CONFIGURATION_MATCHER.matches(m) || !configurationToDirectDependency.containsKey(m.getSimpleName()) ||
m.getArguments().isEmpty()) {
return m;
Expand All @@ -226,7 +264,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
String resultText = directDependencyToTargetDependency.get(direct).stream()
.map(target -> target.getGroupId() + ":" + target.getArtifactId() + ":" + target.getVersion())
.collect(Collectors.joining(","));
return SearchResult.found(m, resultText);
J.MethodInvocation result = SearchResult.found(m, resultText);
if (m == result) {
hasMarker = true;
}
return result;
}
} else if (arg instanceof G.MapEntry) {
String groupId = null;
Expand Down Expand Up @@ -264,7 +306,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
String resultText = directDependencyToTargetDependency.get(direct).stream()
.map(target -> target.getGroupId() + ":" + target.getArtifactId() + ":" + target.getVersion())
.collect(Collectors.joining(","));
return SearchResult.found(m, resultText);
J.MethodInvocation result = SearchResult.found(m, resultText);
if (m == result) {
hasMarker = true;
}
return result;
}
}
return m;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.groovy.Assertions.groovy;

class DependencyInsightTest implements RewriteTest {

Expand Down Expand Up @@ -67,6 +68,96 @@ void findTransitiveDependency() {
);
}

@Test
void findPluginDependencyAndAddToDependencyClosure() {
rewriteRun(
buildGradle("""
plugins {
id 'groovy-gradle-plugin'
}
repositories {
gradlePluginPortal()
}
""", spec -> spec.path("buildSrc/build.gradle")),
groovy("""
plugins{
id "java"
}
dependencies{
implementation 'com.google.guava:guava:31.1-jre'
}
""", spec -> spec.path("buildSrc/src/main/groovy/convention-plugin.gradle")),
buildGradle("""
plugins {
id 'convention-plugin'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.openrewrite:rewrite-java:8.0.0'
}
""",
"""
plugins {
id 'convention-plugin'
}
repositories {
mavenCentral()
}
/*~~(com.google.guava:failureaccess:1.0.1)~~>*/dependencies {
implementation 'org.openrewrite:rewrite-java:8.0.0'
}
"""
)
);
}

@Test
void findPluginDependencyAndAddToRoot() {
rewriteRun(
buildGradle("""
plugins {
id 'groovy-gradle-plugin'
}
repositories {
gradlePluginPortal()
}
""", spec -> spec.path("buildSrc/build.gradle")),
groovy("""
plugins{
id "java"
}
dependencies{
implementation 'com.google.guava:guava:31.1-jre'
}
""", spec -> spec.path("buildSrc/src/main/groovy/convention-plugin.gradle")),
buildGradle("""
plugins {
id 'convention-plugin'
}
repositories {
mavenCentral()
}
""",
"""
/*~~(com.google.guava:failureaccess:1.0.1)~~>*/plugins {
id 'convention-plugin'
}
repositories {
mavenCentral()
}
"""
)
);
}

@Test
void recursive() {
rewriteRun(
Expand Down

0 comments on commit 3a69b22

Please sign in to comment.