Skip to content

Commit

Permalink
feat(gemini): integrate Koin dependency injection
Browse files Browse the repository at this point in the history
Add Koin framework for dependency injection in gemini modules, including initialization and module configuration. Adjust KSP plugin settings for Kotlin compilation and manage dependencies.
  • Loading branch information
hanrw committed Jan 1, 2025
1 parent bacd36d commit 7271538
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
35 changes: 32 additions & 3 deletions gemini-client/gemini-client-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import com.google.devtools.ksp.gradle.KspTaskMetadata
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlinx.serialization)
alias(libs.plugins.kover)
alias(libs.plugins.ksp)
`maven-publish`
}

Expand All @@ -10,9 +14,15 @@ kotlin {
macosX64()

sourceSets {
commonMain.dependencies {
// put your Multiplatform dependencies here
api(projects.common)
commonMain {
// https://github.com/google/ksp/issues/963#issuecomment-1894144639
tasks.withType<KspTaskMetadata> { kotlin.srcDir(destinationDirectory) }

kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
dependencies {
// put your Multiplatform dependencies here
api(projects.common)
}
}

commonTest.dependencies {
Expand Down Expand Up @@ -41,6 +51,25 @@ kotlin {
}
}

// KSP Tasks
dependencies {
add("kspCommonMainMetadata", libs.koin.ksp.compiler)
}

// WORKAROUND: ADD this dependsOn("kspCommonMainKotlinMetadata") instead of above dependencies
tasks.withType<KotlinCompile>().configureEach {
if (name != "kspCommonMainKotlinMetadata") {
dependsOn("kspCommonMainKotlinMetadata")
}
}


ksp {
arg("KOIN_DEFAULT_MODULE", "false")
// https://insert-koin.io/docs/reference/koin-annotations/start#compile-safety---check-your-koin-config-at-compile-time-since-130
arg("KOIN_CONFIG_CHECK", "true")
}

tasks {
named<Test>("jvmTest") {
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import com.tddworks.gemini.api.textGeneration.api.TextGeneration
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.flow.Flow
import org.koin.core.annotation.Single

@Single
class DefaultTextGenerationApi(
private val requester: HttpRequester
) : TextGeneration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.tddworks.gemini.di

import com.tddworks.common.network.api.ktor.api.HttpRequester
import com.tddworks.common.network.api.ktor.internal.*
import com.tddworks.di.commonModule
import kotlinx.serialization.json.Json
import org.koin.core.context.startKoin
import org.koin.core.qualifier.named
import org.koin.dsl.KoinAppDeclaration
import org.koin.dsl.module
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module

//
//fun initGemini(
// appDeclaration: KoinAppDeclaration = {}
//): HttpRequester {
// return startKoin {
// appDeclaration()
// modules(commonModule(false) + geminiModules())
// }.koin.get<HttpRequester>()
//}

@Module
@ComponentScan("com.tddworks.gemini")
class GeminiModule
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tddworks.gemini.di

import com.tddworks.di.commonModule
import org.koin.core.context.startKoin
import org.koin.dsl.KoinAppDeclaration
import org.koin.ksp.generated.module

fun initGemini(
enableNetworkLogs: Boolean = false,
appDeclaration: KoinAppDeclaration = {}
) =
startKoin {
appDeclaration()
modules(
commonModule(enableNetworkLogs = enableNetworkLogs),
GeminiModule().module
)
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ com-linecorp-build-recipe-plugin = "1.1.1"

dokka = "1.9.20"

ksp = "2.1.0-1.0.29"
ksp = "2.0.20-1.0.25"

android-minSdk = "24"
android-compileSdk = "34"
Expand Down

0 comments on commit 7271538

Please sign in to comment.