Skip to content

Commit dda3cca

Browse files
committed
Rewrite semanticdb-gradle-plugin to Kotlin
1 parent 12ac5eb commit dda3cca

3 files changed

Lines changed: 454 additions & 412 deletions

File tree

build.sbt

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,28 @@ lazy val semanticdbShared = project
8585

8686
lazy val gradlePlugin = project
8787
.in(file("semanticdb-gradle-plugin"))
88+
.enablePlugins(KotlinPlugin)
8889
.settings(
8990
name := "semanticdb-gradle",
90-
buildInfoPackage := "com.sourcegraph.scip_java",
9191
publish / skip := true,
92-
scalacOptions ++= Seq("-target:11", "-release", "11"),
92+
kotlinVersion := V.kotlinVersion,
93+
kotlincJvmTarget := "11",
94+
// The Gradle init script puts only gradle-plugin.jar on the initscript
95+
// classpath, so the assembled jar must contain kotlin-stdlib.
96+
kotlinLib("stdlib"),
97+
// Mark sbt-kotlin-plugin's auto-added kotlin-scripting-compiler-embeddable
98+
// as Provided so it does NOT get bundled into our gradle-plugin.jar fat-jar.
99+
// (kotlin-compiler-embeddable is already isolated to the KotlinInternal
100+
// configuration and will not be picked up by sbt-assembly.)
101+
kotlinRuntimeProvided := true,
102+
Compile / javacOptions ++= Seq("--release", "11"),
103+
Compile / sourceGenerators += gradlePluginBuildInfoGenerator.taskValue,
93104
libraryDependencies ++=
94105
List(
95106
"dev.gradleplugins" % "gradle-api" % V.gradle % Provided,
96-
"dev.gradleplugins" % "gradle-test-kit" % V.gradle % Provided,
97-
"org.jetbrains.kotlin" % "kotlin-gradle-plugin" % V.kotlinVersion %
98-
Provided
99-
),
100-
buildInfoKeys :=
101-
Seq[BuildInfoKey](
102-
version,
103-
sbtVersion,
104-
scalaVersion,
105-
"javacModuleOptions" -> javacModuleOptions,
106-
"semanticdbVersion" -> V.scalameta,
107-
"scala213" -> V.scala213
107+
"dev.gradleplugins" % "gradle-test-kit" % V.gradle % Provided
108108
)
109109
)
110-
.enablePlugins(BuildInfoPlugin)
111110

112111
lazy val javacPlugin = project
113112
.in(file("semanticdb-javac"))
@@ -535,6 +534,50 @@ def javacModuleOptions = List(
535534
"-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
536535
)
537536

537+
// Source-generator for the Gradle plugin's small build-info Java class.
538+
// Replaces the previous sbt-buildinfo-generated Scala BuildInfo object so the
539+
// gradle-plugin module can stay Kotlin-only (and the generated class is easy
540+
// to consume from Kotlin).
541+
def javaStringLiteral(value: String): String = {
542+
val escaped = value.flatMap {
543+
case '\\' => "\\\\"
544+
case '"' => "\\\""
545+
case '\n' => "\\n"
546+
case '\r' => "\\r"
547+
case '\t' => "\\t"
548+
case c if c.isControl => f"\\u${c.toInt}%04x"
549+
case c => c.toString
550+
}
551+
"\"" + escaped + "\""
552+
}
553+
554+
lazy val gradlePluginBuildInfoGenerator = Def.task {
555+
val out =
556+
(Compile / sourceManaged).value / "com" / "sourcegraph" / "scip_java" /
557+
"GradlePluginBuildInfo.java"
558+
IO.createDirectory(out.getParentFile)
559+
val optionsLiteral = javacModuleOptions
560+
.map(javaStringLiteral)
561+
.mkString("Arrays.asList(", ", ", ")")
562+
val versionLiteral = javaStringLiteral(version.value)
563+
val contents =
564+
s"""package com.sourcegraph.scip_java;
565+
|
566+
|import java.util.Arrays;
567+
|import java.util.Collections;
568+
|import java.util.List;
569+
|
570+
|public final class GradlePluginBuildInfo {
571+
| private GradlePluginBuildInfo() {}
572+
| public static final String version = $versionLiteral;
573+
| public static final List<String> javacModuleOptions =
574+
| Collections.unmodifiableList($optionsLiteral);
575+
|}
576+
|""".stripMargin
577+
IO.write(out, contents)
578+
Seq(out)
579+
}
580+
538581
lazy val unit = project
539582
.in(file("tests/unit"))
540583
.settings(

0 commit comments

Comments
 (0)