Compare commits

...

37 Commits

Author SHA1 Message Date
23590be5de Update CHANGELOG.md 2023-01-04 20:20:25 +06:00
94acc3c93b Update libs.versions.toml 2023-01-04 08:56:46 +06:00
5616326a3b start 0.16.5 2023-01-04 08:50:19 +06:00
7601860c5c Merge pull request #214 from InsanusMokrassar/0.16.4
0.16.4
2022-12-27 19:03:35 +06:00
8b43d785cc update changelog 2022-12-27 18:51:01 +06:00
b62d3a0b7d launchInCurrentThread 2022-12-27 18:50:41 +06:00
fad73c7213 start 0.16.4 2022-12-27 18:49:04 +06:00
2403c7c2b0 Merge pull request #213 from InsanusMokrassar/0.16.3
0.16.3
2022-12-25 13:24:37 +06:00
fa090bf920 Update StartLauncherPlugin.kt 2022-12-25 10:29:16 +06:00
a83ee86340 improvements in startup launcher 2022-12-25 10:26:44 +06:00
204955bcce add template for startup and readme 2022-12-19 17:51:05 +06:00
ee56e9543a start 0.16.3 2022-12-17 17:27:15 +06:00
96fdff6ffd Merge pull request #212 from InsanusMokrassar/0.16.2
0.16.2
2022-12-16 13:59:47 +06:00
58b007cbb3 fill changelog 2022-12-16 13:59:24 +06:00
4f0c139889 remove redundant stop koin expect-actual 2022-12-16 11:58:18 +06:00
c584c24fce fixes and improvements 2022-12-15 15:07:10 +06:00
85e5cee154 replace createStartupPluginAndRegister 2022-12-15 14:59:07 +06:00
5af91981f1 upgrades and filling of README 2022-12-15 10:26:31 +06:00
2fe4f08059 update compose 2022-12-15 08:51:53 +06:00
83796f345a start fix startup 2022-12-14 22:26:23 +06:00
c1e21364a6 start 0.16.2 2022-12-14 21:40:18 +06:00
067d9d0d3b Merge pull request #211 from InsanusMokrassar/0.16.1
0.16.1
2022-12-09 19:58:54 +06:00
03f527d83e Update CHANGELOG.md 2022-12-09 19:46:58 +06:00
ced05a4586 improve default runCatchingSafely/safelyWithResult and add suspend variances of safe/unsafe SafeWrapper interface 2022-12-09 12:14:24 +06:00
43fe06206a add safe wrapper 2022-12-09 11:09:32 +06:00
023657558e start 0.16.1 2022-12-09 10:52:53 +06:00
9b0b726c80 Merge pull request #210 from InsanusMokrassar/0.16.0
0.16.0
2022-12-08 09:29:12 +06:00
4ee67321c4 fill changelog and update android dependencies 2022-12-08 09:26:59 +06:00
59f1f2e59b Update libs.versions.toml 2022-12-08 08:53:37 +06:00
0766d48b7c Update libs.versions.toml 2022-12-08 07:56:04 +06:00
e18903b9e9 Update gradle.properties 2022-12-08 07:54:53 +06:00
d0eecdead2 Update gradle.properties 2022-12-08 07:53:59 +06:00
cc4a83a033 Update gradle.properties 2022-12-08 07:53:08 +06:00
1cf911bbde Update build.gradle 2022-12-07 19:40:30 +06:00
36d73d5023 Update StartupLaunchingTests.kt 2022-12-07 11:39:45 +06:00
c395242e3e fixes in StartupLauncingTests 2022-12-07 10:50:51 +06:00
cd9cd7cc5d Merge pull request #208 from InsanusMokrassar/0.15.1
0.15.1
2022-12-07 09:43:48 +06:00
43 changed files with 743 additions and 50 deletions

View File

@@ -1,5 +1,41 @@
# Changelog
## 0.16.5
* `Versions`:
* `Ktor`: `2.2.1` -> `2.2.2`
## 0.16.4
* `Coroutines`:
* Create `launchInCurrentThread`
## 0.16.3
* `Startup`:
* `Launcher`:
* All starting API have been moved into `StartLauncherPlugin` and do not require serialize/deserialize cycle for now
## 0.16.2
* `Versions`:
* `Compose`: `1.2.1` -> `1.2.2`
* `Startup`:
* Module become available on `JS` target
## 0.16.1
* `Coroutines`:
* New `runCatchingSafely`/`safelyWithResult` with receivers
* `SafeWrapper`:
* Module inited
## 0.16.0
* `Versions`:
* `Ktor`: `2.1.3` -> `2.2.1`
* `Android Fragment`: `1.5.3` -> `1.5.5`
## 0.15.1
* `Startup`:

View File

@@ -22,6 +22,7 @@ kotlin {
dependencies {
api libs.kt.coroutines.android
}
dependsOn(jvmMain)
}
}
}

View File

@@ -115,10 +115,21 @@ suspend inline fun <T> runCatchingSafely(
safely(onException, block)
}
suspend inline fun <T, R> T.runCatchingSafely(
noinline onException: ExceptionHandler<R> = defaultSafelyExceptionHandler,
noinline block: suspend T.() -> R
): Result<R> = runCatching {
safely(onException) { block() }
}
suspend inline fun <T> safelyWithResult(
noinline block: suspend CoroutineScope.() -> T
): Result<T> = runCatchingSafely(defaultSafelyExceptionHandler, block)
suspend inline fun <T, R> T.safelyWithResult(
noinline block: suspend T.() -> R
): Result<R> = runCatchingSafely(defaultSafelyExceptionHandler, block)
/**
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
* returning null at one time

View File

@@ -0,0 +1,9 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
fun <T> launchInCurrentThread(block: suspend CoroutineScope.() -> T): T {
val scope = CoroutineScope(Dispatchers.Unconfined)
return scope.launchSynchronously(block)
}

View File

@@ -6,7 +6,7 @@ fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T
var result: Result<T>? = null
val objectToSynchronize = Object()
synchronized(objectToSynchronize) {
launch {
launch(start = CoroutineStart.UNDISPATCHED) {
result = safelyWithResult(block)
}.invokeOnCompletion {
synchronized(objectToSynchronize) {

View File

@@ -0,0 +1,47 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.assertEquals
class LaunchInCurrentThreadTests {
@Test
fun simpleTestThatLaunchInCurrentThreadWorks() {
val expectedResult = 10
val result = launchInCurrentThread {
expectedResult
}
assertEquals(expectedResult, result)
}
@Test
fun simpleTestThatSeveralLaunchInCurrentThreadWorks() {
val testData = 0 until 100
testData.forEach {
val result = launchInCurrentThread {
it
}
assertEquals(it, result)
}
}
@Test
fun simpleTestThatLaunchInCurrentThreadWillCorrectlyHandleSuspensionsWorks() {
val testData = 0 until 100
suspend fun test(data: Any): Any {
return withContext(Dispatchers.Default) {
delay(1)
data
}
}
testData.forEach {
val result = launchInCurrentThread {
test(it)
}
assertEquals(it, result)
}
}
}

View File

@@ -23,6 +23,7 @@ allprojects {
mppProjectWithSerializationPresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerialization.gradle"
mppProjectWithSerializationAndComposePresetPath = "${rootProject.projectDir.absolutePath}/mppProjectWithSerializationAndCompose.gradle"
mppJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJavaProject.gradle"
mppJsAndJavaProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppJsAndJavaProject.gradle"
mppAndroidProjectPresetPath = "${rootProject.projectDir.absolutePath}/mppAndroidProject.gradle"
defaultAndroidSettingsPresetPath = "${rootProject.projectDir.absolutePath}/defaultAndroidSettings.gradle"

View File

@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
# Project data
group=dev.inmo
version=0.15.1
android_code_version=167
version=0.16.5
android_code_version=173

View File

@@ -6,26 +6,26 @@ kt-coroutines = "1.6.4"
kslog = "0.5.4"
jb-compose = "1.2.1"
jb-compose = "1.2.2"
jb-exposed = "0.41.1"
jb-dokka = "1.7.20"
klock = "3.4.0"
uuid = "0.6.0"
ktor = "2.1.3"
ktor = "2.2.2"
gh-release = "2.4.1"
koin = "3.2.2"
android-gradle = "7.2.2"
android-gradle = "7.3.0"
dexcount = "3.1.0"
android-coreKtx = "1.9.0"
android-recyclerView = "1.2.1"
android-appCompat = "1.5.1"
android-fragment = "1.5.3"
android-fragment = "1.5.5"
android-espresso = "3.4.0"
android-test = "1.1.3"

View File

@@ -0,0 +1,49 @@
project.version = "$version"
project.group = "$group"
apply from: "$publishGradlePath"
kotlin {
jvm {
compilations.main {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
js (IR) {
browser()
nodejs()
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib')
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmTest {
dependencies {
implementation kotlin('test-junit')
}
}
jsTest {
dependencies {
implementation kotlin('test-js')
implementation kotlin('test-junit')
}
}
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

17
safe_wrapper/build.gradle Normal file
View File

@@ -0,0 +1,17 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":micro_utils.coroutines")
}
}
}
}

View File

@@ -0,0 +1,17 @@
package dev.inmo.micro_utils.safe_wrapper
import dev.inmo.micro_utils.coroutines.runCatchingSafely
interface SafeWrapper<T> {
fun <R> safe(block: T.() -> R): Result<R> = unsafeTarget().runCatching(block)
fun <R> unsafe(block: T.() -> R): R = unsafeTarget().block()
suspend fun <R> safeS(block: suspend T.() -> R): Result<R> = unsafeTarget().runCatchingSafely(block = block)
suspend fun <R> unsafeS(block: suspend T.() -> R): R = unsafeTarget().block()
fun unsafeTarget(): T
class Default<T>(private val t: T) : SafeWrapper<T> { override fun unsafeTarget(): T = t }
companion object {
operator fun <T> invoke(t: T) = Default(t)
}
}

View File

@@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.safe_wrapper"/>

View File

@@ -4,6 +4,7 @@ String[] includes = [
":common",
":common:compose",
":matrix",
":safe_wrapper",
":crypto",
":koin",
":selector:common",

View File

@@ -0,0 +1,92 @@
# Startup Plugin Launcher
This module contains tools to start your plugin system.
## Config
Base config is pretty simple:
```json
{
"plugins": [
"dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin"
]
}
```
So, `"dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin"` is the fully qualified name of plugin you wish to be
included in the server.
> JS note: In JS there are no opportunity to determine object type by its full name. Because of it, in JS developers
> should prefer to use `Config` in their kotlin code directly instead of json config passing. More info see in [JS](#js)
> section
## JVM
For JVM target you may use main class by path: `dev.inmo.micro_utils.startup.launcher.MainKt`
It is expected, that you will pass the main ONE argument with path to the config json. Sample of launching:
```bash
./gradlew run --args="sample.config.json"
```
Content of `sample.config.json` described in [Config](#config) section.
You may build runnable app using:
```bash
./gradlew assembleDist
```
In that case in `build/distributions` folder you will be able to find zip and tar files with all required
tools for application running (via their `bin/app_name` binary). In that case yoy will not need to pass
`--args=...` and launch will look like `./bin/app_name sample.config.json`
## JS
In JS for starting of your plugins app, you should use `PluginsStarter` in your code:
```kotlin
PluginsStarter.startPlugins(
Config(HelloWorldPlugin)
)
```
`Config` here is deserialized variant from [Config](#config) section. As was said in [Config](#config) section, in JS
there is no way to find classes/objects by their full qualifiers. Because of it you should use some way to register your
plugins in `StartPluginSerializer` or use the code like in the snippet above: there plugins will be registered
automatically.
In case you wish to register your plugins manually and run server from config, you should use one of the ways to register
plugin on start.
Sample with `EagerInitialization`: [Kotlin JS doc about lazy initialization](https://kotlinlang.org/docs/js-ir-compiler.html#incremental-compilation-for-development-binaries),
[@EagerInitialization docs](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-eager-initialization/):
```kotlin
@ExperimentalStdlibApi
@EagerInitialization
val plugin = createStartupPluginAndRegister("PluginNameToUseInConfig") {
// Your plugin creation. For example:
HelloWorldPlugin
}
```
So, in that case you will be able to load plugins list as `JsonObject` from anywhere and start plugins app with it:
```kotlin
PluginsStarter.startPlugins(
jsonObject
)
```
It will load `HelloWorldPlugin` if `jsonObject` have next content:
```json
{
"plugins": [
"PluginNameToUseInConfig"
]
}
```

View File

@@ -4,7 +4,7 @@ plugins {
id "application"
}
apply from: "$mppJavaProjectPresetPath"
apply from: "$mppJsAndJavaProjectPresetPath"
kotlin {
sourceSets {
@@ -13,11 +13,16 @@ kotlin {
api internalProject("micro_utils.startup.plugin")
}
}
commonTest {
dependencies {
implementation libs.kt.coroutines.test
}
}
}
}
application {
mainClassName = "dev.inmo.micro_utils.startup.launcher.ServerLauncherKt"
mainClassName = "dev.inmo.micro_utils.startup.launcher.MainKt"
}
java {

View File

@@ -1,10 +1,8 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
import kotlinx.serialization.json.JsonObject
import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext
import org.koin.dsl.module
/**
* Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base
@@ -13,19 +11,7 @@ import org.koin.dsl.module
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will
* deserialize it in its [StartLauncherPlugin.setupDI]
*/
@Deprecated("Fully replaced with StartLauncherPlugin#start", ReplaceWith("StartLauncherPlugin.start(rawConfig)", "dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin"))
suspend fun start(rawConfig: JsonObject) {
with(StartLauncherPlugin) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(rawConfig)
}
)
logger.i("Modules loaded")
GlobalContext.startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("App has been setup")
}
StartLauncherPlugin.start(rawConfig)
}

View File

@@ -4,6 +4,8 @@ import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.taggedLogger
import dev.inmo.kslog.common.w
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.startPlugin
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -12,7 +14,10 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.SerialFormat
import kotlinx.serialization.StringFormat
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin
import org.koin.core.module.Module
import org.koin.dsl.binds
import org.koin.dsl.module
@@ -23,26 +28,20 @@ import org.koin.dsl.module
object StartLauncherPlugin : StartPlugin {
internal val logger = taggedLogger(this)
/**
* Will deserialize [Config] from [config], register it in receiver [Module] (as well as [CoroutineScope] and
* [kotlinx.serialization.json.Json])
*
* Besides, in this method will be called [StartPlugin.setupDI] on each plugin from [Config.plugins]. In case when
* some plugin will not be loaded correctly it will be reported throw the [logger]
*/
override fun Module.setupDI(config: JsonObject) {
val pluginsConfig = defaultJson.decodeFromJsonElement(Config.serializer(), config)
fun Module.setupDI(config: Config, rawJsonObject: JsonObject? = null) {
val rawJsonObject = rawJsonObject ?: defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
single { pluginsConfig }
single { rawJsonObject }
single { config }
single { CoroutineScope(Dispatchers.Default) }
single { defaultJson } binds arrayOf(StringFormat::class, SerialFormat::class)
includes(
pluginsConfig.plugins.mapNotNull {
config.plugins.mapNotNull {
runCatching {
module {
with(it) {
setupDI(config)
setupDI(rawJsonObject)
}
}
}.onFailure { e ->
@@ -52,11 +51,28 @@ object StartLauncherPlugin : StartPlugin {
)
}
/**
* Will deserialize [Config] from [config], register it in receiver [Module] (as well as [CoroutineScope] and
* [kotlinx.serialization.json.Json])
*
* Besides, in this method will be called [StartPlugin.setupDI] on each plugin from [Config.plugins]. In case when
* some plugin will not be loaded correctly it will be reported throw the [logger]
*/
override fun Module.setupDI(config: JsonObject) {
logger.i("Koin for current module has started setup")
setupDI(
defaultJson.decodeFromJsonElement(Config.serializer(), config),
config
)
logger.i("Koin for current module has been setup")
}
/**
* Takes [CoroutineScope] and [Config] from the [koin], and call starting of each plugin from [Config.plugins]
* ASYNCHRONOUSLY. Just like in [setupDI], in case of fail in some plugin it will be reported using [logger]
*/
override suspend fun startPlugin(koin: Koin) {
logger.i("Start starting of subplugins")
val scope = koin.get<CoroutineScope>()
koin.get<Config>().plugins.map { plugin ->
scope.launch {
@@ -66,11 +82,60 @@ object StartLauncherPlugin : StartPlugin {
startPlugin(koin)
}
}.onFailure { e ->
logger.w("Unable to load bot part of $plugin", e)
logger.w("Unable to start plugin $plugin", e)
}.onSuccess {
logger.i("Complete loading of $plugin")
}
}
}.joinAll()
logger.i("Complete subplugins start")
}
/**
* Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base
* plugin
*
* @param rawConfig It is expected that this [JsonObject] will contain serialized [Config] ([StartLauncherPlugin] will
* deserialize it in its [StartLauncherPlugin.setupDI]
*/
suspend fun start(rawConfig: JsonObject) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
koinApp.modules(
module {
setupDI(rawConfig)
}
)
logger.i("Modules loaded")
startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("App has been setup")
}
/**
* Will create [KoinApplication], init, load modules using [StartLauncherPlugin] and start plugins using the same base
* plugin
*
* @param config In difference with other [start] method here config is already deserialized and this config will
* be converted to [JsonObject] as raw config. That means that all plugins from [config] will receive
* serialized version of [config] in [StartPlugin.setupDI] method
*/
suspend fun start(config: Config) {
logger.i("Start initialization")
val koinApp = KoinApplication.init()
logger.i("Koin app created")
koinApp.modules(
module {
setupDI(config)
}
)
startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
}
}

View File

@@ -1,37 +1,43 @@
import dev.inmo.micro_utils.coroutines.launchSynchronously
import dev.inmo.micro_utils.startup.launcher.Config
import dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin
import dev.inmo.micro_utils.startup.launcher.defaultJson
import dev.inmo.micro_utils.startup.launcher.start
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import kotlinx.serialization.json.jsonObject
import org.koin.core.context.stopKoin
import kotlin.test.BeforeTest
import kotlin.test.Test
class StartupLaunchingTests {
@Test(timeout = 1000L)
@BeforeTest
fun resetGlobalKoinContext() {
runCatching { stopKoin() }
}
@Test
fun CheckThatEmptyPluginsListLeadsToEndOfMain() {
val emptyJson = defaultJson.encodeToJsonElement(
Config.serializer(),
Config(emptyList())
).jsonObject
launchSynchronously {
runTest {
val job = launch {
start(emptyJson)
StartLauncherPlugin.start(emptyJson)
}
job.join()
}
}
@Test(timeout = 1000L)
@Test
fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() {
val emptyJson = defaultJson.encodeToJsonElement(
Config.serializer(),
Config(listOf(HelloWorldPlugin))
).jsonObject
launchSynchronously {
runTest {
val job = launch {
start(emptyJson)
StartLauncherPlugin.start(emptyJson)
}
job.join()
}

View File

@@ -0,0 +1,25 @@
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.i
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
@Deprecated("Useless due to including of the same functionality in StrtLauncherPlugin")
object PluginsStarter {
init {
KSLog.default = KSLog("Launcher")
}
/**
* It is expected that you have registered all the [dev.inmo.micro_utils.startup.plugin.StartPlugin]s of your JS
* app inside of [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer] using its
* [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer.registerPlugin] method
*/
suspend fun startPlugins(json: JsonObject) = StartLauncherPlugin.start(json)
/**
* Will convert [config] to [JsonObject] with auto registration of [dev.inmo.micro_utils.startup.plugin.StartPlugin]s
* in [dev.inmo.micro_utils.startup.plugin.StartPluginSerializer]
*/
suspend fun startPlugins(config: Config) = StartLauncherPlugin.start(config)
}

View File

@@ -26,12 +26,12 @@ import java.io.File
*/
suspend fun main(args: Array<String>) {
KSLog.default = KSLog("ServerLauncher")
KSLog.default = KSLog("Launcher")
val (configPath) = args
val file = File(configPath)
KSLog.i("Start read config from ${file.absolutePath}")
val json = defaultJson.parseToJsonElement(file.readText()).jsonObject
KSLog.i("Config has been read")
start(json)
StartLauncherPlugin.start(json)
}

View File

@@ -3,7 +3,7 @@ plugins {
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppJavaProjectPresetPath"
apply from: "$mppJsAndJavaProjectPresetPath"
kotlin {
sourceSets {
@@ -16,5 +16,10 @@ kotlin {
api project(":micro_utils.coroutines")
}
}
jsMain {
dependencies {
api libs.uuid
}
}
}
}

View File

@@ -0,0 +1,38 @@
package dev.inmo.micro_utils.startup.plugin
import com.benasher44.uuid.uuid4
import kotlin.reflect.KClass
/**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin
*
* @param name Will be used as a key for registration in [StartPluginSerializer] and will be passed to the [block] as
* parameter
*/
inline fun <T : StartPlugin> createStartupPluginAndRegister(
name: String = uuid4().toString(),
block: (String) -> T
): T {
val plugin = block(name)
StartPluginSerializer.registerPlugin(name, plugin)
return plugin
}
/**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin
*/
inline fun <T : StartPlugin> createStartupPluginAndRegister(
kClass: KClass<T>,
name: String = uuid4().toString(),
block: (String) -> T
): T = createStartupPluginAndRegister("${kClass.simpleName}_$name", block)
/**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin
*/
inline fun <reified T : StartPlugin> createStartupPluginAndRegister(
block: (String) -> T
): T = createStartupPluginAndRegister(T::class, uuid4().toString(), block)

View File

@@ -0,0 +1,35 @@
package dev.inmo.micro_utils.startup.plugin
import com.benasher44.uuid.uuid4
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
actual object StartPluginSerializer : KSerializer<StartPlugin> {
private val registeredPlugins = mutableMapOf<String, StartPlugin>()
private val registeredPluginsByPlugin = mutableMapOf<StartPlugin, String>()
override val descriptor: SerialDescriptor = String.serializer().descriptor
override fun deserialize(decoder: Decoder): StartPlugin {
val name = decoder.decodeString()
return registeredPlugins[name] ?: error("Unable to find startup plugin for $name")
}
override fun serialize(encoder: Encoder, value: StartPlugin) {
val name = registeredPluginsByPlugin[value] ?: uuid4().toString().also {
registeredPlugins[it] = value
registeredPluginsByPlugin[value] = it
}
encoder.encodeString(name)
}
/**
* Register plugin inside of this [KSerializer]. Since plugin has been registered, you may use its [name] in any
* serialized [dev.inmo.micro_utils.startup.launcher.Config] to retrieve [plugin] you passed here
*/
fun registerPlugin(name: String, plugin: StartPlugin) {
registeredPlugins[name] = plugin
}
}

View File

@@ -0,0 +1,10 @@
# How to use
In case you have multiplatform project and wish to use startup plugin, this template may help you to create new modules.
1. Copy-paste whole template folder (you may clone this folder to your project and actualize some data to copy your prepared template)
2. Replace `group_name` by your project (or root module) group name
3. Replace `module_name` by the name of your new module name
You may read about the `build.gradle` structure in these templates in project
[KotlinMultiplatformProjectTemplate](https://github.com/InsanusMokrassar/KotlinMultiplatformProjectTemplate).

View File

@@ -0,0 +1,18 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
alias(libs.plugins.compose)
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":${rootProject.name}.module_name.common")
}
}
}
}

View File

@@ -0,0 +1,15 @@
package group_name.module_name.client
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ClientPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
}
}

View File

@@ -0,0 +1,20 @@
package group_name.module_name.client
import group_name.module_name.common.CommonJSPlugin
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ClientJSPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with(CommonJSPlugin) { setupDI(config) }
with(ClientPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonJSPlugin.startPlugin(koin)
ClientPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1,9 @@
package group_name.module_name.client
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
@ExperimentalStdlibApi
@EagerInitialization
@JsExport
@ExperimentalJsExport
private val jsModuleLoader = createStartupPluginAndRegister("template.ClientJSPlugin") { ClientJSPlugin }

View File

@@ -0,0 +1,21 @@
package group_name.module_name.client
import group_name.module_name.common.CommonJVMPlugin
import group_name.module_name.common.CommonJVMPlugin.setupDI
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ClientJVMPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with(CommonJVMPlugin) { setupDI(config) }
with(ClientPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonJVMPlugin.startPlugin(koin)
ClientPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1 @@
<manifest package="group_name.module_name.client"/>

View File

@@ -0,0 +1,21 @@
package group_name.module_name.client
import group_name.module_name.common.CommonAndroidPlugin
import group_name.module_name.common.CommonAndroidPlugin.setupDI
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ClientAndroidPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with(CommonAndroidPlugin) { setupDI(config) }
with(ClientPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonAndroidPlugin.startPlugin(koin)
ClientPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1,7 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@@ -0,0 +1,11 @@
package group_name.module_name.common
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.module.Module
object CommonPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
}
}

View File

@@ -0,0 +1 @@
package group_name.module_name.common

View File

@@ -0,0 +1,17 @@
package group_name.module_name.common
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object CommonJSPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with (CommonPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1,9 @@
package group_name.module_name.common
import dev.inmo.micro_utils.startup.plugin.createStartupPluginAndRegister
@ExperimentalStdlibApi
@EagerInitialization
@JsExport
@ExperimentalJsExport
private val jsModuleLoader = createStartupPluginAndRegister("template.CommonJSPlugin") { CommonJSPlugin }

View File

@@ -0,0 +1,17 @@
package group_name.module_name.common
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object CommonJVMPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with (CommonPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1 @@
<manifest package="group_name.module_name.common"/>

View File

@@ -0,0 +1,17 @@
package group_name.module_name.common
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object CommonAndroidPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with (CommonPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonPlugin.startPlugin(koin)
}
}

View File

@@ -0,0 +1,16 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppJavaProjectPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":${rootProject.name}.module_name.common")
}
}
}
}

View File

@@ -0,0 +1,15 @@
package group_name.module_name.server
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ServerPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
}
}

View File

@@ -0,0 +1,20 @@
package group_name.module_name.server
import group_name.module_name.common.CommonJVMPlugin
import dev.inmo.micro_utils.startup.plugin.StartPlugin
import kotlinx.serialization.json.JsonObject
import org.koin.core.Koin
import org.koin.core.module.Module
object ServerJVMPlugin : StartPlugin {
override fun Module.setupDI(config: JsonObject) {
with(CommonJVMPlugin) { setupDI(config) }
with(ServerPlugin) { setupDI(config) }
}
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
CommonJVMPlugin.startPlugin(koin)
ServerPlugin.startPlugin(koin)
}
}