Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding Filer convenience methods back #105

Closed
JakeWharton opened this issue Jun 2, 2017 · 8 comments
Closed

Consider adding Filer convenience methods back #105

JakeWharton opened this issue Jun 2, 2017 · 8 comments
Milestone

Comments

@JakeWharton
Copy link
Collaborator

JakeWharton commented Jun 2, 2017

kapt compiles kt files generated from processors per comments on #93

@hotchemi
Copy link

hotchemi commented Jul 19, 2017

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

@JorgeCastilloPrz
Copy link

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
Copy link
Collaborator Author

JakeWharton commented Jul 21, 2017 via email

@hotchemi
Copy link

hotchemi commented Jul 22, 2017

@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
Copy link

JorgeCastilloPrz commented Jul 22, 2017

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
Copy link

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())
}

@swankjesse swankjesse added this to the Backlog milestone Nov 25, 2018
@Egorand Egorand modified the milestones: Backlog, Icebox Mar 2, 2019
@ZacSweers
Copy link
Collaborator

Definitely want this now that Kapt is adding support for gradle's incremental kapt support and using the same filer APIs

JetBrains/kotlin#2179

https://github.com/JetBrains/kotlin/pull/2179/files#diff-f5958ff916f88658e65a723fb591467dR142

I can take a swing at this this weekend

@Egorand
Copy link
Collaborator

Egorand commented Mar 13, 2019

Awesome, please do!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants