From 2baed760f7ec41557d92c96c2316b783f9a771b0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 17 Feb 2021 01:21:41 +0600 Subject: [PATCH] inluding sdi --- CHANGELOG.md | 6 +++++- bot/build.gradle | 2 ++ bot/src/main/kotlin/dev/inmo/plagubot/App.kt | 6 ++++-- .../main/kotlin/dev/inmo/plagubot/config/Config.kt | 6 ++++-- .../dev/inmo/plagubot/config/DatabaseConfig.kt | 6 ++++++ build.gradle | 2 +- gradle.properties | 3 +++ plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt | 12 +++++++++--- 8 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c049a90..70423ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,12 @@ * `exposed`: `0.28.1` -> `0.29.1` * `tgbotapi`: `0.30.10` -> `0.32.5` * `microutils`: `0.4.11` -> `0.4.25` +* `Bot` + * New dependency `sdi` + * Now it is possible to pass `Module` to configuration for providing a global plugins parameters like different + common database or tools * `Plugin` - * New method `BehaviourContext#invoke` + * Two new methods `BehaviourContext#invoke` * Old method `invoke` has been deprecated ## 0.0.5 diff --git a/bot/build.gradle b/bot/build.gradle index e174da2..88536b1 100644 --- a/bot/build.gradle +++ b/bot/build.gradle @@ -18,6 +18,7 @@ dependencies { api "org.jetbrains.exposed:exposed-jdbc:$kotlin_exposed_version" api "dev.inmo:tgbotapi:$tgbotapi_version" + api "dev.inmo:sdi:$sdi_version" api "dev.inmo:micro_utils.repos.exposed:$microutils_version" api "com.github.matfax.klassindex:library:$klassindex_version" @@ -34,6 +35,7 @@ application { kapt { arguments { arg("com.github.matfax.klassindex.IndexSubclasses", "dev.inmo.plagubot.Plugin") + arg("com.github.matfax.klassindex.IndexAnnotated", "dev.inmo.sdi.SDIIncluded") } } diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt index de2672b..5d0d004 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/App.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/App.kt @@ -1,7 +1,7 @@ package dev.inmo.plagubot import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions -import dev.inmo.plagubot.config.Config +import dev.inmo.plagubot.config.* import dev.inmo.plagubot.config.configSerialFormat import dev.inmo.tgbotapi.bot.Ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands @@ -18,9 +18,11 @@ suspend inline fun initPlaguBot( ) { val bot = telegramBot(config.botToken) + val paramsMap = config.params ?.toMap() ?: emptyMap() + val database = config.params ?.database ?: config.database.database bot.buildBehaviour(scope) { val commands = config.plugins.flatMap { - it.apply { invoke(config.database.database) } + it.apply { invoke(database, paramsMap) } it.getCommands() }.let { val futureUnavailable = it.drop(botCommandsLimit.last) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt index bef1034..c3f8fd5 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt @@ -2,6 +2,7 @@ package dev.inmo.plagubot.config import com.github.matfax.klassindex.KlassIndex import dev.inmo.plagubot.Plugin +import dev.inmo.sdi.Module import kotlinx.serialization.* import kotlinx.serialization.json.Json import kotlinx.serialization.modules.* @@ -37,6 +38,7 @@ internal val configSerialFormat: StringFormat @Serializable data class Config( val plugins: List, - val database: DatabaseConfig, - val botToken: String + val database: DatabaseConfig = DatabaseConfig(), + val botToken: String, + val params: Module? = null ) diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt index 835a548..86750c1 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/DatabaseConfig.kt @@ -1,5 +1,6 @@ package dev.inmo.plagubot.config +import dev.inmo.sdi.SDIIncluded import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import org.jetbrains.exposed.sql.Database @@ -7,7 +8,12 @@ import org.jetbrains.exposed.sql.transactions.transactionManager import org.sqlite.JDBC import java.sql.Connection +const val defaultDatabaseParamsName = "defaultDatabase" +inline val Map.database: Database? + get() = (get(defaultDatabaseParamsName) as? DatabaseConfig) ?.database + @Serializable +@SDIIncluded data class DatabaseConfig( val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared", val driver: String = JDBC::class.qualifiedName!!, diff --git a/build.gradle b/build.gradle index e5c7d9c..4d09d52 100644 --- a/build.gradle +++ b/build.gradle @@ -13,8 +13,8 @@ buildscript { allprojects { repositories { - jcenter() mavenCentral() + jcenter() mavenLocal() maven { url 'https://jitpack.io' } } diff --git a/gradle.properties b/gradle.properties index bde1212..050610b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,9 +8,12 @@ kotlin_version=1.4.30 kotlin_coroutines_version=1.4.2 kotlin_serialisation_runtime_version=1.1.0-RC kotlin_exposed_version=0.29.1 + +sdi_version=0.4.0-rc2 tgbotapi_version=0.32.5 microutils_version=0.4.25 klassindex_version=4.1.0-rc.1 + sqlite_version=3.30.1 github_release_plugin_version=2.2.12 diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt index dbd17b1..74141f2 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt @@ -33,7 +33,13 @@ interface Plugin { */ suspend operator fun BehaviourContext.invoke( database: Database - ) { - invoke(bot, database, flowsUpdatesFilter, scope) - } + ) = invoke(bot, database, flowsUpdatesFilter, scope) + + /** + * This method (usually) will be invoked just one time in the whole application. + */ + suspend operator fun BehaviourContext.invoke( + database: Database, + params: Map + ) = invoke(database) }