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
# Conflicts:
#	projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinMetaDataScanner.kt
  • Loading branch information
OffRange committed Jan 27, 2025
2 parents 149beca + bb85a51 commit e0e0616
Show file tree
Hide file tree
Showing 46 changed files with 974 additions and 430 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


1 change: 1 addition & 0 deletions examples/android-coffee-maker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ dependencies {
ksp {
arg("KOIN_CONFIG_CHECK","true")
arg("KOIN_DEFAULT_MODULE","true")
arg("KOIN_LOG_TIMES","true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import org.koin.android.ext.android.getKoin
import org.koin.android.ext.android.inject
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.scope.activityRetainedScope
import org.koin.androidx.scope.activityScope
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.core.component.KoinScopeComponent
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope
import org.koin.sample.android.library.MyScope
import org.koin.sample.androidx.app.*
import org.koin.sample.androidx.app.scope.ScopeViewModel
import org.koin.sample.androidx.data.ProvidedComponent
import org.koin.sample.androidx.data.TaskDatasource

class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(), AndroidScopeComponent {

override val scope: Scope by activityRetainedScope()

// inject & ViewModel
val coffeeViewModel : CoffeeViewModel by viewModel()
val myPresenter : MyPresenter by inject { parametersOf(this@MainActivity) }
val todoViewModel : TodoViewModel by viewModel()
val heater : AndroidHeater by inject()
val coffeeFactory : AndroidCoffeeMakerTester by inject()
val scopeVM : ScopeViewModel by viewModel()

private val button : Button by lazy { findViewById(R.id.main_button) }
private val textView : TextView by lazy { findViewById(R.id.main_text) }
Expand All @@ -48,5 +57,7 @@ class MainActivity : AppCompatActivity() {

val scope = getKoin().createScope<MyScope>()
scope.get<ScopedStuff>()

println("VM scope data: ${scopeVM.data.id}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.koin.sample.androidx.app.scope

import androidx.lifecycle.ViewModel
import org.koin.android.annotation.KoinViewModel
import org.koin.core.annotation.Scope
import org.koin.core.annotation.Scoped
import org.koin.sample.androidx.MainActivity
import java.util.UUID

@Scoped
@Scope(MainActivity::class)
class ScopedData {
val id = UUID.randomUUID().toString()
}

@KoinViewModel
@Scope(MainActivity::class)
class ScopeViewModel(val data : ScopedData) : ViewModel()
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-Beta5"
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
1 change: 1 addition & 0 deletions examples/other-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ dependencies {

ksp {
arg("KOIN_CONFIG_CHECK","true")
arg("KOIN_LOG_TIMES","true")
}

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.koin.example.supertype

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

@Module
@ComponentScan
public class SuperTypesModule

public interface A

public open class B : A

public interface D

@Single
public class C : B(),D
Loading

0 comments on commit e0e0616

Please sign in to comment.