square / kotlinpoet

A Kotlin API for generating .kt source files.
https://square.github.io/kotlinpoet/
Apache License 2.0
3.93k stars 289 forks source link

Consider adding Filer convenience methods back #105

Closed JakeWharton closed 5 years ago

JakeWharton commented 7 years ago

kapt compiles kt files generated from processors per comments on https://github.com/square/kotlinpoet/pull/93

hotchemi commented 7 years ago

I investigated a bit so let me share about it here.

JorgeCastilloPrz commented 7 years ago

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?

JakeWharton commented 7 years ago

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 .

hotchemi commented 7 years ago

@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

JorgeCastilloPrz commented 7 years ago

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.

codeprogression commented 7 years ago

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())
}
ZacSweers commented 5 years ago

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

Egorand commented 5 years ago

Awesome, please do!