Compare commits

...

24 Commits

Author SHA1 Message Date
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
19 changed files with 348 additions and 17 deletions

View File

@@ -1,5 +1,25 @@
# Changelog
## 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

@@ -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

@@ -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.2
android_code_version=170

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.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"

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

@@ -3,7 +3,7 @@ package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import kotlinx.serialization.json.JsonObject
import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext
import org.koin.core.context.startKoin
import org.koin.dsl.module
/**
@@ -23,7 +23,7 @@ suspend fun start(rawConfig: JsonObject) {
}
)
logger.i("Modules loaded")
GlobalContext.startKoin(koinApp)
startKoin(koinApp)
logger.i("Koin started")
startPlugin(koinApp.koin)
logger.i("App has been setup")

View File

@@ -1,35 +1,41 @@
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.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)
}
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)
}

View File

@@ -0,0 +1,33 @@
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
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) {
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) {
KSLog.i("Start convert config to JSON")
val json = defaultJson.encodeToJsonElement(Config.serializer(), config).jsonObject
KSLog.i("Config has been read")
start(json)
}
}

View File

@@ -26,7 +26,7 @@ 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}")

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
}
}