Skip to content

Commit 047cb44

Browse files
authored
Add Kotlin settings file support to AddSettingsPluginRepository (#6110)
1 parent 485cffc commit 047cb44

7 files changed

Lines changed: 1083 additions & 124 deletions

File tree

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

Lines changed: 180 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
import org.openrewrite.gradle.IsSettingsGradle;
2626
import org.openrewrite.gradle.marker.GradleProject;
2727
import org.openrewrite.gradle.marker.GradleSettings;
28-
import org.openrewrite.groovy.GroovyIsoVisitor;
2928
import org.openrewrite.groovy.tree.G;
3029
import org.openrewrite.internal.ListUtils;
30+
import org.openrewrite.java.JavaIsoVisitor;
3131
import org.openrewrite.java.style.IntelliJ;
3232
import org.openrewrite.java.style.TabsAndIndentsStyle;
3333
import org.openrewrite.java.tree.J;
3434
import org.openrewrite.java.tree.JavaSourceFile;
35+
import org.openrewrite.kotlin.tree.K;
3536
import org.openrewrite.marker.BuildTool;
3637
import org.openrewrite.maven.MavenDownloadingException;
3738
import org.openrewrite.maven.table.MavenMetadataFailures;
@@ -129,8 +130,21 @@ public Validated<Object> validate() {
129130

130131
@Override
131132
public TreeVisitor<?, ExecutionContext> getVisitor() {
132-
return Preconditions.check(Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), new GroovyIsoVisitor<ExecutionContext>() {
133+
return Preconditions.check(Preconditions.or(new IsBuildGradle<>(), new IsSettingsGradle<>()), new JavaIsoVisitor<ExecutionContext>() {
134+
133135
@Override
136+
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
137+
if (tree instanceof JavaSourceFile) {
138+
if (tree instanceof G.CompilationUnit) {
139+
return visitCompilationUnit((G.CompilationUnit) tree, ctx);
140+
}
141+
if (tree instanceof K.CompilationUnit) {
142+
return visitCompilationUnit((K.CompilationUnit) tree, ctx);
143+
}
144+
}
145+
return super.visit(tree, ctx);
146+
}
147+
134148
public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
135149
Optional<BuildTool> maybeBuildTool = cu.getMarkers().findFirst(BuildTool.class);
136150
if (!maybeBuildTool.isPresent()) {
@@ -198,6 +212,73 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
198212
return cu;
199213
}
200214

215+
public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, ExecutionContext ctx) {
216+
Optional<BuildTool> maybeBuildTool = cu.getMarkers().findFirst(BuildTool.class);
217+
if (!maybeBuildTool.isPresent()) {
218+
return cu;
219+
}
220+
BuildTool buildTool = maybeBuildTool.get();
221+
if (buildTool.getType() != BuildTool.Type.Gradle) {
222+
return cu;
223+
}
224+
VersionComparator versionComparator = Semver.validate("(,6)", null).getValue();
225+
if (versionComparator == null) {
226+
return cu;
227+
}
228+
// Don't modify an existing gradle enterprise DSL, only add one which is not already present
229+
if (containsGradleDevelocityDsl(cu)) {
230+
return cu;
231+
}
232+
233+
boolean gradleSixOrLater = versionComparator.compare(null, buildTool.getVersion(), "6.0") >= 0;
234+
if (gradleSixOrLater && cu.getSourcePath().endsWith("settings.gradle.kts")) {
235+
// Newer than 6.0 goes in settings
236+
Optional<GradleSettings> maybeGradleSettings = cu.getMarkers().findFirst(GradleSettings.class);
237+
if (!maybeGradleSettings.isPresent()) {
238+
return cu;
239+
}
240+
GradleSettings gradleSettings = maybeGradleSettings.get();
241+
242+
try {
243+
String newVersion = findNewerVersion(new DependencyVersionSelector(metadataFailures, null, gradleSettings), ctx);
244+
if (newVersion == null) {
245+
return cu;
246+
}
247+
248+
String pluginId;
249+
if (versionComparator.compare(null, newVersion, "3.17") >= 0) {
250+
pluginId = "com.gradle.develocity";
251+
} else {
252+
pluginId = "com.gradle.enterprise";
253+
}
254+
255+
cu = withPlugin(cu, pluginId, newVersion, versionComparator, ctx);
256+
} catch (MavenDownloadingException e) {
257+
return e.warn(cu);
258+
}
259+
} else if (!gradleSixOrLater && "build.gradle.kts".equals(cu.getSourcePath().toString())) {
260+
// Older than 6.0 goes in root build.gradle only, not in build.gradle of subprojects
261+
Optional<GradleProject> maybeGradleProject = cu.getMarkers().findFirst(GradleProject.class);
262+
if (!maybeGradleProject.isPresent()) {
263+
return cu;
264+
}
265+
GradleProject gradleProject = maybeGradleProject.get();
266+
267+
try {
268+
String newVersion = findNewerVersion(new DependencyVersionSelector(metadataFailures, gradleProject, null), ctx);
269+
if (newVersion == null) {
270+
return cu;
271+
}
272+
273+
cu = withPlugin(cu, "com.gradle.build-scan", newVersion, versionComparator, ctx);
274+
} catch (MavenDownloadingException e) {
275+
return e.warn(cu);
276+
}
277+
}
278+
279+
return cu;
280+
}
281+
201282
private @Nullable String findNewerVersion(DependencyVersionSelector versionSelector, ExecutionContext ctx) throws MavenDownloadingException {
202283
String newVersion = versionSelector
203284
.select(new GroupArtifact("com.gradle.develocity", "com.gradle.develocity.gradle.plugin"), "classpath", version, null, ctx);
@@ -207,25 +288,38 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
207288
}
208289
return newVersion;
209290
}
210-
});
211-
}
212291

213-
private G.CompilationUnit withPlugin(G.CompilationUnit cu, String pluginId, String newVersion, VersionComparator versionComparator, ExecutionContext ctx) {
214-
cu = (G.CompilationUnit) new AddPluginVisitor(pluginId, newVersion, null, null, false)
215-
.visitNonNull(cu, ctx);
216-
cu = (G.CompilationUnit) new UpgradePluginVersion(pluginId, newVersion, null).getVisitor()
217-
.visitNonNull(cu, ctx);
218-
J.MethodInvocation gradleEnterpriseInvocation = gradleEnterpriseDsl(
219-
newVersion,
220-
versionComparator,
221-
getIndent(cu),
222-
ctx);
223-
return cu.withStatements(ListUtils.concat(cu.getStatements(), gradleEnterpriseInvocation));
292+
private G.CompilationUnit withPlugin(G.CompilationUnit cu, String pluginId, String newVersion, VersionComparator versionComparator, ExecutionContext ctx) {
293+
cu = (G.CompilationUnit) new AddPluginVisitor(pluginId, newVersion, null, null, false)
294+
.visitNonNull(cu, ctx, getCursor());
295+
cu = (G.CompilationUnit) new UpgradePluginVersion(pluginId, newVersion, null).getVisitor()
296+
.visitNonNull(cu, ctx, getCursor());
297+
J.MethodInvocation gradleEnterpriseInvocation = groovyEnterpriseDsl(
298+
newVersion,
299+
versionComparator,
300+
getIndent(cu),
301+
ctx);
302+
return cu.withStatements(ListUtils.concat(cu.getStatements(), gradleEnterpriseInvocation));
303+
}
304+
305+
private K.CompilationUnit withPlugin(K.CompilationUnit cu, String pluginId, String newVersion, VersionComparator versionComparator, ExecutionContext ctx) {
306+
cu = (K.CompilationUnit) new AddPluginVisitor(pluginId, newVersion, null, null, false)
307+
.visitNonNull(cu, ctx, getCursor());
308+
cu = (K.CompilationUnit) new UpgradePluginVersion(pluginId, newVersion, null).getVisitor()
309+
.visitNonNull(cu, ctx, getCursor());
310+
J.MethodInvocation gradleEnterpriseInvocation = kotlinEnterpriseDsl(
311+
newVersion,
312+
versionComparator,
313+
getIndent(cu),
314+
ctx);
315+
return cu.withStatements(ListUtils.concat(cu.getStatements(), gradleEnterpriseInvocation));
316+
}
317+
});
224318
}
225319

226320
private static boolean containsGradleDevelocityDsl(JavaSourceFile cu) {
227321
AtomicBoolean found = new AtomicBoolean(false);
228-
new GroovyIsoVisitor<AtomicBoolean>() {
322+
new JavaIsoVisitor<AtomicBoolean>() {
229323
@Override
230324
public @Nullable J visit(@Nullable Tree tree, AtomicBoolean atomicBoolean) {
231325
if (atomicBoolean.get()) {
@@ -246,7 +340,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
246340
return found.get();
247341
}
248342

249-
private J.@Nullable MethodInvocation gradleEnterpriseDsl(String newVersion, VersionComparator versionComparator, String indent, ExecutionContext ctx) {
343+
private J.@Nullable MethodInvocation groovyEnterpriseDsl(String newVersion, VersionComparator versionComparator, String indent, ExecutionContext ctx) {
250344
if (server == null && allowUntrustedServer == null && captureTaskInputFiles == null && uploadInBackground == null && publishCriteria == null) {
251345
return null;
252346
}
@@ -314,7 +408,75 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
314408
return (J.MethodInvocation) cu.getStatements().get(0);
315409
}
316410

317-
private static String getIndent(G.CompilationUnit cu) {
411+
private J.@Nullable MethodInvocation kotlinEnterpriseDsl(String newVersion, VersionComparator versionComparator, String indent, ExecutionContext ctx) {
412+
if (server == null && allowUntrustedServer == null && captureTaskInputFiles == null && uploadInBackground == null && publishCriteria == null) {
413+
return null;
414+
}
415+
boolean versionIsAtLeast3_2 = versionComparator.compare(null, newVersion, "3.2") >= 0;
416+
boolean versionIsAtLeast3_7 = versionComparator.compare(null, newVersion, "3.7") >= 0;
417+
boolean versionIsAtLeast3_17 = versionComparator.compare(null, newVersion, "3.17") >= 0;
418+
StringBuilder ge;
419+
if (versionIsAtLeast3_17) {
420+
ge = new StringBuilder("\ndevelocity {\n");
421+
} else {
422+
ge = new StringBuilder("\ngradleEnterprise {\n");
423+
}
424+
if (server != null && !server.isEmpty()) {
425+
ge.append(indent).append("server.set(\"").append(server).append("\")\n");
426+
}
427+
if (allowUntrustedServer != null && versionIsAtLeast3_2) {
428+
ge.append(indent).append("allowUntrustedServer.set(").append(allowUntrustedServer).append(")\n");
429+
}
430+
if (captureTaskInputFiles != null || uploadInBackground != null || (allowUntrustedServer != null && !versionIsAtLeast3_2) || publishCriteria != null) {
431+
ge.append(indent).append("buildScan {\n");
432+
if (publishCriteria != null) {
433+
if (publishCriteria == PublishCriteria.Always) {
434+
if (versionIsAtLeast3_17) {
435+
ge.append(indent).append(indent).append("publishing.onlyIf { true }\n");
436+
} else {
437+
ge.append(indent).append(indent).append("publishAlways()\n");
438+
}
439+
} else {
440+
if (versionIsAtLeast3_17) {
441+
ge.append(indent).append(indent).append("publishing.onlyIf { it.buildResult.failures.isNotEmpty() }\n");
442+
} else {
443+
ge.append(indent).append(indent).append("publishOnFailure()\n");
444+
}
445+
}
446+
}
447+
if (allowUntrustedServer != null && !versionIsAtLeast3_2) {
448+
ge.append(indent).append(indent).append("allowUntrustedServer.set(").append(allowUntrustedServer).append(")\n");
449+
}
450+
if (uploadInBackground != null) {
451+
ge.append(indent).append(indent).append("uploadInBackground.set(").append(uploadInBackground).append(")\n");
452+
}
453+
if (captureTaskInputFiles != null) {
454+
if (versionIsAtLeast3_7) {
455+
ge.append(indent).append(indent).append("capture {\n");
456+
if (versionIsAtLeast3_17) {
457+
ge.append(indent).append(indent).append(indent).append("fileFingerprints.set(").append(captureTaskInputFiles).append(")\n");
458+
} else {
459+
ge.append(indent).append(indent).append(indent).append("taskInputFiles.set(").append(captureTaskInputFiles).append(")\n");
460+
}
461+
ge.append(indent).append(indent).append("}\n");
462+
} else {
463+
ge.append(indent).append(indent).append("captureTaskInputFiles.set(").append(captureTaskInputFiles).append(")\n");
464+
}
465+
}
466+
ge.append(indent).append("}\n");
467+
}
468+
ge.append("}\n");
469+
K.CompilationUnit cu = GradleParser.builder().build()
470+
.parseInputs(singletonList(
471+
Parser.Input.fromString(Paths.get("settings.gradle.kts"), ge.toString())), null, ctx)
472+
.map(K.CompilationUnit.class::cast)
473+
.findFirst()
474+
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"));
475+
476+
return (J.MethodInvocation) ((J.Block) cu.getStatements().get(0)).getStatements().get(0);
477+
}
478+
479+
private static String getIndent(JavaSourceFile cu) {
318480
TabsAndIndentsStyle style = Style.from(TabsAndIndentsStyle.class, cu, IntelliJ::tabsAndIndents);
319481
if (style.getUseTabCharacter()) {
320482
return "\t";

0 commit comments

Comments
 (0)