-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from OffRange/feat-component-scan-glob
feat: @componentscan glob support
- Loading branch information
Showing
21 changed files
with
504 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
plugins { | ||
alias(libs.plugins.ksp) | ||
kotlin("jvm") | ||
} | ||
|
||
sourceSets.main { | ||
java.srcDirs("build/generated/ksp/main/kotlin") | ||
} | ||
|
||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenLocal() | ||
google() | ||
} | ||
|
||
dependencies { | ||
implementation(libs.koin.core) | ||
implementation(libs.koin.annotations) | ||
ksp(libs.koin.ksp) | ||
implementation(project(":coffee-maker-module")) | ||
|
||
testImplementation(libs.koin.ksp) | ||
testImplementation(libs.koin.test) | ||
testImplementation(libs.junit) | ||
} | ||
|
||
ksp { | ||
arg("KOIN_CONFIG_CHECK", "true") | ||
} |
42 changes: 42 additions & 0 deletions
42
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/CoffeeGlobApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.koin.example | ||
|
||
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.example.coffee.CoffeeMaker | ||
import org.koin.example.di.CoffeeAppAndTesterModule | ||
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() | ||
} | ||
|
||
// be sure to import "import org.koin.ksp.generated.*" | ||
|
||
fun main() { | ||
startKoin { | ||
printLogger(Level.DEBUG) | ||
// if no module | ||
// defaultModule() | ||
|
||
// else let's use our modules | ||
modules( | ||
CoffeeAppAndTesterModule().module, | ||
TeaModule().module, | ||
ExternalModule().module, | ||
org.koin.example.test.ext2.ExternalModule().module, | ||
ScopeModule().module | ||
) | ||
} | ||
|
||
val coffeeShop = CoffeeApp() | ||
val t = measureTime { | ||
coffeeShop.maker.brew() | ||
} | ||
println("Got Coffee in $t") | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/CoffeeMaker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.koin.example.coffee | ||
|
||
import org.koin.core.annotation.Singleton | ||
import org.koin.example.coffee.pump.Pump | ||
|
||
@Singleton | ||
class CoffeeMaker(private val pump: Pump, private val heater: Heater) { | ||
|
||
fun brew() { | ||
heater.on() | ||
pump.pump() | ||
println(" [_]P coffee! [_]P ") | ||
heater.off() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/CoffeePumpList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.koin.example.coffee | ||
|
||
import org.koin.core.annotation.Single | ||
import org.koin.example.coffee.pump.Pump | ||
|
||
@Single | ||
class CoffeePumpList(val list : List<Pump>) |
20 changes: 20 additions & 0 deletions
20
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/ElectricHeater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.koin.example.coffee | ||
|
||
import org.koin.core.annotation.Single | ||
|
||
@Single | ||
class ElectricHeater : Heater { | ||
|
||
private var heating: Boolean = false | ||
|
||
override fun on() { | ||
println("~ ~ ~ heating ~ ~ ~") | ||
heating = true | ||
} | ||
|
||
override fun off() { | ||
heating = false | ||
} | ||
|
||
override fun isHot(): Boolean = heating | ||
} |
7 changes: 7 additions & 0 deletions
7
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/Heater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.koin.example.coffee | ||
|
||
interface Heater { | ||
fun on() | ||
fun off() | ||
fun isHot(): Boolean | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/pump/FakePump.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.koin.example.coffee.pump | ||
|
||
import org.koin.core.annotation.Single | ||
|
||
@Single | ||
class FakePump : Pump { | ||
override fun pump() { | ||
println("fake pump") | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/pump/Pump.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.koin.example.coffee.pump | ||
|
||
interface Pump { | ||
fun pump() | ||
} |
5 changes: 5 additions & 0 deletions
5
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/pump/PumpCounter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.koin.example.coffee.pump | ||
|
||
class PumpCounter(val pump : List<Pump>){ | ||
val count = pump.size | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/coffee/pump/Thermosiphon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.koin.example.coffee.pump | ||
|
||
import org.koin.core.annotation.Single | ||
import org.koin.example.coffee.Heater | ||
|
||
@Single | ||
class Thermosiphon(private val heater: Heater) : Pump { | ||
override fun pump() { | ||
if (heater.isHot()) { | ||
println("=> => pumping => =>") | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/di/CoffeeAppAndTesterModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.koin.example.di | ||
|
||
import org.koin.core.annotation.ComponentScan | ||
import org.koin.core.annotation.Module | ||
import org.koin.core.annotation.Single | ||
import org.koin.example.coffee.CoffeeMaker | ||
import org.koin.example.coffee.pump.Pump | ||
import org.koin.example.coffee.pump.PumpCounter | ||
import org.koin.example.test.CoffeeMakerTesterTest | ||
import org.koin.example.test.CoffeeMakerTesterTestImpl | ||
|
||
/** | ||
* CoffeeAppAndTesterModule | ||
* | ||
* This module is responsible for configuring component scanning for both the Coffee (glob) application | ||
* and its associated tester components. | ||
* | ||
* It scans the following package patterns: | ||
* - "org.koin.example.coff*.**" | ||
* - "org.koin.example.coff*" | ||
* - "org.koin.example.tes*" | ||
* | ||
* Note: The two patterns for the coffee components ("org.koin.example.coff*.**" and "org.koin.example.coff*") | ||
* can be consolidated into a single, more concise pattern "org.koin.example.coff**". | ||
*/ | ||
@Module | ||
@ComponentScan("org.koin.example.coff*.**", "org.koin.example.coff*", "org.koin.example.tes*") | ||
class CoffeeAppAndTesterModule { | ||
|
||
@Single | ||
fun pumpCounter(list: List<Pump>) = PumpCounter(list) | ||
|
||
|
||
@Single | ||
fun CoffeeMakerTesterTest(coffeeMaker: CoffeeMaker): CoffeeMakerTesterTest = CoffeeMakerTesterTestImpl(coffeeMaker) | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/tea/TeaModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.koin.example.tea | ||
|
||
import org.koin.core.annotation.ComponentScan | ||
import org.koin.core.annotation.Module | ||
|
||
@Module | ||
@ComponentScan | ||
class TeaModule |
7 changes: 7 additions & 0 deletions
7
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/tea/TeaPot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.koin.example.tea | ||
|
||
import org.koin.core.annotation.Single | ||
import org.koin.example.coffee.Heater | ||
|
||
@Single | ||
class TeaPot(val heater: Heater) |
20 changes: 20 additions & 0 deletions
20
examples/coffee-maker-glob/src/main/kotlin/org/koin/example/test/CoffeeMakerTester.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.koin.example.test | ||
|
||
import org.koin.core.annotation.Factory | ||
import org.koin.core.annotation.Named | ||
import org.koin.core.annotation.Single | ||
import org.koin.example.coffee.CoffeeMaker | ||
|
||
@Factory | ||
@Named("test") | ||
class CoffeeMakerTester(val coffeeMaker: CoffeeMaker) | ||
|
||
interface CoffeeMakerTesterTest { | ||
fun coffeeTest() | ||
} | ||
|
||
class CoffeeMakerTesterTestImpl(val coffeeMaker: CoffeeMaker) : CoffeeMakerTesterTest { | ||
override fun coffeeTest() { | ||
coffeeMaker.brew() | ||
} | ||
} |
Oops, something went wrong.