Closed JakeWharton closed 5 years ago
I investigated a bit so let me share about it here.
generated/source/kaptKotlin/$sourceSetName
(obtained by processingEnv.options["kapt.kotlin.generated"]
), not generated/source/apt/$sourceSetName
Do you have any clue about how to write KotlinPoet generated classes to filer without using the not yet available KotlinPoet api method writeTo(filer)
for it @hotchemi?
Call toString(), write that as your file contents.
On Fri, Jul 21, 2017, 2:05 PM Jorge Castillo notifications@github.com wrote:
Do you have any clue about how to write KotlinPoet generated classes to filer without using the not yet available KotlinPoet api method writeTo(filer) for it @hotchemi https://github.com/hotchemi?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/square/kotlinpoet/issues/105#issuecomment-317072061, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEYexRlYhhOkRQV43iSpSDFynv3U8ks5sQOhSgaJpZM4Nupmb .
@JorgeCastilloPrz might be an off topic for this issue but right now we do as below and it works...the reason of replace("kaptKotlin", "kapt")
is https://youtrack.jetbrains.com/issue/KT-19097. Hope it will help you.
https://github.com/hotchemi/PermissionsDispatcher/blob/master/processor/src/main/kotlin/permissions/dispatcher/processor/PermissionsProcessor.kt#L69
Im gonna add here another valid executable code sample. It's a repo I'm contributing to where we are also generating Kotlin files and compile time using KotlinPoet API and being able to referentiate those files from production code with no compile time errors: https://github.com/kategory/implicits-processor.
Interesting point here: I am just taking the kapt.kotlin.generated
option path as it is, without replacing anything into it (related to @hotchemi's comment).
./gradlew clean :app:build
to run and generate the files.
This is what worked for me (inspired by the kotlin-examples processor).
In the process method...
Get the target generated directory for kapt:
val kaptKotlinGeneratedDir = processingEnv.options["kapt.kotlin.generated"] ?:
run {
processingEnv.messager.printMessage(
ERROR,
"Can't find the target directory for generated Kotlin files."
)
return false
}
Create your file object:
val kotlinFile = KotlinFile.builder("com.example", "KotlinClass.kt")
.addType(TypeSpec.classBuilder("KotlinClass").build())
.build()
Write the file:
File(
"$kaptKotlinGeneratedDir/${kotlinFile.packageName.replace(".", "/")}",
kotlinFile.fileName
).apply {
parentFile.mkdirs()
writeText(kotlinFile.toString())
}
Definitely want this now that Kapt is adding support for gradle's incremental kapt support and using the same filer APIs
https://github.com/JetBrains/kotlin/pull/2179
https://github.com/JetBrains/kotlin/pull/2179/files#diff-f5958ff916f88658e65a723fb591467dR142
I can take a swing at this this weekend
Awesome, please do!
kapt compiles kt files generated from processors per comments on https://github.com/square/kotlinpoet/pull/93