Skip to content

Commit

Permalink
Merge branch '2.0.0' into feat-component-scan-glob
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani authored Jan 27, 2025
2 parents 149beca + 93a6805 commit 27c88ff
Show file tree
Hide file tree
Showing 41 changed files with 897 additions and 429 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
pull_request:
branches:
- '*'
push:
branches:
- '*'

concurrency:
group: build-${{ github.ref }}
Expand All @@ -22,7 +25,7 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'zulu'
Expand All @@ -31,10 +34,10 @@ jobs:
- name: Setup Gradle
uses: gradle/gradle-build-action@v2

- name: Install Compiler
- name: Install
run: cd projects && ./install.sh

- name: Run Sandbox Test
- name: Run Tests
run: cd examples && ./test.sh


Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.sample.android.library.CommonModule
import org.koin.sample.androidx.repository.RepositoryModule
import org.koin.sample.clients.ClientModule

@Module(includes = [DataModule::class])
@ComponentScan("org.koin.sample.androidx.app")
class AppModule

@Module(includes = [CommonModule::class, RepositoryModule::class])
@Module(includes = [CommonModule::class, ClientModule::class, RepositoryModule::class])
@ComponentScan("org.koin.sample.androidx.data")
internal class DataModule
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.koin.sample.androidx

import io.ktor.client.HttpClient
import it.example.component.ExampleSingleton
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named
import org.koin.ksp.generated.module
import org.koin.sample.android.library.CommonRepository
import org.koin.sample.android.library.MyScope
Expand Down Expand Up @@ -41,6 +43,8 @@ class AndroidModuleTest {

assert(koin.getOrNull<ExampleSingleton>() != null)

assert(koin.get<HttpClient>(named("clientA")) != koin.get<HttpClient>(named("clientB")))


stopKoin()
}
Expand Down
3 changes: 2 additions & 1 deletion examples/android-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ dependencies {
implementation(libs.android.appcompat)
ksp(libs.koin.ksp)
implementation(project(":coffee-maker-module"))

api(libs.ktor.core)
implementation(libs.ktor.cio)
testImplementation(libs.koin.test)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.koin.sample.clients

import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import org.koin.core.annotation.Module
import org.koin.core.annotation.Named
import org.koin.core.annotation.Single

@Module(includes = [ClientModuleA::class, ClientModuleB::class])
class ClientModule

@Module
class ClientModuleA {

@Single
@Named("clientA")
fun createClient() = HttpClient(CIO) {}
}

@Module
class ClientModuleB {

@Single
@Named("clientB")
fun createClient() = HttpClient(CIO) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import org.koin.core.time.measureDuration
import org.koin.example.coffee.CoffeeMaker
import org.koin.example.di.CoffeeAppModule
import org.koin.example.di.CoffeeTesterModule
import org.koin.example.tea.TeaModule
import org.koin.example.test.ext.ExternalModule
import org.koin.example.test.scope.ScopeModule
import org.koin.ksp.generated.*
import kotlin.time.measureTime

class CoffeeApp : KoinComponent {
val maker: CoffeeMaker by inject()
Expand All @@ -37,13 +37,8 @@ fun main() {
}

val coffeeShop = CoffeeApp()
measureDuration("Got Coffee") {
val t = measureTime {
coffeeShop.maker.brew()
}
}

fun measureDuration(msg: String, code: () -> Unit): Double {
val duration = measureDuration(code)
println("$msg in $duration ms")
return duration
println("Got Coffee in $t")
}
5 changes: 3 additions & 2 deletions examples/coffee-maker/src/test/java/CoffeeAppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.koin.example.coffee.MyDetachCoffeeComponent
import org.koin.example.coffee.pump.PumpCounter
import org.koin.example.di.CoffeeAppModule
import org.koin.example.di.CoffeeTesterModule
import org.koin.example.measureDuration
import org.koin.example.tea.TeaModule
import org.koin.example.tea.TeaPot
import org.koin.example.test.CoffeeMakerTester
Expand All @@ -22,6 +21,7 @@ import org.koin.example.test.include.IncludedComponent
import org.koin.example.test.scope.*
import org.koin.ksp.generated.module
import org.koin.mp.KoinPlatformTools
import kotlin.time.measureTime

class CoffeeAppTest {

Expand All @@ -44,9 +44,10 @@ class CoffeeAppTest {
}

val coffeeShop = CoffeeApp()
measureDuration("Got Coffee") {
val time = measureTime {
coffeeShop.maker.brew()
}
println("Got Coffee in $time")

// Tests
val koin = KoinPlatformTools.defaultContext().get()
Expand Down
5 changes: 4 additions & 1 deletion examples/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ kotlin.code.style=official
#Android
android.useAndroidX=true
androidMinSDK=21
androidCompileSDK=34
androidCompileSDK=34

#KSP
ksp.useKSP2=true
7 changes: 5 additions & 2 deletions examples/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

# Core
kotlin = "2.0.21"
koin = "4.0.1-RC2"
koinAnnotations = "2.0.0-Beta2"
koin = "4.0.2"
koinAnnotations = "2.0.0-Beta4"
ksp = "2.0.21-1.0.28"
junit = "4.13.2"
# Android
agp = "8.3.2"
androidCompat = "1.7.0"
ktor = "2.3.12"

[libraries]
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
Expand All @@ -21,6 +22,8 @@ koin-ksp = { module = "io.insert-koin:koin-ksp-compiler", version.ref = "koinAnn
ksp-api = {module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp"}
android-appcompat = {module = "androidx.appcompat:appcompat", version.ref = "androidCompat"}
junit = {module = "junit:junit",version.ref ="junit"}
ktor-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }

[plugins]
#kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Cat : Animal

public class Bunny(public val color: String) : Animal

@Single
public class Farm(@WhiteBunny public val whiteBunny: Bunny, @BlackBunny public val blackBunny: Bunny)

@Named
public annotation class WhiteBunny

Expand All @@ -22,7 +25,6 @@ public annotation class BlackBunny
@Module
@ComponentScan
public class AnimalModule {

@Factory
public fun animal(cat: Cat, dog: Dog): Animal = if (randomBoolean()) cat else dog

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.koin.example.by.example

import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.core.annotation.Single

@Module
@ComponentScan
public class ByModule

@Single
public class ByExampleSingle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.koin.example.defaultparam

import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Factory
import org.koin.core.annotation.InjectedParam
import org.koin.core.annotation.Module

@Module
@ComponentScan
public class MyModule

public const val COMPONENT_DEFAULT: String = "default"

@Factory
public class Component(@InjectedParam public val param: String = COMPONENT_DEFAULT)

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class MyScope
@Scoped
public class MyScopedInstance

@Scope(name = "my_scope")
@Factory
public class MyScopeFactory(
public val oc : MyOtherComponent,
Expand Down
26 changes: 22 additions & 4 deletions examples/other-ksp/src/test/kotlin/org.koin.example/TestModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import org.junit.Test
import org.koin.core.Koin
import org.koin.core.error.NoDefinitionFoundException
import org.koin.core.logger.Level
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named
import org.koin.core.qualifier.qualifier
import org.koin.dsl.koinApplication
import org.koin.example.animal.*
import org.koin.example.by.example.ByExampleSingle
import org.koin.example.by.example.ByModule
import org.koin.example.defaultparam.COMPONENT_DEFAULT
import org.koin.example.defaultparam.Component
import org.koin.example.defaultparam.MyModule
import org.koin.example.`interface`.MyInterfaceExt
import org.koin.example.newmodule.*
import org.koin.example.newmodule.ComponentWithProps.Companion.DEFAULT_ID
Expand All @@ -20,6 +26,7 @@ import org.koin.ksp.generated.defaultModule
import org.koin.ksp.generated.module
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class TestModule {
Expand All @@ -34,7 +41,9 @@ class TestModule {
MyModule3().module,
MyModule2().module,
AnimalModule().module,
ScopeModule().module
ScopeModule().module,
ByModule().module,
MyModule().module
)
}.koin

Expand All @@ -58,22 +67,31 @@ class TestModule {
assertTrue { animals.any { it is Cat } }

val scope = koin.createScope("my_scope_id", named("my_scope"))

assertTrue {
koin.get<MyScopeFactory>().msi == scope.get<MyScopedInstance>()
scope.get<MyScopeFactory>().msi == scope.get<MyScopedInstance>()
}

assertTrue {
koin.get<MyScopeFactory>().msi == koin.get<MyScopeFactory>().msi
scope.get<MyScopeFactory>().msi == scope.get<MyScopeFactory>().msi
}

assertFailsWith(NoDefinitionFoundException::class) {
koin.get<Bunny>()
}

assertEquals("White", koin.get<Bunny>(qualifier<WhiteBunny>()).color)

val farm = koin.get<Farm>()
assertEquals("White", farm.whiteBunny.color)
assertEquals("Black", farm.blackBunny.color)

assertNotNull(koin.getOrNull<ByExampleSingle>())

assertEquals(COMPONENT_DEFAULT,koin.get<Component>().param) // display warning in build logs
}



private fun randomGetAnimal(koin: Koin): Animal {
val a = koin.get<Animal>()
println("animal: $a")
Expand Down
1 change: 1 addition & 0 deletions examples/test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh

./gradlew testDebug --no-build-cache
./gradlew :other-ksp:test :coffee-maker:test :compile-perf:test --no-build-cache
3 changes: 2 additions & 1 deletion projects/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ org.gradle.parallel=true
#Kotlin
kotlin.code.style=official
#Koin
koinAnnotationsVersion=2.0.0-Beta3
koinAnnotationsVersion=2.0.0-Beta4
#Android
android.useAndroidX=true
androidMinSDK=14
androidCompileSDK=34

#android.nonTransitiveRClass=true
2 changes: 1 addition & 1 deletion projects/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Core
kotlin = "2.0.21"
koin = "4.0.1"
koin = "4.0.2"
ksp = "2.0.21-1.0.28"
publish = "2.0.0"
dokka = "1.9.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,4 @@ annotation class ComponentScan(val value: String = "")
* Tag a dependency as already provided by Koin (like DSL declaration, or internals)
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER)
annotation class Provided

/**
* Internal usage for components discovery in generated package
*
* @param value: package of declared definition
*/
@Target(AnnotationTarget.CLASS, AnnotationTarget.FIELD, AnnotationTarget.FUNCTION)
annotation class Definition(val value: String = "")
annotation class Provided
Loading

0 comments on commit 27c88ff

Please sign in to comment.