Skip to content

Commit

Permalink
Added check for generateCompanionExtensions to ensure correct compo…
Browse files Browse the repository at this point in the history
…nent creation calls (#21)

kotlin-inject has a ksp flag:

`me.tatarka.inject.generateCompanionExtensions`

That can optionally use component companion objects to instantiate
themselves instead of call off the component `KClass` like so:

```kotlin
UserComponent.create(…)

vs 

UserComponent::class.create(…)
```

This PR checks that flag and changes how we call component creation
under the hood in generated code

---------

Co-authored-by: Drew Heavner <[email protected]>
  • Loading branch information
r0adkll and Drew Heavner authored Aug 17, 2024
1 parent 1f54d6e commit 0e649bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ internal class MergeComponentSymbolProcessor(
}
}

private val isGenerateCompanionExtensionsEnabled: Boolean
get() = env.options["me.tatarka.inject.generateCompanionExtensions"] == "true"

private var deferred: MutableList<KSClassDeclaration> = mutableListOf()

override fun process(resolver: Resolver): List<KSAnnotated> {
Expand Down Expand Up @@ -109,12 +112,17 @@ internal class MergeComponentSymbolProcessor(

// Create Companion extension method on original element to create this component
val constructorParameters = getConstructorParameters(element)
val createFunction = if (isGenerateCompanionExtensionsEnabled) {
"%T.create"
} else {
"%T::class.create"
}
addFunction(
FunSpec.builder("create$classSimpleName")
.receiver(element.toClassName().nestedClass("Companion"))
.addParameters(constructorParameters)
.addStatement(
"return %T.create(${constructorParameters.joinToString { "%L" }})",
"return $createFunction(${constructorParameters.joinToString { "%L" }})",
className,
*constructorParameters.map { it.name }.toTypedArray(),
)
Expand Down Expand Up @@ -253,7 +261,7 @@ internal class MergeComponentSymbolProcessor(
addSuperinterface(subcomponent.factoryClass.toClassName())

// Generate the factory creation function overload to generate this subcomponent
addFunction(subcomponent.createFactoryFunctionOverload())
addFunction(subcomponent.createFactoryFunctionOverload(isGenerateCompanionExtensionsEnabled))

// Generate the Subcomponent
addType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class SubcomponentDeclaration(
)
}

fun createFactoryFunctionOverload(): FunSpec = with(factoryClass) {
fun createFactoryFunctionOverload(
isGenerateCompanionExtensionsEnabled: Boolean,
): FunSpec = with(factoryClass) {
return FunSpec.buildFun(factoryFunction.simpleName.asString()) {
addModifiers(KModifier.OVERRIDE)

Expand All @@ -51,8 +53,13 @@ class SubcomponentDeclaration(

// Build the return statement constructing the expected merged subcomponent, including
// parent.
val componentCreationFunction = if (isGenerateCompanionExtensionsEnabled) {
"%L.create"
} else {
"%L::class.create"
}
addStatement(
"return %L.create(${factoryParameters.joinToString { "%L" }}" +
"return $componentCreationFunction(${factoryParameters.joinToString { "%L" }}" +
"${if (factoryParameters.isNotEmpty()) ", " else ""}this)",
subcomponentSimpleName,
*factoryParameters.map { it.name }.toTypedArray(),
Expand Down
4 changes: 0 additions & 4 deletions sample/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ composeCompiler {

android { namespace = "com.r0adkll.kimchi.restaurant" }

ksp {
arg("me.tatarka.inject.generateCompanionExtensions", "true")
}

private fun Project.addKspDependencyForAllTargets(dependencyNotation: Any) {
val kmpExtension = extensions.getByType<KotlinMultiplatformExtension>()
dependencies {
Expand Down

0 comments on commit 0e649bb

Please sign in to comment.