mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-17 14:29:24 +00:00
Compare commits
46 Commits
Author | SHA1 | Date | |
---|---|---|---|
3e24cf1768 | |||
fa090bf920 | |||
a83ee86340 | |||
ee56e9543a | |||
96fdff6ffd | |||
58b007cbb3 | |||
4f0c139889 | |||
c584c24fce | |||
85e5cee154 | |||
5af91981f1 | |||
2fe4f08059 | |||
83796f345a | |||
c1e21364a6 | |||
067d9d0d3b | |||
03f527d83e | |||
ced05a4586 | |||
43fe06206a | |||
023657558e | |||
9b0b726c80 | |||
4ee67321c4 | |||
59f1f2e59b | |||
0766d48b7c | |||
e18903b9e9 | |||
d0eecdead2 | |||
cc4a83a033 | |||
1cf911bbde | |||
36d73d5023 | |||
c395242e3e | |||
cd9cd7cc5d | |||
acbb8a0c07 | |||
b9d8528599 | |||
4971326eca | |||
09d1047260 | |||
02dbd493c2 | |||
b17931e7bd | |||
2a4570eafc | |||
c9514d3a6d | |||
072805efc7 | |||
369ff26627 | |||
c5abbbbd2d | |||
d974639f1e | |||
26efde316b | |||
fafe50f80a | |||
41504469db | |||
03b3ddd98b | |||
89d919f2be |
3
.github/workflows/dokka_push.yml
vendored
3
.github/workflows/dokka_push.yml
vendored
@@ -11,9 +11,6 @@ jobs:
|
||||
- uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 11
|
||||
- name: Fix android 32.0.0 dx
|
||||
continue-on-error: true
|
||||
run: cd /usr/local/lib/android/sdk/build-tools/32.0.0/ && mv d8 dx && cd lib && mv d8.jar dx.jar
|
||||
- name: Build
|
||||
run: ./gradlew build && ./gradlew dokkaHtml
|
||||
- name: Publish KDocs
|
||||
|
37
CHANGELOG.md
37
CHANGELOG.md
@@ -1,5 +1,42 @@
|
||||
# Changelog
|
||||
|
||||
## 0.16.3
|
||||
|
||||
* `Coroutines`:
|
||||
* Create `launchInCurrentThread`
|
||||
* `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`:
|
||||
* Inited :)
|
||||
* `Plugin`:
|
||||
* Inited :)
|
||||
* `Launcher`:
|
||||
* Inited :)
|
||||
|
||||
## 0.15.0
|
||||
|
||||
* `Repos`:
|
||||
|
@@ -22,6 +22,7 @@ kotlin {
|
||||
dependencies {
|
||||
api libs.kt.coroutines.android
|
||||
}
|
||||
dependsOn(jvmMain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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)
|
||||
}
|
@@ -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) {
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,7 +17,7 @@ kotlin {
|
||||
// browser()
|
||||
// nodejs()
|
||||
// }
|
||||
android()
|
||||
android {}
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
@@ -30,8 +30,8 @@ kotlin {
|
||||
&& it.hasProperty("kotlin")
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||
// && it.kotlin.sourceSets.any { it.name.contains("jsMain") }
|
||||
// && it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
|
||||
// && it.kotlin.sourceSets.any { it.name.contains("androidMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("androidMain") }
|
||||
) {
|
||||
api it
|
||||
}
|
||||
@@ -62,7 +62,7 @@ kotlin {
|
||||
if (
|
||||
it != project
|
||||
&& it.hasProperty("kotlin")
|
||||
// && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("jvmMain") }
|
||||
) {
|
||||
api it
|
||||
@@ -78,7 +78,7 @@ kotlin {
|
||||
if (
|
||||
it != project
|
||||
&& it.hasProperty("kotlin")
|
||||
// && it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("commonMain") }
|
||||
&& it.kotlin.sourceSets.any { it.name.contains("androidMain") }
|
||||
) {
|
||||
api it
|
||||
@@ -100,7 +100,7 @@ private List<SourceDirectorySet> findSourcesWithName(String... approximateNames)
|
||||
}.collect { it.kotlin }
|
||||
}
|
||||
|
||||
dokkaHtml {
|
||||
tasks.dokkaHtml {
|
||||
dokkaSourceSets {
|
||||
configureEach {
|
||||
skipDeprecated.set(true)
|
||||
|
@@ -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"
|
||||
|
@@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
||||
# Project data
|
||||
|
||||
group=dev.inmo
|
||||
version=0.15.0
|
||||
android_code_version=166
|
||||
version=0.16.3
|
||||
android_code_version=171
|
||||
|
@@ -4,26 +4,28 @@ kt = "1.7.20"
|
||||
kt-serialization = "1.4.1"
|
||||
kt-coroutines = "1.6.4"
|
||||
|
||||
jb-compose = "1.2.1"
|
||||
kslog = "0.5.4"
|
||||
|
||||
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.1"
|
||||
|
||||
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"
|
||||
|
||||
@@ -60,6 +62,7 @@ ktor-server-websockets = { module = "io.ktor:ktor-server-websockets", version.re
|
||||
ktor-server-statusPages = { module = "io.ktor:ktor-server-status-pages", version.ref = "ktor" }
|
||||
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
|
||||
|
||||
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
|
||||
|
||||
klock = { module = "com.soywiz.korlibs.klock:klock", version.ref = "klock" }
|
||||
uuid = { module = "com.benasher44:uuid", version.ref = "uuid" }
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
@@ -28,6 +28,7 @@ import org.w3c.xhr.XMLHttpRequestResponseType
|
||||
* @param data [Map] where keys will be used as names for multipart parts and values as values. If you will pass
|
||||
* [dev.inmo.micro_utils.common.MPPFile] (File from JS or JVM platform). Also you may pass [UniUploadFileInfo] as value
|
||||
* in case you wish to pass other source of multipart binary data than regular file
|
||||
* @suppress
|
||||
*/
|
||||
actual suspend fun <T> HttpClient.uniUpload(
|
||||
url: String,
|
||||
|
@@ -27,6 +27,7 @@ import java.io.File
|
||||
* @param data [Map] where keys will be used as names for multipart parts and values as values. If you will pass
|
||||
* [dev.inmo.micro_utils.common.MPPFile] (File from JS or JVM platform). Also you may pass [UniUploadFileInfo] as value
|
||||
* in case you wish to pass other source of multipart binary data than regular file
|
||||
* @suppress
|
||||
*/
|
||||
actual suspend fun <T> HttpClient.uniUpload(
|
||||
url: String,
|
||||
|
49
mppJsAndJavaProject.gradle
Normal file
49
mppJsAndJavaProject.gradle
Normal 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
|
||||
}
|
@@ -23,7 +23,7 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation libs.kt.serialization
|
||||
api libs.kt.serialization
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
|
@@ -23,7 +23,7 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation libs.kt.serialization
|
||||
api libs.kt.serialization
|
||||
implementation compose.runtime
|
||||
}
|
||||
}
|
||||
|
17
safe_wrapper/build.gradle
Normal file
17
safe_wrapper/build.gradle
Normal 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
safe_wrapper/src/commonMain/kotlin/SafeWrapper.kt
Normal file
17
safe_wrapper/src/commonMain/kotlin/SafeWrapper.kt
Normal 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)
|
||||
}
|
||||
}
|
1
safe_wrapper/src/main/AndroidManifest.xml
Normal file
1
safe_wrapper/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="dev.inmo.micro_utils.safe_wrapper"/>
|
@@ -4,6 +4,7 @@ String[] includes = [
|
||||
":common",
|
||||
":common:compose",
|
||||
":matrix",
|
||||
":safe_wrapper",
|
||||
":crypto",
|
||||
":koin",
|
||||
":selector:common",
|
||||
@@ -32,6 +33,8 @@ String[] includes = [
|
||||
":serialization:base64",
|
||||
":serialization:encapsulator",
|
||||
":serialization:typed_serializer",
|
||||
":startup:plugin",
|
||||
":startup:launcher",
|
||||
|
||||
":fsm:common",
|
||||
":fsm:repos:common",
|
||||
|
92
startup/launcher/README.md
Normal file
92
startup/launcher/README.md
Normal 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"
|
||||
]
|
||||
}
|
||||
```
|
31
startup/launcher/build.gradle
Normal file
31
startup/launcher/build.gradle
Normal file
@@ -0,0 +1,31 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "application"
|
||||
}
|
||||
|
||||
apply from: "$mppJsAndJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api internalProject("micro_utils.startup.plugin")
|
||||
}
|
||||
}
|
||||
commonTest {
|
||||
dependencies {
|
||||
implementation libs.kt.coroutines.test
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
mainClassName = "dev.inmo.micro_utils.startup.launcher.MainKt"
|
||||
}
|
||||
|
||||
java {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
22
startup/launcher/src/commonMain/kotlin/Config.kt
Normal file
22
startup/launcher/src/commonMain/kotlin/Config.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* Contains just [List] of [StartPlugin]s. In json this config should look like:
|
||||
*
|
||||
* ```json
|
||||
* {
|
||||
* "plugins": [
|
||||
* "dev.inmo.micro_utils.startup.launcher.HelloWorldPlugin"
|
||||
* ]
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* In the sample above [HelloWorldPlugin] will be loaded during startup of application
|
||||
*/
|
||||
@Serializable
|
||||
data class Config(
|
||||
val plugins: List<StartPlugin>
|
||||
)
|
7
startup/launcher/src/commonMain/kotlin/DefaultJson.kt
Normal file
7
startup/launcher/src/commonMain/kotlin/DefaultJson.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
val defaultJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
13
startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt
Normal file
13
startup/launcher/src/commonMain/kotlin/HelloWorldPlugin.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.kslog.common.i
|
||||
import dev.inmo.kslog.common.logger
|
||||
import dev.inmo.micro_utils.startup.plugin.StartPlugin
|
||||
import org.koin.core.Koin
|
||||
|
||||
object HelloWorldPlugin : StartPlugin {
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
logger.i("Hello world")
|
||||
}
|
||||
}
|
17
startup/launcher/src/commonMain/kotlin/Start.kt
Normal file
17
startup/launcher/src/commonMain/kotlin/Start.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.KoinApplication
|
||||
|
||||
/**
|
||||
* 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]
|
||||
*/
|
||||
@Deprecated("Fully replaced with StartLauncherPlugin#start", ReplaceWith("StartLauncherPlugin.start(rawConfig)", "dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin"))
|
||||
suspend fun start(rawConfig: JsonObject) {
|
||||
StartLauncherPlugin.start(rawConfig)
|
||||
}
|
141
startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt
Normal file
141
startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt
Normal file
@@ -0,0 +1,141 @@
|
||||
package dev.inmo.micro_utils.startup.launcher
|
||||
|
||||
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
|
||||
import kotlinx.coroutines.joinAll
|
||||
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
|
||||
|
||||
/**
|
||||
* Default startup plugin. See [setupDI] and [startPlugin] for more info
|
||||
*/
|
||||
object StartLauncherPlugin : StartPlugin {
|
||||
internal val logger = taggedLogger(this)
|
||||
|
||||
fun Module.setupDI(config: Config, rawJsonObject: JsonObject? = null) {
|
||||
val rawJsonObject = rawJsonObject ?: defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
|
||||
|
||||
single { rawJsonObject }
|
||||
single { config }
|
||||
single { CoroutineScope(Dispatchers.Default) }
|
||||
single { defaultJson } binds arrayOf(StringFormat::class, SerialFormat::class)
|
||||
|
||||
includes(
|
||||
config.plugins.mapNotNull {
|
||||
runCatching {
|
||||
module {
|
||||
with(it) {
|
||||
setupDI(rawJsonObject)
|
||||
}
|
||||
}
|
||||
}.onFailure { e ->
|
||||
logger.w("Unable to load DI part of $it", e)
|
||||
}.getOrNull()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
runCatchingSafely {
|
||||
logger.i("Start loading of $plugin")
|
||||
with(plugin) {
|
||||
startPlugin(koin)
|
||||
}
|
||||
}.onFailure { 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)
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
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 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 {
|
||||
@BeforeTest
|
||||
fun resetGlobalKoinContext() {
|
||||
runCatching { stopKoin() }
|
||||
}
|
||||
@Test
|
||||
fun CheckThatEmptyPluginsListLeadsToEndOfMain() {
|
||||
val emptyJson = defaultJson.encodeToJsonElement(
|
||||
Config.serializer(),
|
||||
Config(emptyList())
|
||||
).jsonObject
|
||||
|
||||
runTest {
|
||||
val job = launch {
|
||||
StartLauncherPlugin.start(emptyJson)
|
||||
}
|
||||
job.join()
|
||||
}
|
||||
}
|
||||
@Test
|
||||
fun CheckThatHelloWorldPluginsListLeadsToEndOfMain() {
|
||||
val emptyJson = defaultJson.encodeToJsonElement(
|
||||
Config.serializer(),
|
||||
Config(listOf(HelloWorldPlugin))
|
||||
).jsonObject
|
||||
|
||||
runTest {
|
||||
val job = launch {
|
||||
StartLauncherPlugin.start(emptyJson)
|
||||
}
|
||||
job.join()
|
||||
}
|
||||
}
|
||||
}
|
25
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal file
25
startup/launcher/src/jsMain/kotlin/PluginsStarter.kt
Normal 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)
|
||||
}
|
37
startup/launcher/src/jvmMain/kotlin/Main.kt
Normal file
37
startup/launcher/src/jvmMain/kotlin/Main.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
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 java.io.File
|
||||
|
||||
/**
|
||||
* It is expected, that [args] will contain 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] KDocs.
|
||||
*
|
||||
* 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`
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
|
||||
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")
|
||||
|
||||
StartLauncherPlugin.start(json)
|
||||
}
|
25
startup/plugin/build.gradle
Normal file
25
startup/plugin/build.gradle
Normal file
@@ -0,0 +1,25 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
}
|
||||
|
||||
apply from: "$mppJsAndJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api libs.koin
|
||||
api libs.kt.serialization
|
||||
api libs.kslog
|
||||
api libs.kt.reflect
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
jsMain {
|
||||
dependencies {
|
||||
api libs.uuid
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
31
startup/plugin/src/commonMain/kotlin/StartPlugin.kt
Normal file
31
startup/plugin/src/commonMain/kotlin/StartPlugin.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
/**
|
||||
* Default plugin for start of your app
|
||||
*/
|
||||
@Serializable(StartPluginSerializer::class)
|
||||
interface StartPlugin {
|
||||
/**
|
||||
* This method will be called first to configure [Koin] [Module] related to this plugin. You may use
|
||||
* [org.koin.core.scope.Scope.get] in your koin definitions like [Module.single] to retrieve
|
||||
* [kotlinx.coroutines.CoroutineScope], [kotlinx.serialization.json.Json] or [dev.inmo.micro_utils.startup.launcher.Config]
|
||||
*/
|
||||
fun Module.setupDI(config: JsonObject) {}
|
||||
|
||||
/**
|
||||
* This method will be called after all other [StartPlugin] will [setupDI]
|
||||
*
|
||||
* It is allowed to lock end of this method in case you require to prevent application to end its run (for example,
|
||||
* you are starting some web server)
|
||||
*
|
||||
* @param koin Will contains everything you will register in [setupDI] (as well as other [StartPlugin]s) and
|
||||
* [kotlinx.coroutines.CoroutineScope], [kotlinx.serialization.json.Json] and [dev.inmo.micro_utils.startup.launcher.Config]
|
||||
* by their types
|
||||
*/
|
||||
suspend fun startPlugin(koin: Koin) {}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
|
||||
expect object StartPluginSerializer : KSerializer<StartPlugin>
|
@@ -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)
|
35
startup/plugin/src/jsMain/kotlin/StartPluginSerializer.kt
Normal file
35
startup/plugin/src/jsMain/kotlin/StartPluginSerializer.kt
Normal 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
|
||||
}
|
||||
}
|
23
startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt
Normal file
23
startup/plugin/src/jvmMain/kotlin/StartPluginSerializer.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package dev.inmo.micro_utils.startup.plugin
|
||||
|
||||
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> {
|
||||
override val descriptor: SerialDescriptor
|
||||
get() = String.serializer().descriptor
|
||||
|
||||
override fun deserialize(decoder: Decoder): StartPlugin {
|
||||
val kclass = Class.forName(decoder.decodeString()).kotlin
|
||||
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as StartPlugin
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: StartPlugin) {
|
||||
encoder.encodeString(
|
||||
value::class.java.canonicalName
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user