Skip to content

Commit

Permalink
bumped to kotlin 2.0.0-Beta5, included the compiler plugin with tests…
Browse files Browse the repository at this point in the history
… as separate module
  • Loading branch information
Jolanrensen committed Mar 21, 2024
1 parent 4f8d874 commit 2a878f1
Show file tree
Hide file tree
Showing 32 changed files with 1,338 additions and 61 deletions.
38 changes: 38 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@file:Suppress("UnstableApiUsage")

import Projects.compilerPlugin
import Projects.gradlePlugin
import com.github.gmazzo.buildconfig.BuildConfigExtension


buildscript {
repositories {
mavenCentral()
Expand All @@ -15,6 +20,7 @@ plugins {
dokka version Versions.dokka
idea
kotlin version Versions.kotlin apply false
buildconfig version Versions.buildconfig apply false
}

group = Versions.groupID
Expand Down Expand Up @@ -113,4 +119,36 @@ allprojects {
}
}
}
}

subprojects {
afterEvaluate {
extensions.findByType<BuildConfigExtension>()?.apply {
val projectVersion = Versions.project
val groupId = Versions.groupID

val compilerPluginId = "$groupId.compilerPlugin"

val compilerPluginArtifactId = compilerPlugin.name
val gradlePluginArtifactId = gradlePlugin.name

val defaultSparkifyFqName = "$groupId.plugin.annotations.Sparkify"
val defaultColumnNameFqName = "$groupId.plugin.annotations.ColumnName"

val projectRoot = project.rootDir.absolutePath

packageName(groupId)
className("Artifacts")

buildConfigField("compilerPluginId", compilerPluginId)
buildConfigField("groupId", groupId)
buildConfigField("gradlePluginArtifactId", gradlePluginArtifactId)
buildConfigField("projectVersion", projectVersion)
buildConfigField("compilerPluginArtifactId", compilerPluginArtifactId)

buildConfigField("defaultSparkifyFqName", defaultSparkifyFqName)
buildConfigField("defaultColumnNameFqName", defaultColumnNameFqName)
buildConfigField("projectRoot", projectRoot)
}
}
}
15 changes: 14 additions & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ object Dependencies {
inline val hadoopClient get() = "org.apache.hadoop:hadoop-client:${Versions.hadoop}"
inline val sparkRepl get() = "org.apache.spark:spark-repl_${Versions.scalaCompat}:${Versions.spark}"
inline val jupyter get() = "org.jetbrains.kotlinx:kotlin-jupyter-api:${Versions.jupyter}"
inline val junit get() = "org.junit.jupiter:junit-jupiter-engine:5.8.1"
inline val junitJupiterEngine get() = "org.junit.jupiter:junit-jupiter-engine:${Versions.junitJupiterEngine}"
// must be platform()
inline val junitBom get() = "org.junit:junit-bom:${Versions.junitJupiterEngine}"
inline val junitJupiter get() = "org.junit.jupiter:junit-jupiter"
inline val junitPlatformCommons get() = "org.junit.platform:junit-platform-commons"
inline val junitPlatformLauncher get() = "org.junit.platform:junit-platform-launcher"
inline val junitPlatformRunner get() = "org.junit.platform:junit-platform-runner"
inline val junitPlatformSuiteApi get() = "org.junit.platform:junit-platform-suite-api"

inline val junit get() = "junit:junit:${Versions.junit}"
inline val sparkStreamingKafka get() = "org.apache.spark:spark-streaming-kafka-0-10_${Versions.scalaCompat}:${Versions.spark}"
inline val kotest get() = "io.kotest:kotest-runner-junit5:${Versions.kotest}"
inline val kotestTestcontainers get() = "io.kotest.extensions:kotest-extensions-testcontainers:${Versions.kotestTestContainers}"
Expand All @@ -22,6 +31,10 @@ object Dependencies {
inline val kotlinScriptingJvm get() = "org.jetbrains.kotlin:kotlin-scripting-jvm"
inline val jacksonDatabind get() = "com.fasterxml.jackson.core:jackson-databind:${Versions.jacksonDatabind}"
inline val kotlinDateTime get() = "org.jetbrains.kotlinx:kotlinx-datetime:${Versions.kotlinxDateTime}"
inline val kotlinCompiler get() = "org.jetbrains.kotlin:kotlin-compiler:${Versions.kotlin}"
inline val kotlinScriptRuntime get() = "org.jetbrains.kotlin:kotlin-script-runtime:${Versions.kotlin}"
inline val kotlinAnnotationsJvm get() = "org.jetbrains.kotlin:kotlin-annotations-jvm:${Versions.kotlin}"
inline val kotlinCompilerInternalTestFramework get() = "org.jetbrains.kotlin:kotlin-compiler-internal-test-framework:${Versions.kotlin}"
}


Expand Down
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ fun DependencyHandler.testImplementation(vararg dependencyNotations: Any): List<
add("testImplementation", it)
}

fun DependencyHandler.testRuntimeOnly(vararg dependencyNotations: Any): List<Dependency?> =
dependencyNotations.map {
add("testRuntimeOnly", it)
}

fun DependencyHandler.implementation(vararg dependencyNotations: Any): List<Dependency?> =
dependencyNotations.map {
add("implementation", it)
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Plugins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ inline val Project.mavenPublishBase
inline val PluginDependenciesSpec.jupyter
get() = kotlin("jupyter.api") version Versions.jupyter

inline val PluginDependenciesSpec.buildconfig
get() = id("com.github.gmazzo.buildconfig")

6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/Projects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ object Projects {

inline val Project.scalaTuplesInKotlin
get() = searchProject("scala-tuples-in-kotlin")

inline val Project.compilerPlugin
get() = searchProject("compiler-plugin")

inline val Project.gradlePlugin
get() = searchProject("gradle-plugin")
}
11 changes: 7 additions & 4 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
object Versions {
const val project = "1.2.5-SNAPSHOT"
const val groupID = "org.jetbrains.kotlinx.spark"
const val kotlin = "1.9.22"
const val kotlin = "2.0.0-Beta5"
const val jvmTarget = "8"
const val jupyterJvmTarget = "8"
inline val spark get() = System.getProperty("spark") as String

inline val scala get() = System.getProperty("scala") as String
inline val sparkMinor get() = spark.substringBeforeLast('.')
inline val scalaCompat get() = scala.substringBeforeLast('.')

inline val scalaCompat get() = scala.substringBeforeLast('.')
// TODO
inline val sparkConnect get() = System.getProperty("sparkConnect", "false").toBoolean()

const val jupyter = "0.12.0-32-1"

const val kotest = "5.5.4"

const val buildconfig = "5.3.5"

const val junitJupiterEngine = "5.8.1"
const val junit = "4.13.2"
const val kotestTestContainers = "1.3.3"
const val dokka = "1.8.20"
const val jcp = "7.0.5"
Expand Down
92 changes: 92 additions & 0 deletions compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
kotlin
mavenPublishBase
buildconfig
}

group = Versions.groupID
version = Versions.project

repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
}

sourceSets {
test {
val srcDirs = listOf("src/test-gen/kotlin")
kotlin.srcDirs(srcDirs)
java.srcDirs(srcDirs)
}
}

dependencies {
with(Dependencies) {
compileOnly(kotlinCompiler)

testRuntimeOnly(
kotlinTest,
kotlinScriptRuntime,
kotlinAnnotationsJvm,
)

testImplementation(
kotlinCompiler,
reflect,
kotlinCompilerInternalTestFramework,
junit,

platform(junitBom),
junitJupiter,
junitPlatformCommons,
junitPlatformLauncher,
junitPlatformRunner,
junitPlatformSuiteApi,
)
}
}

tasks.test {
useJUnitPlatform()
doFirst {
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-stdlib", "kotlin-stdlib")
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-stdlib-jdk8", "kotlin-stdlib-jdk8")
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-reflect", "kotlin-reflect")
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-test", "kotlin-test")
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-script-runtime", "kotlin-script-runtime")
setLibraryProperty("org.jetbrains.kotlin.test.kotlin-annotations-jvm", "kotlin-annotations-jvm")
}
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
languageVersion = "2.0"
freeCompilerArgs = freeCompilerArgs +
"-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi" +
"-Xcontext-receivers"
}
}

val generateTests by tasks.creating(JavaExec::class) {
classpath = sourceSets.test.get().runtimeClasspath
mainClass.set("org.jetbrains.kotlinx.spark.compilerPlugin.GenerateTestsKt")
}

val compileTestKotlin by tasks.getting {
doLast {
generateTests.exec()
}
}

fun Test.setLibraryProperty(propName: String, jarName: String) {
val path = project.configurations
.testRuntimeClasspath.get()
.files
.find { """$jarName-\d.*jar""".toRegex().matches(it.name) }
?.absolutePath
?: return
systemProperty(propName, path)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.kotlinx.spark.compilerPlugin

import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar

// Potential future K2 FIR hook
class SimplePluginRegistrar(private val sparkifyAnnotationFqNames: List<String>) : FirExtensionRegistrar() {
override fun ExtensionRegistrarContext.configurePlugin() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.jetbrains.kotlinx.spark.compilerPlugin

import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlinx.spark.Artifacts

open class SparkifyCommandLineProcessor : CommandLineProcessor {

init {
println("SparkifyCommandLineProcessor loaded")
}

override val pluginId: String = Artifacts.compilerPluginId

override val pluginOptions: Collection<AbstractCliOption> = listOf(
OPTION_ENABLED,
OPTION_SPARKIFY_ANNOTATION_FQ_NAMES,
OPTION_COLUMN_NAME_ANNOTATION_FQ_NAMES,
)

override fun processOption(option: AbstractCliOption, value: String, configuration: CompilerConfiguration) {
when (val optionName = option.optionName) {
OPTION_ENABLED.optionName ->
configuration.put(KEY_ENABLED, value.toBoolean())

OPTION_SPARKIFY_ANNOTATION_FQ_NAMES.optionName ->
configuration.put(KEY_SPARKIFY_ANNOTATION_FQ_NAMES, value.split(",").map { it.trim() })

OPTION_COLUMN_NAME_ANNOTATION_FQ_NAMES.optionName ->
configuration.put(KEY_COLUMN_NAME_ANNOTATION_FQ_NAMES, value.split(",").map { it.trim() })

else -> error("Unexpected option: $optionName")
}
}
}

internal val KEY_ENABLED = CompilerConfigurationKey<Boolean>("Whether to enable Sparkify")

internal val OPTION_ENABLED = CliOption(
optionName = "enabled",
valueDescription = "<true|false>",
description = "Whether to enable Sparkify",
required = false,
allowMultipleOccurrences = false,
)

internal val KEY_SPARKIFY_ANNOTATION_FQ_NAMES = CompilerConfigurationKey<List<String>>(
"Fully qualified names of annotations for Sparkify"
)

internal val OPTION_SPARKIFY_ANNOTATION_FQ_NAMES = CliOption(
optionName = "sparkifyAnnotationFqNames",
valueDescription = "<fqName1,fqName2,...>",
description = "Fully qualified names of annotations to sparkify",
required = false,
allowMultipleOccurrences = false,
)

internal val KEY_COLUMN_NAME_ANNOTATION_FQ_NAMES = CompilerConfigurationKey<List<String>>(
"Fully qualified names of annotations for ColumnName"
)

internal val OPTION_COLUMN_NAME_ANNOTATION_FQ_NAMES = CliOption(
optionName = "columnNameAnnotationFqNames",
valueDescription = "<fqName1,fqName2,...>",
description = "Fully qualified names of annotations for ColumnName",
required = false,
allowMultipleOccurrences = false,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jetbrains.kotlinx.spark.compilerPlugin

import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlinx.spark.Artifacts
import org.jetbrains.kotlinx.spark.compilerPlugin.ir.SparkifyIrGenerationExtension

open class SparkifyCompilerPluginRegistrar: CompilerPluginRegistrar() {
init {
println("SparkifyCompilerPluginRegistrar loaded")
}

override val supportsK2: Boolean
get() = true

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
if (configuration.get(KEY_ENABLED) != true) return

val sparkifyAnnotationFqNames = configuration.get(KEY_SPARKIFY_ANNOTATION_FQ_NAMES)
?: listOf(Artifacts.defaultSparkifyFqName)

val columnNameAnnotationFqNames = configuration.get(KEY_COLUMN_NAME_ANNOTATION_FQ_NAMES)
?: listOf(Artifacts.defaultColumnNameFqName)

IrGenerationExtension.registerExtension(
SparkifyIrGenerationExtension(
sparkifyAnnotationFqNames = sparkifyAnnotationFqNames,
columnNameAnnotationFqNames = columnNameAnnotationFqNames,
)
)
}
}
Loading

0 comments on commit 2a878f1

Please sign in to comment.