From 72715384f1a32d5969d2bd5d526114545c6b2edf Mon Sep 17 00:00:00 2001 From: slam Date: Wed, 1 Jan 2025 12:40:22 +0800 Subject: [PATCH] feat(gemini): integrate Koin dependency injection Add Koin framework for dependency injection in gemini modules, including initialization and module configuration. Adjust KSP plugin settings for Kotlin compilation and manage dependencies. --- .../gemini-client-core/build.gradle.kts | 35 +++++++++++++++++-- .../api/internal/DefaultTextGenerationApi.kt | 2 ++ .../com/tddworks/gemini/di/GeminiModule.kt | 26 ++++++++++++++ .../kotlin/com/tddworks/gemini/di/Koin.kt | 18 ++++++++++ gradle/libs.versions.toml | 2 +- 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/GeminiModule.kt create mode 100644 gemini-client/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/di/Koin.kt diff --git a/gemini-client/gemini-client-core/build.gradle.kts b/gemini-client/gemini-client-core/build.gradle.kts index 9aa47ee..9713d47 100644 --- a/gemini-client/gemini-client-core/build.gradle.kts +++ b/gemini-client/gemini-client-core/build.gradle.kts @@ -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` } @@ -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 { kotlin.srcDir(destinationDirectory) } + + kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin") + dependencies { + // put your Multiplatform dependencies here + api(projects.common) + } } commonTest.dependencies { @@ -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().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("jvmTest") { useJUnitPlatform() diff --git a/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/internal/DefaultTextGenerationApi.kt b/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/internal/DefaultTextGenerationApi.kt index bd154d6..b89a833 100644 --- a/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/internal/DefaultTextGenerationApi.kt +++ b/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/api/textGeneration/api/internal/DefaultTextGenerationApi.kt @@ -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 { diff --git a/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/GeminiModule.kt b/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/GeminiModule.kt new file mode 100644 index 0000000..4c7d052 --- /dev/null +++ b/gemini-client/gemini-client-core/src/commonMain/kotlin/com/tddworks/gemini/di/GeminiModule.kt @@ -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() +//} + +@Module +@ComponentScan("com.tddworks.gemini") +class GeminiModule \ No newline at end of file diff --git a/gemini-client/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/di/Koin.kt b/gemini-client/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/di/Koin.kt new file mode 100644 index 0000000..13511b4 --- /dev/null +++ b/gemini-client/gemini-client-darwin/src/appleMain/kotlin/com/tddworks/gemini/di/Koin.kt @@ -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 + ) + } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 080cab3..1356a3e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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"