Skip to content

Commit

Permalink
Add initializers inject module (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
illarionov authored Jan 14, 2024
1 parent a57637a commit 42bff64
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ internal object PixnewsInitializerClassName {
"AsyncInitializer",
)
internal val appInitializersScope: ClassName = ClassName(
"ru.pixnews.foundation.initializers.inject",
"ru.pixnews.anvil.codegen.initializer.inject.wiring",
"AppInitializersScope",
)
internal val contributesInitializerFqName = FqName(
"ru.pixnews.foundation.initializers.inject.ContributesInitializer",
"ru.pixnews.anvil.codegen.initializer.inject.ContributesInitializer",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ class ContributesInitializerCodeGeneratorTest {
public fun interface AsyncInitializer { public fun init() }
""".trimIndent()
private val initializersInjectCode = """
package ru.pixnews.foundation.initializers.inject
package ru.pixnews.anvil.codegen.initializer.inject
import kotlin.reflect.KClass
public abstract class AppInitializersScope private constructor()
public annotation class ContributesInitializer(
val replaces: Array<KClass<*>> = [],
)
""".trimIndent()
private val appInitializersScope = """
package ru.pixnews.anvil.codegen.initializer.inject.wiring
public abstract class AppInitializersScope private constructor()
""".trimIndent()
private val loggerStubCode = """
package co.touchlab.kermit
open class Logger
Expand All @@ -63,7 +66,7 @@ class ContributesInitializerCodeGeneratorTest {
import co.touchlab.kermit.Logger
import com.google.firebase.FirebaseApp
import ru.pixnews.foundation.initializers.AsyncInitializer
import ru.pixnews.foundation.initializers.inject.ContributesInitializer
import ru.pixnews.anvil.codegen.initializer.inject.ContributesInitializer
import javax.inject.Provider
@ContributesInitializer
Expand All @@ -80,7 +83,7 @@ class ContributesInitializerCodeGeneratorTest {
import co.touchlab.kermit.Logger
import com.google.firebase.FirebaseApp
import ru.pixnews.foundation.initializers.AsyncInitializer
import ru.pixnews.foundation.initializers.inject.ContributesInitializer
import ru.pixnews.anvil.codegen.initializer.inject.ContributesInitializer
import ru.pixnews.initializers.FirebaseInitializer
import javax.inject.Provider
Expand All @@ -103,6 +106,7 @@ class ContributesInitializerCodeGeneratorTest {
fun setup() {
compilationResult = compileAnvil(
initializersInjectCode,
appInitializersScope,
firebaseStubCode,
initializerInterfacesCode,
javaxInjectProviderCode,
Expand Down Expand Up @@ -166,6 +170,7 @@ class ContributesInitializerCodeGeneratorTest {
fun setup() {
compilationResult = compileAnvil(
initializersInjectCode,
appInitializersScope,
firebaseStubCode,
initializerInterfacesCode,
javaxInjectProviderCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.initializer.inject

import kotlin.annotation.AnnotationTarget.CLASS
import kotlin.reflect.KClass

/**
* Annotate a Initializer / AsyncInitializer class with this to automatically contribute it to the
* Initializer multibinding.
* Equivalent to the following declaration in an application module:
*```
* @Module
* @ContributesTo(AppInitializersScope::class)
* abstract class ExperimentModule {
* @Binds @IntoSet
* abstract fun bindMainExperiment(initializer: MainInitializer): Initializer
* }
*```
* The generated code created via the anvil-codegen module.
*
* @property replaces This contributed module will replace these contributed classes.
* The array is allowed to include other contributed bindings, multibindings and Dagger modules. All replaced classes
* must use the same scope.
*/
@Target(CLASS)
public annotation class ContributesInitializer(
val replaces: Array<KClass<*>> = [],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024, the pixnews-anvil-codegen project authors and contributors.
* Please see the AUTHORS file for details.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/

package ru.pixnews.anvil.codegen.initializer.inject.wiring

public abstract class AppInitializersScope private constructor()

0 comments on commit 42bff64

Please sign in to comment.