Skip to content

Commit 6b5d958

Browse files
Fix AddDependency duplicating dependencies in applied scripts (#6650)
* Fix AddDependency duplicating dependencies in applied scripts * Move match constraint to `IsBuildGradle` --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 1bfe6e9 commit 6b5d958

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

rewrite-gradle/src/main/java/org/openrewrite/gradle/AddDependency.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
188188
// or when source files were scanned
189189
boolean hasExplicitConfiguration = !StringUtils.isBlank(configuration);
190190
return Preconditions.check(hasExplicitConfiguration || onlyIfUsing == null || !acc.configurationsByProject.isEmpty(),
191-
Preconditions.check(new IsBuildGradle<>(), new JavaIsoVisitor<ExecutionContext>() {
191+
Preconditions.check(new IsBuildGradle<>(true), new JavaIsoVisitor<ExecutionContext>() {
192192

193193
@Override
194194
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {

rewrite-gradle/src/main/java/org/openrewrite/gradle/GradleConfigurationFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GradleConfigurationFilter {
3636
public void removeTransitiveConfigurations() {
3737
Set<String> tmpConfigurations = new HashSet<>(filteredConfigurations);
3838
for (String tmpConfiguration : tmpConfigurations) {
39-
GradleDependencyConfiguration gdc = requireNonNull((gradleProject.getConfiguration(tmpConfiguration)));
39+
GradleDependencyConfiguration gdc = requireNonNull(gradleProject.getConfiguration(tmpConfiguration));
4040
for (GradleDependencyConfiguration transitive : gradleProject.configurationsExtendingFrom(gdc, true)) {
4141
filteredConfigurations.remove(transitive.getName());
4242
}

rewrite-gradle/src/main/java/org/openrewrite/gradle/IsBuildGradle.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,35 @@
2424

2525
import java.nio.file.Path;
2626

27+
/**
28+
* Finds Gradle build files ending in `.gradle` and `.gradle.kts` files, but not `settings.gradle` or `settings.gradle.kts` files.
29+
* @param <P>
30+
*/
2731
public class IsBuildGradle<P> extends TreeVisitor<Tree, P> {
32+
33+
/**
34+
* Only match `build.gradle` and `build.gradle.kts` files, not any `dependencies.gradle` or similar files.
35+
*/
36+
boolean exactMatch;
37+
38+
public IsBuildGradle() {
39+
this(false);
40+
}
41+
42+
public IsBuildGradle(boolean exactMatch) {
43+
this.exactMatch = exactMatch;
44+
}
45+
2846
@Override
2947
public @Nullable Tree preVisit(@NonNull Tree tree, P p) {
3048
stopAfterPreVisit();
3149
if (tree instanceof JavaSourceFile) {
3250
JavaSourceFile sourceFile = (JavaSourceFile) tree;
33-
if (matches(sourceFile.getSourcePath())) {
51+
Path sourcePath = sourceFile.getSourcePath();
52+
if (exactMatch) {
53+
return sourcePath.endsWith("build.gradle") || sourcePath.endsWith("build.gradle.kts") ? SearchResult.found(sourceFile) : tree;
54+
}
55+
if (matches(sourcePath)) {
3456
return SearchResult.found(sourceFile);
3557
}
3658
}

rewrite-gradle/src/test/java/org/openrewrite/gradle/AddDependencyTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,54 @@ fun githubPackages(): Pair<String, String> {
18721872
);
18731873
}
18741874

1875+
@Test
1876+
void doNotAddDependencyToAppliedScripts() {
1877+
rewriteRun(
1878+
spec -> spec.recipe(addDependency("com.google.guava:guava:29.0-jre", null, "implementation")),
1879+
mavenProject("project",
1880+
buildGradle(
1881+
//language=groovy
1882+
"""
1883+
plugins {
1884+
id "java-library"
1885+
}
1886+
1887+
repositories {
1888+
mavenCentral()
1889+
}
1890+
1891+
apply from: "dependencies.gradle"
1892+
""",
1893+
//language=groovy
1894+
"""
1895+
plugins {
1896+
id "java-library"
1897+
}
1898+
1899+
repositories {
1900+
mavenCentral()
1901+
}
1902+
1903+
apply from: "dependencies.gradle"
1904+
1905+
dependencies {
1906+
implementation "com.google.guava:guava:29.0-jre"
1907+
}
1908+
"""
1909+
),
1910+
buildGradle(
1911+
//language=groovy
1912+
"""
1913+
dependencies {
1914+
}
1915+
""",
1916+
s -> s.path("dependencies.gradle")
1917+
)
1918+
)
1919+
);
1920+
}
1921+
1922+
18751923
private AddDependency addDependency(@SuppressWarnings("SameParameterValue") String gav) {
18761924
return addDependency(gav, null, null);
18771925
}

0 commit comments

Comments
 (0)