Skip to content

Commit a486cb5

Browse files
div-bargaligithub-actions[bot]timtebeek
authored
Bug: Incorrect plugin insertion order in settings.gradle with pluginManagement and buildscript (#6478)
* Add tests for adding plugins with plugin management and buildscript blocks * Refactor assertions in version interpolation methods to enhance readability and maintainability * Enhance AddPluginVisitor to correctly insert plugins block after buildscript in settings.gradle(.kts) * Update rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddPluginVisitor.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review * Apply formatter * Remove needless `interpolateResolvedVersion` as 3.11.x sees no more releases --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent e91804a commit a486cb5

File tree

3 files changed

+209
-104
lines changed

3 files changed

+209
-104
lines changed

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/AddPluginVisitor.java

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -232,34 +232,41 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integ
232232
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));
233233

234234
if (FindMethods.find(cu, "RewriteGradleProject plugins(..)").isEmpty() && FindMethods.find(cu, "RewriteSettings plugins(..)").isEmpty()) {
235-
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle")) &&
236-
!cu.getStatements().isEmpty() &&
237-
cu.getStatements().get(0) instanceof J.MethodInvocation &&
238-
"pluginManagement".equals(((J.MethodInvocation) cu.getStatements().get(0)).getSimpleName())) {
239-
return cu.withStatements(ListUtils.insert(cu.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), 1));
235+
int insertAtIdx = 0;
236+
237+
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle"))) {
238+
// For settings.gradle, find the last position of pluginManagement or buildscript
239+
for (int i = 0; i < cu.getStatements().size(); i++) {
240+
Statement existingStatement = cu.getStatements().get(i);
241+
if (existingStatement instanceof J.MethodInvocation) {
242+
String methodName = ((J.MethodInvocation) existingStatement).getSimpleName();
243+
if ("pluginManagement".equals(methodName) || "buildscript".equals(methodName)) {
244+
insertAtIdx = i + 1;
245+
}
246+
}
247+
}
240248
} else {
241-
int insertAtIdx = 0;
242249
for (int i = 0; i < cu.getStatements().size(); i++) {
243250
Statement existingStatement = cu.getStatements().get(i);
244251
if (existingStatement instanceof J.MethodInvocation && "buildscript".equals(((J.MethodInvocation) existingStatement).getSimpleName())) {
245252
insertAtIdx = i + 1;
246253
break;
247254
}
248255
}
249-
if (insertAtIdx == 0) {
250-
Comment licenseHeader = getLicenseHeader(cu);
251-
if (licenseHeader != null) {
252-
cu = (G.CompilationUnit) removeLicenseHeader(cu);
253-
statement = statement.withComments(singletonList(licenseHeader));
254-
}
255-
Space leadingSpace = Space.firstPrefix(cu.getStatements());
256-
return cu.withStatements(ListUtils.insert(
257-
Space.formatFirstPrefix(cu.getStatements(), leadingSpace.withWhitespace("\n\n" + leadingSpace.getWhitespace())),
258-
autoFormat(statement, ctx, getCursor()),
259-
insertAtIdx));
260-
} else {
261-
return cu.withStatements(ListUtils.insert(cu.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), insertAtIdx));
256+
}
257+
if (insertAtIdx == 0) {
258+
Comment licenseHeader = getLicenseHeader(cu);
259+
if (licenseHeader != null) {
260+
cu = (G.CompilationUnit) removeLicenseHeader(cu);
261+
statement = statement.withComments(singletonList(licenseHeader));
262262
}
263+
Space leadingSpace = Space.firstPrefix(cu.getStatements());
264+
return cu.withStatements(ListUtils.insert(
265+
Space.formatFirstPrefix(cu.getStatements(), leadingSpace.withWhitespace("\n\n" + leadingSpace.getWhitespace())),
266+
autoFormat(statement, ctx, getCursor()),
267+
insertAtIdx));
268+
} else {
269+
return cu.withStatements(ListUtils.insert(cu.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), insertAtIdx));
263270
}
264271
} else {
265272
MethodMatcher buildPluginsMatcher = new MethodMatcher("RewriteGradleProject plugins(groovy.lang.Closure)");
@@ -345,7 +352,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
345352
return ((K.CompilationUnit) parsed);
346353
})
347354
.map(parsed -> parsed.getStatements().get(0))
348-
.map(block -> ((J.Block)block).getStatements().get(0))
355+
.map(block -> ((J.Block) block).getStatements().get(0))
349356
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));
350357

351358
AtomicBoolean hasPluginsBlock = new JavaIsoVisitor<AtomicBoolean>() {
@@ -360,40 +367,47 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
360367
}.reduce(cu, new AtomicBoolean());
361368
if (!hasPluginsBlock.get()) {
362369
J.Block block = (J.Block) cu.getStatements().get(0);
363-
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle")) &&
364-
!block.getStatements().isEmpty() &&
365-
block.getStatements().get(0) instanceof J.MethodInvocation &&
366-
"pluginManagement".equals(((J.MethodInvocation) block.getStatements().get(0)).getSimpleName())) {
367-
block = block.withStatements(ListUtils.insert(block.getStatements(), autoFormat(statement.withPrefix(Space.format("\n\n")), ctx, getCursor()), 1));
370+
int insertAtIdx = 0;
371+
372+
if (cu.getSourcePath().endsWith(Paths.get("settings.gradle.kts"))) {
373+
for (int i = 0; i < block.getStatements().size(); i++) {
374+
Statement existingStatement = block.getStatements().get(i);
375+
if (existingStatement instanceof J.MethodInvocation) {
376+
String methodName = ((J.MethodInvocation) existingStatement).getSimpleName();
377+
if ("pluginManagement".equals(methodName) || "buildscript".equals(methodName)) {
378+
insertAtIdx = i + 1;
379+
}
380+
}
381+
}
368382
} else {
369-
int insertAtIdx = 0;
370-
for (int i = 0; i < cu.getStatements().size(); i++) {
371-
Statement existingStatement = cu.getStatements().get(i);
383+
for (int i = 0; i < block.getStatements().size(); i++) {
384+
Statement existingStatement = block.getStatements().get(i);
372385
if (existingStatement instanceof J.MethodInvocation && "buildscript".equals(((J.MethodInvocation) existingStatement).getSimpleName())) {
373386
insertAtIdx = i + 1;
374387
break;
375388
}
376389
}
377-
if (insertAtIdx == 0) {
378-
Comment licenseHeader = getLicenseHeader(cu);
379-
if (licenseHeader != null) {
380-
cu = (K.CompilationUnit) removeLicenseHeader(cu);
381-
block = (J.Block) cu.getStatements().get(0);
382-
statement = statement.withComments(singletonList(licenseHeader));
383-
}
384-
Space leadingSpace = Space.firstPrefix(block.getStatements());
385-
block = block.withStatements(ListUtils.insert(
386-
Space.formatFirstPrefix(block.getStatements(), leadingSpace.withWhitespace("\n\n" + leadingSpace.getWhitespace())),
387-
statement,
388-
insertAtIdx));
389-
} else {
390-
block = block.withStatements(ListUtils.insert(block.getStatements(), statement.withPrefix(Space.format("\n\n")), insertAtIdx));
390+
}
391+
392+
if (insertAtIdx == 0) {
393+
Comment licenseHeader = getLicenseHeader(cu);
394+
if (licenseHeader != null) {
395+
cu = (K.CompilationUnit) removeLicenseHeader(cu);
396+
block = (J.Block) cu.getStatements().get(0);
397+
statement = statement.withComments(singletonList(licenseHeader));
391398
}
399+
Space leadingSpace = Space.firstPrefix(block.getStatements());
400+
block = block.withStatements(ListUtils.insert(
401+
Space.formatFirstPrefix(block.getStatements(), leadingSpace.withWhitespace("\n\n" + leadingSpace.getWhitespace())),
402+
statement,
403+
insertAtIdx));
404+
} else {
405+
block = block.withStatements(ListUtils.insert(block.getStatements(), statement.withPrefix(Space.format("\n\n")), insertAtIdx));
392406
}
393407
J.Block newStatement = block;
394408
return cu.withStatements(ListUtils.mapFirst(cu.getStatements(), __ -> newStatement));
395409
} else {
396-
J.MethodInvocation pluginDef = (J.MethodInvocation)(((J.Block) ((J.Lambda) ((J.MethodInvocation) statement).getArguments().get(0)).getBody()).getStatements().get(0));
410+
J.MethodInvocation pluginDef = (J.MethodInvocation) (((J.Block) ((J.Lambda) ((J.MethodInvocation) statement).getArguments().get(0)).getBody()).getStatements().get(0));
397411
return cu.withStatements(ListUtils.mapFirst(cu.getStatements(), b -> {
398412
if (b instanceof J.Block) {
399413
J.Block block = (J.Block) b;

rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/AddBuildPluginTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static org.openrewrite.Tree.randomId;
2525
import static org.openrewrite.gradle.Assertions.buildGradle;
26+
import static org.openrewrite.gradle.Assertions.buildGradleKts;
2627
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
2728

2829
class AddBuildPluginTest implements RewriteTest {
@@ -254,4 +255,36 @@ void addPluginApplyFalse() {
254255
)
255256
);
256257
}
258+
259+
@Test
260+
void addPluginAfterBuildscriptBlockKotlin() {
261+
rewriteRun(
262+
spec -> spec.recipe(new AddBuildPlugin("com.jfrog.bintray", "1.0", null, null, null)),
263+
buildGradleKts(
264+
"""
265+
buildscript {
266+
repositories {
267+
mavenCentral()
268+
}
269+
}
270+
271+
val myProperty = "test"
272+
""",
273+
"""
274+
buildscript {
275+
repositories {
276+
mavenCentral()
277+
}
278+
}
279+
280+
plugins {
281+
id("com.jfrog.bintray") version "1.0"
282+
}
283+
284+
val myProperty = "test"
285+
"""
286+
)
287+
);
288+
}
289+
257290
}

0 commit comments

Comments
 (0)