Skip to content

Commit

Permalink
feat: migrate to Anvil's annotation-optional artifact
Browse files Browse the repository at this point in the history
Signed-off-by: Harsh Shandilya <[email protected]>
  • Loading branch information
msfjarvis committed Sep 17, 2023
1 parent 39ee7ee commit 524b166
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 69 deletions.
7 changes: 4 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ androidGradle = "7.4.2"
androidxActivity = "1.5.0"
androidxAppCompat = "1.3.1"
androidxCompose = "1.2.0"
androidxComposeCompiler = "1.4.4"
androidxComposeCompiler = "1.4.6"
androidxCore = "1.7.0"
androidxFragment = "1.5.0"
androidxLifecycle = "2.5.0"
androidxWork = "2.7.0"
anvil = "2.4.4"
anvil = "2.4.8-1-8"
dagger = "2.45"
kotlin = "1.8.10"
kotlin = "1.8.20"
kotlinxCoroutines = "1.6.4"
mavenPublish = "0.22.0"
truth = "1.1.3"
Expand All @@ -37,6 +37,7 @@ androidxLifecycleViewModel = { module = "androidx.lifecycle:lifecycle-viewmodel-
androidxLifecycleViewModelCompose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidxLifecycle" }
androidxWorkRuntime = { module = "androidx.work:work-runtime-ktx", version.ref = "androidxWork" }
anvilAnnotations = { module = "com.squareup.anvil:annotations", version.ref = "anvil" }
anvilAnnotationsOptional = { module = "com.squareup.anvil:annotations-optional", version.ref = "anvil" }
anvilCompiler = { module = "com.squareup.anvil:compiler-api", version.ref = "anvil" }
anvilCompilerUtils = { module = "com.squareup.anvil:compiler-utils", version.ref = "anvil" }
anvilGradle = { module = "com.squareup.anvil:gradle-plugin", version.ref = "anvil" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.deliveryhero.whetstone.ForScope
import com.deliveryhero.whetstone.worker.ContributesWorker
import com.deliveryhero.whetstone.worker.WorkerScope
import com.squareup.anvil.annotations.optional.ForScope
import javax.inject.Inject

@ContributesWorker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal object FqNames {
@JvmField val CONTRIBUTES_INJECTOR = FqName("com.deliveryhero.whetstone.injector.ContributesInjector")
@JvmField val AUTO_INJECTOR = FqName("com.deliveryhero.whetstone.meta.AutoInjectorBinding")
@JvmField val AUTO_INSTANCE = FqName("com.deliveryhero.whetstone.meta.AutoInstanceBinding")
@JvmField val SINGLE_IN = FqName("com.deliveryhero.whetstone.SingleIn")
@JvmField val SINGLE_IN = FqName("com.squareup.anvil.annotations.optional.SingleIn")
@JvmField val APPLICATION = FqName("android.app.Application")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.content.Context
import androidx.work.ListenableWorker
import androidx.work.WorkerFactory
import androidx.work.WorkerParameters
import com.deliveryhero.whetstone.ForScope
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.app.ApplicationScope
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.ForScope
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance
import javax.inject.Provider

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal class CodegenTest {
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.deliveryhero.whetstone.ForScope
import com.deliveryhero.whetstone.worker.ContributesWorker
import com.deliveryhero.whetstone.worker.WorkerScope
import com.squareup.anvil.annotations.optional.ForScope
import javax.inject.Inject
@ContributesWorker
Expand Down
8 changes: 0 additions & 8 deletions whetstone/api/whetstone.api
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,9 @@ public final class anvil/hint/subcomponent/com/deliveryhero/whetstone/viewmodel/
public static final fun getCom_deliveryhero_whetstone_viewmodel_ViewModelComponent_scope ()Lkotlin/reflect/KClass;
}

public abstract interface annotation class com/deliveryhero/whetstone/ForScope : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/Class;
}

public abstract interface annotation class com/deliveryhero/whetstone/InternalWhetstoneApi : java/lang/annotation/Annotation {
}

public abstract interface annotation class com/deliveryhero/whetstone/SingleIn : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/Class;
}

public final class com/deliveryhero/whetstone/Whetstone {
public static final field INSTANCE Lcom/deliveryhero/whetstone/Whetstone;
public final fun fromActivity (Landroid/app/Activity;)Ljava/lang/Object;
Expand Down
1 change: 1 addition & 0 deletions whetstone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ anvil {

dependencies {
api(libs.dagger)
api(libs.anvilAnnotationsOptional)

implementation(libs.androidxLifecycleRuntime)
implementation(libs.androidxLifecycleProcess)
Expand Down
21 changes: 0 additions & 21 deletions whetstone/src/main/java/com/deliveryhero/whetstone/ForScope.kt

This file was deleted.

19 changes: 0 additions & 19 deletions whetstone/src/main/java/com/deliveryhero/whetstone/SingleIn.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.deliveryhero.whetstone.activity

import android.app.Activity
import androidx.fragment.app.FragmentFactory
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.app.ApplicationScope
import com.deliveryhero.whetstone.injector.MembersInjectorMap
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.deliveryhero.whetstone.app

import android.app.Application
import androidx.lifecycle.ViewModelProvider
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.injector.MembersInjectorMap
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance
import javax.inject.Singleton

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.Context
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.deliveryhero.whetstone.ForScope
import com.deliveryhero.whetstone.injector.MembersInjectorMap
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.ForScope
import dagger.Binds
import dagger.Module
import dagger.Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.deliveryhero.whetstone.fragment

import androidx.fragment.app.Fragment
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.activity.ActivityScope
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import javax.inject.Provider

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.deliveryhero.whetstone.service

import android.app.Service
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.app.ApplicationScope
import com.deliveryhero.whetstone.injector.MembersInjectorMap
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.deliveryhero.whetstone.view

import android.view.View
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.activity.ActivityScope
import com.deliveryhero.whetstone.injector.MembersInjectorMap
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.deliveryhero.whetstone.viewmodel

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.app.ApplicationScope
import com.squareup.anvil.annotations.ContributesSubcomponent
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.optional.SingleIn
import dagger.BindsInstance
import javax.inject.Provider

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
@file:OptIn(ExperimentalCompilerApi::class)

package com.deliveryhero.whetstone.codegen

import com.deliveryhero.whetstone.SingleIn
import com.deliveryhero.whetstone.app.ApplicationComponent
import com.deliveryhero.whetstone.app.ApplicationScope
import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.annotations.MergeComponent
import com.squareup.anvil.annotations.optional.SingleIn
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.STAR
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.JvmCompilationResult
import dagger.Binds
import dagger.Component
import dagger.MembersInjector
Expand All @@ -24,7 +25,8 @@ import kotlin.reflect.full.*
import kotlin.test.assertEquals
import kotlin.test.assertTrue

internal fun KotlinCompilation.Result.validateInstanceBinding(
@OptIn(ExperimentalCompilerApi::class)
internal fun JvmCompilationResult.validateInstanceBinding(
classUnderTest: String,
baseClass: KClass<*>,
scope: KClass<*>
Expand All @@ -43,7 +45,8 @@ internal fun KotlinCompilation.Result.validateInstanceBinding(
assertEquals(baseClass, bindsMethod.returnType.classifier)
}

internal fun KotlinCompilation.Result.validateInjectorBinding(classUnderTest: String, scope: KClass<*>) {
@OptIn(ExperimentalCompilerApi::class)
internal fun JvmCompilationResult.validateInjectorBinding(classUnderTest: String, scope: KClass<*>) {
val clas = classLoader.loadClass(classUnderTest).kotlin
val bindingModule = classLoader.loadClass("${classUnderTest}BindingsModule").kotlin
val membersInjector = MembersInjector::class.asClassName()
Expand All @@ -62,12 +65,13 @@ internal fun KotlinCompilation.Result.validateInjectorBinding(classUnderTest: St
assertEquals(membersInjector.parameterizedBy(STAR), bindsMethod.returnType.asTypeName())
}

internal fun KotlinCompilation.Result.validateAppComponent() {
@OptIn(ExperimentalCompilerApi::class)
internal fun JvmCompilationResult.validateAppComponent() {
val appComponent = classLoader.loadClass("GeneratedApplicationComponent").kotlin

assertTrue(appComponent.isAbstract)
assertTrue(appComponent.hasAnnotation<Singleton>())
assertEquals(ApplicationScope::class, appComponent.findAnnotation<SingleIn>()?.value)
assertEquals(ApplicationScope::class, appComponent.findAnnotation<SingleIn>()?.scope)
assertEquals(ApplicationScope::class, appComponent.findAnnotation<MergeComponent>()?.scope)

val appComponentFactoryCn = appComponent.asClassName().nestedClass("Factory")
Expand Down

0 comments on commit 524b166

Please sign in to comment.