Skip to content

Commit

Permalink
change kotlin-compile-testing fork
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Apr 17, 2024
1 parent e452fca commit 335e4dc
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Expand Up @@ -15,9 +15,9 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '11'
java-version: '17'
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Publish
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Expand Up @@ -12,9 +12,9 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '11'
java-version: '17'
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
uses: actions/checkout@v4
- name: Run tests
uses: burrunan/gradle-cache-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Expand Up @@ -15,6 +15,6 @@ dependencies {

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
languageVersion.set(JavaLanguageVersion.of(17))
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/kotlin.gradle.kts
Expand Up @@ -4,6 +4,6 @@ plugins {

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
languageVersion.set(JavaLanguageVersion.of(17))
}
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/release.gradle.kts
Expand Up @@ -23,7 +23,7 @@ tasks.register("publish") {

tasks.register<Delete>("clean") {
group = "build"
delete = setOf(buildDir)
delete = setOf(layout.buildDirectory)
}

nexusPublishing {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
@@ -1,5 +1,5 @@
kotlin.code.style=official
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m
org.gradle.jvmargs=-Xmx4096m
kapt.includeCompileClasspath=false

group=com.faendir.kotlin.autodsl
11 changes: 5 additions & 6 deletions gradle/libs.versions.toml
@@ -1,15 +1,14 @@
[versions]
kotlin = "1.9.0"
kotlin = "1.9.23"
dokka = "1.9.20"
jgitver = "0.10.0-rc03"
nexusPublish = "2.0.0"
ksp = "1.9.0-1.0.11"
kotlinPoet = "1.14.2"
ksp = "1.9.23-1.0.19"
kotlinPoet = "1.16.0"
kotlinBard = "0.5.1"
compileTesting = "1.5.0"
compileTesting = "0.4.1"
junit = "5.10.2"
strikt = "0.34.1"
autoCommon = "1.1.2"
intellijPlugin = "1.17.3"

[plugins]
Expand All @@ -27,5 +26,5 @@ dokka-core = { module = "org.jetbrains.dokka:dokka-core", version.ref = "dokka"
jgitver = { module = "gradle.plugin.fr.brouillard.oss.gradle:gradle-jgitver-plugin", version.ref = "jgitver" }
nexusPublish = { module = "io.github.gradle-nexus:publish-plugin", version.ref = "nexusPublish" }
junit = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
kotlin-compileTesting = { module = "com.github.tschuchortdev:kotlin-compile-testing-ksp", version.ref = "compileTesting" }
kotlin-compileTesting = { module = "dev.zacsweers.kctfork:ksp", version.ref = "compileTesting" }
strikt = { module = "io.strikt:strikt-core", version.ref = "strikt" }
23 changes: 22 additions & 1 deletion processor/build.gradle.kts
Expand Up @@ -10,5 +10,26 @@ dependencies {
implementation(libs.kotlinpoet.ksp)
implementation(libs.kotlinpoet.metadata)
implementation(libs.kotlinbard)
implementation(kotlin("compiler-embeddable", "1.8.0"))
implementation(kotlin("compiler-embeddable"))
}

kotlin.sourceSets.all {
languageSettings.optIn("org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi")
}

if (JavaVersion.current() >= JavaVersion.VERSION_16) {
tasks.withType<Test> {
jvmArgs(
"--add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
)
}
}
Expand Up @@ -4,16 +4,16 @@ import com.faendir.kotlin.autodsl.CodeWriter
import com.squareup.kotlinpoet.FileSpec
import java.io.File
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.Element
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic

class KaptCodeWriter(processingEnv: ProcessingEnvironment) : CodeWriter<Type> {
private val dir : File = processingEnv.options["kapt.kotlin.generated"]?.let { File(it) }
private val dir: File = processingEnv.options["kapt.kotlin.generated"]?.let { File(it) }
?: run {
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, "Can't find the target directory for generated Kotlin files. Are you using kapt?")
throw IllegalStateException()
}

override fun emit(source: Type, fileSpec: FileSpec) = fileSpec.writeTo(dir)
override fun emit(source: Type, fileSpec: FileSpec) {
fileSpec.writeTo(dir)
}
}
Expand Up @@ -7,10 +7,16 @@ import com.faendir.kotlin.autodsl.nonnull
import com.google.devtools.ksp.symbol.ClassKind
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.metadata.*
import kotlinx.metadata.Flag
import kotlinx.metadata.KmClass
import kotlinx.metadata.KmConstructor
import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.Modality
import kotlinx.metadata.Visibility
import kotlinx.metadata.declaresDefaultValue
import kotlinx.metadata.isSecondary
import kotlinx.metadata.kind
import kotlinx.metadata.modality
import kotlinx.metadata.visibility
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.element.ElementKind
Expand All @@ -30,14 +36,14 @@ class KaptSourceInfoResolver(private val processingEnv: ProcessingEnvironment, p
override fun getClassesWithAnnotation(annotation: Type): List<Type> =
roundEnv.getElementsAnnotatedWith(annotation.element).filterIsInstance<TypeElement>().map { Type(it) }

override fun Type.getClassKind(): ClassKind = when {
kmClass.isEnum -> ClassKind.ENUM_CLASS
kmClass.isAnnotation -> ClassKind.ANNOTATION_CLASS
kmClass.isInterface -> ClassKind.INTERFACE
kmClass.isEnumEntry -> ClassKind.ENUM_ENTRY
kmClass.isClass -> ClassKind.CLASS
kmClass.isObject -> ClassKind.OBJECT
else -> throw IllegalArgumentException()
override fun Type.getClassKind(): ClassKind = when(kmClass.kind) {
kotlinx.metadata.ClassKind.CLASS -> ClassKind.CLASS
kotlinx.metadata.ClassKind.INTERFACE -> ClassKind.INTERFACE
kotlinx.metadata.ClassKind.ENUM_CLASS -> ClassKind.ENUM_CLASS
kotlinx.metadata.ClassKind.ENUM_ENTRY -> ClassKind.ENUM_ENTRY
kotlinx.metadata.ClassKind.ANNOTATION_CLASS -> ClassKind.ANNOTATION_CLASS
kotlinx.metadata.ClassKind.OBJECT -> ClassKind.OBJECT
kotlinx.metadata.ClassKind.COMPANION_OBJECT -> ClassKind.OBJECT
}

override fun <T : Annotation> Annotated.getAnnotationTypeProperty(annotation: KClass<T>, property: KProperty1<T, KClass<*>>): ClassName? = try {
Expand All @@ -51,7 +57,7 @@ class KaptSourceInfoResolver(private val processingEnv: ProcessingEnvironment, p
override fun <T : Annotation, V> Annotated.getAnnotationProperty(annotation: KClass<T>, property: KProperty1<T, V>): V? =
getAnnotation(annotation)?.let(property)

override fun Type.isAbstract(): Boolean = Flag.IS_ABSTRACT(kmClass.flags)
override fun Type.isAbstract(): Boolean = kmClass.modality == Modality.ABSTRACT

override fun Type.getConstructors(): List<Constructor> {
val constructorElements = element.enclosedElements.filterIsInstance<ExecutableElement>().filter { it.kind == ElementKind.CONSTRUCTOR }.toMutableList()
Expand All @@ -71,9 +77,9 @@ class KaptSourceInfoResolver(private val processingEnv: ProcessingEnvironment, p
}.map { (kmConstructor, element) -> Constructor(element, kmConstructor) }
}

override fun Constructor.isAccessible(): Boolean = Flag.IS_PUBLIC(kmConstructor.flags) || Flag.IS_INTERNAL(kmConstructor.flags)
override fun Constructor.isAccessible(): Boolean = kmConstructor.visibility in listOf(Visibility.PUBLIC, Visibility.INTERNAL)

override fun Type.getPrimaryConstructor(): Constructor? = getConstructors().find { it.kmConstructor.isPrimary }
override fun Type.getPrimaryConstructor(): Constructor? = getConstructors().find { !it.kmConstructor.isSecondary }

override fun Constructor.isValid(): Boolean = true

Expand All @@ -82,6 +88,7 @@ class KaptSourceInfoResolver(private val processingEnv: ProcessingEnvironment, p

override fun Type.asClassName(): ClassName = kmClass.asClassName()

@OptIn(KotlinPoetMetadataPreview::class)
override fun Parameter.getTypeDeclaration(): Type? {
val element = processingEnv.typeUtils.asElement(element.asType()) as? TypeElement
val kmType = try {
Expand Down
Expand Up @@ -2,7 +2,6 @@ package com.faendir.kotlin.autodsl.kapt

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import io.github.enjoydambience.kotlinbard.nullable
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
Expand Down
Expand Up @@ -2,6 +2,7 @@ package com.faendir.kotlin.autodsl

import com.faendir.kotlin.autodsl.kapt.KaptProcessor
import com.faendir.kotlin.autodsl.ksp.KspProcessorProvider
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile.Companion.fromPath
import com.tschuchort.compiletesting.SourceFile.Companion.kotlin
Expand All @@ -14,10 +15,10 @@ import strikt.assertions.isEqualTo
import java.io.File
import kotlin.test.assertNotNull

internal val KotlinCompilation.Result.workingDir: File
internal val JvmCompilationResult.workingDir: File
get() = outputDirectory.parentFile!!

val KotlinCompilation.Result.kspGeneratedSources: List<File>
val JvmCompilationResult.kspGeneratedSources: List<File>
get() = workingDir.resolve("ksp/sources/kotlin").collectSourceFiles()

private fun File.collectSourceFiles(): List<File> {
Expand Down Expand Up @@ -50,13 +51,13 @@ fun compileKsp(
symbolProcessorProviders = listOf(KspProcessorProvider())
}
val pass1 = compilation.compile()
expectThat(pass1).get(KotlinCompilation.Result::exitCode).isEqualTo(expect)
expectThat(pass1).get(JvmCompilationResult::exitCode).isEqualTo(expect)
val pass2 = KotlinCompilation().apply {
inheritClassPath = true
jvmTarget = "1.8"
sources = compilation.sources + pass1.kspGeneratedSources.map { fromPath(it) } + kotlin("Eval.kt", eval)
}.compile()
expectThat(pass2).get(KotlinCompilation.Result::exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
expectThat(pass2).get(JvmCompilationResult::exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
pass2.callEval()
return pass1.kspGeneratedSources
}
Expand All @@ -72,14 +73,14 @@ fun compileKapt(
sources = listOf(kotlin("Source.kt", source), kotlin("Eval.kt", eval))
annotationProcessors = listOf(KaptProcessor())
}.compile()
expectThat(result).get(KotlinCompilation.Result::exitCode).isEqualTo(expect)
expectThat(result).get(JvmCompilationResult::exitCode).isEqualTo(expect)
if (expect == KotlinCompilation.ExitCode.OK) {
result.callEval()
}
return result.generatedFiles.filter { it.extension == "kt" }
}

private fun KotlinCompilation.Result.callEval() = classLoader.loadClass("EvalKt").declaredMethods
private fun JvmCompilationResult.callEval() = classLoader.loadClass("EvalKt").declaredMethods
.first { it.name[0] != '$' /* skip jacoco added function */ }
.run {
isAccessible = true
Expand Down
2 changes: 1 addition & 1 deletion renovate.json
Expand Up @@ -20,7 +20,7 @@
"automergeType": "branch"
},
{
"matchPackagePatterns": ["^org\\.jetbrains\\.kotlin", "^com\\.google\\.devtools\\.ksp"],
"matchPackagePatterns": ["^org\\.jetbrains\\.kotlin", "^com\\.google\\.devtools\\.ksp", "^dev\\.zacsweers\\.kctfork"],
"groupName": "kotlin"
}
]
Expand Down
13 changes: 0 additions & 13 deletions sample-ksp/build.gradle.kts
Expand Up @@ -8,16 +8,3 @@ dependencies {
implementation(projects.annotations)
ksp(projects.processor)
}

sourceSets {
main {
java {
srcDir(file("$buildDir/generated/ksp/main/kotlin"))
}
}
test {
java {
srcDir(file("$buildDir/generated/ksp/test/kotlin"))
}
}
}

0 comments on commit 335e4dc

Please sign in to comment.