evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.14k stars 51 forks source link

Update the generated filename for KmpComponentCreate functions #385

Closed eygraber closed 2 months ago

eygraber commented 2 months ago

I missed the Target...Accessor part in the previous PR.

I also simplified the name, because what I had before was pretty gnarly since it was trying to avoid FileAlreadyExistsException in case there are multiple @KmpComponentCreate functions for the same Component.

With this new approach the following would cause a compile time error because the processor would generate two files named KmpComponentCreateMyComponent.kt:

@Component
abstract class MyComponent

@KmpComponentCreate
expect fun createKmp(): MyComponent

@KmpComponentCreate
expect fun MyComponent.Companion.createKmp(): MyComponent

Using the approach from before there would be no FileAlreadyExistsException, but the generated file names would be KmpComponentCreateMyComponentCreateKmp.kt and KmpComponentCreateMyComponentMyComponentCompanionCreateKmp.kt respectively. That works, and is safest, so the question is how ugly is too ugly for generated file names?

Another approach would be to defer writing the files until the final round, and then group the functions by Component type, and generate one file containing all of the functions for that Component.

evant commented 2 months ago

Another option would be to group by source file, I've used that successfully with ksp before though I don't think it's currently doing that for components in this project. ksp's incremental compilation only works on the file-level too anyway so there shouldn't be an issues if one function in the file changes, though I don't know if there's plans to change that.

eygraber commented 2 months ago

I implemented grouping the KmpComponentCreate functions into a single file by Component type and it was pretty simple. I'll push that in a separate commit now.