From 8fdfe830fed04b35ceae831e78e95ca235a2312b Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 2 Dec 2020 09:25:25 +0800 Subject: [PATCH] Introduce new module mirai-core-utils --- mirai-core-api/build.gradle.kts | 1 + .../commonMain/kotlin/utils/StandardUtils.kt | 21 --- mirai-core-utils/build.gradle.kts | 160 ++++++++++++++++++ .../src/commonMain/kotlin/Arrays.kt | 13 ++ .../src/commonMain/kotlin/StandardUtils.kt | 18 ++ .../src/commonMain/kotlin/package.kt | 10 ++ mirai-core/build.gradle.kts | 1 + settings.gradle.kts | 1 + 8 files changed, 204 insertions(+), 21 deletions(-) delete mode 100644 mirai-core-api/src/commonMain/kotlin/utils/StandardUtils.kt create mode 100644 mirai-core-utils/build.gradle.kts create mode 100644 mirai-core-utils/src/commonMain/kotlin/Arrays.kt create mode 100644 mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt create mode 100644 mirai-core-utils/src/commonMain/kotlin/package.kt diff --git a/mirai-core-api/build.gradle.kts b/mirai-core-api/build.gradle.kts index a8204f408..07598321d 100644 --- a/mirai-core-api/build.gradle.kts +++ b/mirai-core-api/build.gradle.kts @@ -69,6 +69,7 @@ kotlin { sourceSets { val commonMain by getting { dependencies { + implementation(project(":mirai-core-utils")) api(kotlin("serialization")) api(kotlin("reflect")) diff --git a/mirai-core-api/src/commonMain/kotlin/utils/StandardUtils.kt b/mirai-core-api/src/commonMain/kotlin/utils/StandardUtils.kt deleted file mode 100644 index ed730d39f..000000000 --- a/mirai-core-api/src/commonMain/kotlin/utils/StandardUtils.kt +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:JvmName("MiraiUtils") - -package net.mamoe.mirai.utils - -@PublishedApi -internal inline fun Any?.cast(): T = this as T - -@PublishedApi -internal inline fun Any?.safeCast(): T? = this as? T - -@PublishedApi -internal inline fun Any?.castOrNull(): T? = this as? T diff --git a/mirai-core-utils/build.gradle.kts b/mirai-core-utils/build.gradle.kts new file mode 100644 index 000000000..96dc63f26 --- /dev/null +++ b/mirai-core-utils/build.gradle.kts @@ -0,0 +1,160 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:Suppress("UNUSED_VARIABLE") + +plugins { + kotlin("multiplatform") + id("kotlinx-atomicfu") + kotlin("plugin.serialization") + id("net.mamoe.kotlin-jvm-blocking-bridge") + `maven-publish` + id("com.jfrog.bintray") +} + +description = "mirai-core ultilities" + +val isAndroidSDKAvailable: Boolean by project + +afterEvaluate { + tasks.getByName("compileKotlinCommon").enabled = false + tasks.getByName("compileTestKotlinCommon").enabled = false + + tasks.getByName("compileCommonMainKotlinMetadata").enabled = false + tasks.getByName("compileKotlinMetadata").enabled = false +} + +kotlin { + explicitApi() + + if (isAndroidSDKAvailable) { + apply(from = rootProject.file("gradle/android.gradle")) + android("android") { + publishAllLibraryVariants() + } + } else { + println( + """Android SDK 可能未安装. + $name 的 Android 目标编译将不会进行. + 这不会影响 Android 以外的平台的编译. + """.trimIndent() + ) + println( + """Android SDK might not be installed. + Android target of $name will not be compiled. + It does no influence on the compilation of other platforms. + """.trimIndent() + ) + } + + jvm("common") { + attributes.attribute(Attribute.of("mirai.target.platform", String::class.java), "common") + } + + jvm("jvm") { + attributes.attribute(Attribute.of("mirai.target.platform", String::class.java), "jvm") + } + +// jvm("android") { +// attributes.attribute(Attribute.of("mirai.target.platform", String::class.java), "android") +// } + + sourceSets { + val commonMain by getting { + dependencies { + api(kotlin("serialization")) + api(kotlin("reflect")) + + api1(`kotlinx-serialization-core`) + api1(`kotlinx-serialization-json`) + implementation1(`kotlinx-serialization-protobuf`) + api1(`kotlinx-io-jvm`) + api1(`kotlinx-coroutines-io-jvm`) + api(`kotlinx-coroutines-core`) + + implementation1(`kotlinx-atomicfu`) + + api1(`ktor-client-cio`) + api1(`ktor-client-core`) + api1(`ktor-network`) + + compileOnly(`log4j-api`) + compileOnly(slf4j) + } + } + + if (isAndroidSDKAvailable) { + androidMain { + dependencies { + api1(`ktor-client-android`) + } + } + } + + val jvmMain by getting + + val jvmTest by getting { + dependencies { + api("org.pcap4j:pcap4j-distribution:1.8.2") + + runtimeOnly(files("build/classes/kotlin/jvm/test")) // classpath is not properly set by IDE + } + } + } +} + +val NamedDomainObjectContainer.androidMain: NamedDomainObjectProvider + get() = named("androidMain") + +val NamedDomainObjectContainer.androidTest: NamedDomainObjectProvider + get() = named("androidTest") + + +val NamedDomainObjectContainer.jvmMain: NamedDomainObjectProvider + get() = named("jvmMain") + +val NamedDomainObjectContainer.jvmTest: NamedDomainObjectProvider + get() = named("jvmTest") + + +fun org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler.implementation1(dependencyNotation: String) = + implementation(dependencyNotation) { + exclude("org.jetbrains.kotlin", "kotlin-stdlib") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-common") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-jvm") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-metadata") + } + +fun org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler.api1(dependencyNotation: String) = + api(dependencyNotation) { + exclude("org.jetbrains.kotlin", "kotlin-stdlib") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-common") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-jvm") + exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-metadata") + } + +apply(from = rootProject.file("gradle/publish.gradle")) + +tasks.withType { + doFirst { + publishing.publications + .filterIsInstance() + .forEach { publication -> + val moduleFile = buildDir.resolve("publications/${publication.name}/module.json") + if (moduleFile.exists()) { + publication.artifact(object : + org.gradle.api.publish.maven.internal.artifact.FileBasedMavenArtifact(moduleFile) { + override fun getDefaultExtension() = "module" + }) + } + } + } +} \ No newline at end of file diff --git a/mirai-core-utils/src/commonMain/kotlin/Arrays.kt b/mirai-core-utils/src/commonMain/kotlin/Arrays.kt new file mode 100644 index 000000000..e2ef8bc8c --- /dev/null +++ b/mirai-core-utils/src/commonMain/kotlin/Arrays.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:JvmName("MiraiUtils") + +package net.mamoe.mirai.utils + diff --git a/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt new file mode 100644 index 000000000..ef821d254 --- /dev/null +++ b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:JvmName("MiraiUtils") + +package net.mamoe.mirai.utils + +inline fun Any?.cast(): T = this as T + +inline fun Any?.safeCast(): T? = this as? T + +inline fun Any?.castOrNull(): T? = this as? T diff --git a/mirai-core-utils/src/commonMain/kotlin/package.kt b/mirai-core-utils/src/commonMain/kotlin/package.kt new file mode 100644 index 000000000..002185ab8 --- /dev/null +++ b/mirai-core-utils/src/commonMain/kotlin/package.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.utils \ No newline at end of file diff --git a/mirai-core/build.gradle.kts b/mirai-core/build.gradle.kts index 6516821a3..b6b21d5a7 100644 --- a/mirai-core/build.gradle.kts +++ b/mirai-core/build.gradle.kts @@ -76,6 +76,7 @@ kotlin { commonMain { dependencies { + implementation(project(":mirai-core-utils")) api1(`kotlinx-serialization-core`) api1(`kotlinx-serialization-json`) implementation1(`kotlinx-serialization-protobuf`) diff --git a/settings.gradle.kts b/settings.gradle.kts index 04f6a9240..5e4ec6d80 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,6 +21,7 @@ pluginManagement { rootProject.name = "mirai" +include(":mirai-core-utils") include(":mirai-core-api") include(":mirai-core") // include(":mirai-core-all") \ No newline at end of file