Skip to content

Commit

Permalink
Merge pull request #996 from Kotlin/keywords-generator
Browse files Browse the repository at this point in the history
Keywords generator plugin: moved and fixed for Kotlin 2.1
  • Loading branch information
Jolanrensen authored Dec 19, 2024
2 parents 04d3653 + 69c239b commit 2143fb4
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 57 deletions.

This file was deleted.

9 changes: 3 additions & 6 deletions generator/README.md → plugins/keywords-generator/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
## :generator
## :plugins:keywords-generator

This module holds a little Gradle plugin whose sole purpose is to provide
[:core](../core) with the `generateKeywordsSrc` task.
[:core](../../core) with the `generateKeywordsSrc` task.

This task, generates three enum classes: `HardKeywords`, `ModifierKeywords`, and `SoftKeywords`.
These enums together contain all restricted Kotlin keywords to be taken into account when generating our own
code in Notebooks or any of our [plugins](../plugins). Words like "package", "fun", "suspend", etc...
code in Notebooks or any of our [plugins](..). Words like "package", "fun", "suspend", etc...

As the Kotlin language can change over time, this task ensures that any changes to the language
will be reflected in our code generation.

This module will likely be moved under [:plugins](../plugins):
[Issue #899](https://github.com/Kotlin/dataframe/issues/899).
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
`java-gradle-plugin`
`kotlin-dsl`
id("com.github.gmazzo.buildconfig") version "5.5.1"
}

val kotlinCompilerVersion: String by project
Expand All @@ -10,8 +11,14 @@ repositories {
mavenCentral()
}

buildConfig {
packageName = "org.jetbrains.kotlinx.dataframe"
className = "BuildConfig"
buildConfigField("kotlinCompilerVersion", kotlinCompilerVersion)
}

dependencies {
implementation(kotlin("compiler-embeddable", kotlinCompilerVersion))
compileOnly(kotlin("compiler-embeddable", kotlinCompilerVersion))
implementation("com.squareup:kotlinpoet:$kotlinPoetVersion")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlinCompilerVersion=2.0.20
kotlinPoetVersion=1.18.1
kotlinPoetVersion=2.0.0
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,38 @@ import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.lexer.KtKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import java.io.File

open class GeneratorTask : DefaultTask() {
abstract class KeywordsGeneratorAction : WorkAction<KeywordsGeneratorAction.Parameters> {

@OutputDirectory
lateinit var srcDir: File
interface Parameters : WorkParameters {
var srcDir: File
}

private val taskPackageName = "org.jetbrains.dataframe.keywords"

@Input
override fun getGroup() = "codegen"

@TaskAction
fun generate() {
srcDir.deleteRecursively()
override fun execute() {
parameters.srcDir.deleteRecursively()
generateKeywordEnums()
}

private fun generateKeywordEnums() {
listOf(
"HardKeywords" to KtTokens.KEYWORDS,
"SoftKeywords" to KtTokens.SOFT_KEYWORDS,
"ModifierKeywords" to KtTokens.MODIFIER_KEYWORDS
"ModifierKeywords" to KtTokens.MODIFIER_KEYWORDS,
).forEach { (name, set) ->
generateKeywordsEnum(name, set)
}
}

private fun generateKeywordsEnum(name: String, tokenSet: TokenSet) {
buildKwEnum(name, getKeywords(tokenSet)).writeTo(srcDir)
buildKwEnum(name, getKeywords(tokenSet)).writeTo(parameters.srcDir)
}

private fun getKeywords(tokenSet: TokenSet): List<EnumEntry> {
Expand Down Expand Up @@ -87,8 +82,4 @@ open class GeneratorTask : DefaultTask() {

return fileBuilder.build()
}

companion object {
const val NAME = "generateKeywordsSrc"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.jetbrains.dataframe.keywords

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.DependencyScopeConfiguration
import org.gradle.api.artifacts.ResolvableConfiguration
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.register
import org.jetbrains.kotlinx.dataframe.BuildConfig
import java.io.File

@Suppress("UnstableApiUsage")
abstract class KeywordsGeneratorPlugin : Plugin<Project> {

override fun apply(target: Project): Unit = with(target) {
// from https://kotlinlang.org/docs/whatsnew21.html#compiler-symbols-hidden-from-the-kotlin-gradle-plugin-api
val dependencyScopeConfiguration: DependencyScopeConfiguration = configurations.dependencyScope("keywordsGeneratorDependencyScope").get()
dependencies.add(dependencyScopeConfiguration.name, "$KOTLIN_COMPILER_EMBEDDABLE:$KOTLIN_COMPILER_VERSION")

val resolvableConfiguration: ResolvableConfiguration = configurations.resolvable("keywordGeneratorResolvable") {
extendsFrom(dependencyScopeConfiguration)
}.get()

val genSrcDir = layout.buildDirectory.asFile.get().resolve("generatedSrc")

val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
val mainSourceSet = sourceSets.named("main").get()
mainSourceSet.addDir(genSrcDir)

val genTask = tasks.register<KeywordsGeneratorTask>(KeywordsGeneratorTask.NAME) {
kotlinCompiler.from(resolvableConfiguration)
srcDir = genSrcDir
}

tasks["compileKotlin"].dependsOn(genTask)
}

private fun SourceSet.addDir(dir: File) {
java.setSrcDirs(java.srcDirs + dir)
}

companion object {
const val KOTLIN_COMPILER_EMBEDDABLE = "org.jetbrains.kotlin:kotlin-compiler-embeddable"
const val KOTLIN_COMPILER_VERSION: String = BuildConfig.kotlinCompilerVersion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.jetbrains.dataframe.keywords

import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.submit
import org.gradle.workers.WorkerExecutor
import java.io.File
import javax.inject.Inject

abstract class KeywordsGeneratorTask: DefaultTask() {

@get:Inject
abstract val executor: WorkerExecutor

@get:Classpath
abstract val kotlinCompiler: ConfigurableFileCollection

@OutputDirectory
lateinit var srcDir: File

@Input
override fun getGroup() = "codegen"

@TaskAction
fun generate() {
val workQueue = executor.classLoaderIsolation {
classpath.from(kotlinCompiler)
}
workQueue.submit(KeywordsGeneratorAction::class) {
srcDir = this@KeywordsGeneratorTask.srcDir
}
}

companion object {
const val NAME = "generateKeywordsSrc"
}
}
4 changes: 2 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

rootProject.name = "dataframe"

// treated as a separate project with its own Kotlin version etc.
includeBuild("generator")
// treated as a separate project with its own Kotlin version, etc.
includeBuild("plugins/keywords-generator")

include("plugins:dataframe-gradle-plugin")
include("plugins:symbol-processor")
Expand Down

0 comments on commit 2143fb4

Please sign in to comment.