From e0476d7e1486cfc6681d4bed65c9cdd9fd4632ce Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Thu, 28 Jan 2021 09:32:23 +0800
Subject: [PATCH] Include mirai-console in mirai project build

---
 .gitmodules                                   |  3 +
 CONTRIBUTING.md                               |  8 ++-
 build.gradle.kts                              | 23 +++++---
 buildSrc/src/main/kotlin/JvmDependencies.kt   | 58 +++++++++++++++++++
 .../src/main/kotlin/MiraiCoreDependency.kt    | 23 ++++++++
 buildSrc/src/main/kotlin/Versions.kt          | 32 +++++++++-
 mirai-console                                 |  1 +
 settings.gradle.kts                           | 40 ++++++++++++-
 8 files changed, 177 insertions(+), 11 deletions(-)
 create mode 100644 .gitmodules
 create mode 100644 buildSrc/src/main/kotlin/JvmDependencies.kt
 create mode 100644 buildSrc/src/main/kotlin/MiraiCoreDependency.kt
 create mode 160000 mirai-console

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 000000000..6242fb9ef
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "mirai-console"]
+	path = mirai-console
+	url = git@github.com:mamoe/mirai-console.git
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6e7472448..b6e899766 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -7,6 +7,13 @@ mirai 欢迎一切形式的代码贡献。你可以通过以下几种途径向 m
 ## 主仓库 `mirai-core`
 
 ### 构建
+
+mirai git 仓库含有 submodule, 请在 clone 时使用 `--recursive` 参数, 或在 clone 后使用如下命令更新 submodule:
+```shell script
+git submodule init
+git submodule update
+```
+
 - 要构建项目, 请运行 `gradlew assemble`
 - 要运行测试, 请运行 `gradlew test`
 - 要构建项目并运行测试, 请运行 `gradlew build`
@@ -16,7 +23,6 @@ mirai 欢迎一切形式的代码贡献。你可以通过以下几种途径向 m
 
 - `1.x`: 1.x 版本的开发 (已停止)
 - `dev`: 2.0 版本的开发
-- `master`: 最新稳定版
 - `-release` 后缀: 基于[版本规范](docs/Evolution.md#版本规范), 用于从 `dev` 中筛选 bugfix 并发布一个版本的 patch 的版本. 如 `2.0-release` 会包含 `2.0.x` 版本的更新.
 
 **请基于 `dev` 分支进行修改**
diff --git a/build.gradle.kts b/build.gradle.kts
index e7419a512..98e49d091 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -41,17 +41,19 @@ plugins {
     id("org.jetbrains.dokka") version Versions.dokka
     id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
     id("com.jfrog.bintray") // version Versions.bintray
+    id("com.gradle.plugin-publish") version "0.12.0" apply false
 }
 
 // https://github.com/kotlin/binary-compatibility-validator
 apply(plugin = "binary-compatibility-validator")
 
 configure<kotlinx.validation.ApiValidationExtension> {
-    ignoredProjects.add("mirai-core")
-    ignoredProjects.add("mirai-core-api")
-    ignoredProjects.add("mirai-core-utils")
-    ignoredProjects.add("mirai-core-all")
-    ignoredProjects.add("mirai")
+    allprojects.forEach { subproject ->
+        ignoredProjects.add(subproject.name)
+    }
+    ignoredProjects.remove("binary-compatibility-validator")
+    // Enable validator for module `binary-compatibility-validator` only.
+
 
     ignoredPackages.add("net.mamoe.mirai.internal")
     nonPublicMarkers.add("net.mamoe.mirai.MiraiInternalApi")
@@ -101,8 +103,10 @@ allprojects {
         configureKotlinCompilerSettings()
         configureKotlinExperimentalUsages()
 
-        blockingBridge {
-            unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
+        runCatching {
+            blockingBridge {
+                unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
+            }
         }
 
         //  useIr()
@@ -278,7 +282,10 @@ val experimentalAnnotations = arrayOf(
     "net.mamoe.mirai.LowLevelApi",
     "net.mamoe.mirai.utils.UnstableExternalImage",
 
-    "net.mamoe.mirai.message.data.ExperimentalMessageKey"
+    "net.mamoe.mirai.message.data.ExperimentalMessageKey",
+    "net.mamoe.mirai.console.ConsoleFrontEndImplementation",
+    "net.mamoe.mirai.console.util.ConsoleInternalApi",
+    "net.mamoe.mirai.console.util.ConsoleExperimentalApi"
 )
 
 fun Project.configureKotlinExperimentalUsages() {
diff --git a/buildSrc/src/main/kotlin/JvmDependencies.kt b/buildSrc/src/main/kotlin/JvmDependencies.kt
new file mode 100644
index 000000000..e8ea3b6c4
--- /dev/null
+++ b/buildSrc/src/main/kotlin/JvmDependencies.kt
@@ -0,0 +1,58 @@
+/*
+ * 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:Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
+
+import org.gradle.api.artifacts.ExternalModuleDependency
+import org.gradle.api.artifacts.dsl.DependencyHandler
+import org.gradle.kotlin.dsl.accessors.runtime.addDependencyTo
+
+@Suppress("unused")
+fun DependencyHandler.compileAndTestRuntime(any: Any) {
+    add("compileOnly", any)
+    add("testImplementation", any)
+}
+
+fun DependencyHandler.smartApi(
+    dependencyNotation: String
+): ExternalModuleDependency {
+    return smart("api", dependencyNotation)
+}
+
+fun DependencyHandler.smartImplementation(
+    dependencyNotation: String
+): ExternalModuleDependency {
+    return smart("implementation", dependencyNotation)
+}
+
+private fun DependencyHandler.smart(
+    configuration: String,
+    dependencyNotation: String
+): ExternalModuleDependency {
+    return addDependencyTo(
+        this, configuration, dependencyNotation
+    ) {
+        fun exclude(group: String, module: String) {
+            exclude(
+                mapOf(
+                    "group" to group,
+                    "module" to module
+                )
+            )
+        }
+        exclude("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
+        exclude("org.jetbrains.kotlin", "kotlin-stdlib")
+        exclude("org.jetbrains.kotlin", "kotlin-stdlib-common")
+        exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core-common")
+        exclude("org.jetbrains.kotlinx", "kotlinx-coroutines-core")
+        exclude("org.jetbrains.kotlinx", "kotlinx-serialization-common")
+        exclude("org.jetbrains.kotlinx", "kotlinx-serialization-core")
+    }
+}
diff --git a/buildSrc/src/main/kotlin/MiraiCoreDependency.kt b/buildSrc/src/main/kotlin/MiraiCoreDependency.kt
new file mode 100644
index 000000000..75ec2d4fd
--- /dev/null
+++ b/buildSrc/src/main/kotlin/MiraiCoreDependency.kt
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2019-2021 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("ObjectPropertyName", "HasPlatformType")
+
+import org.gradle.api.Project
+
+/*
+ * For compatibility with composite mirai-core and mirai-console builds and dedicated mirai-console builds.
+ *
+ * If you're in mirai project, see also root/buildSrc/MiraiCoreDependency.kt (likely path)
+ */
+
+
+val Project.`mirai-core-api` get() = rootProject.project(":mirai-core-api")
+val Project.`mirai-core` get() = rootProject.project(":mirai-core")
+val Project.`mirai-core-utils` get() = rootProject.project(":mirai-core-utils")
diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt
index 16fba8ddd..3abb82b4c 100644
--- a/buildSrc/src/main/kotlin/Versions.kt
+++ b/buildSrc/src/main/kotlin/Versions.kt
@@ -13,6 +13,9 @@ import org.gradle.api.attributes.Attribute
 
 object Versions {
     const val project = "2.2.0-dev-4"
+    const val core = project
+    const val console = project
+    const val consoleTerminal = project
 
     const val kotlinCompiler = "1.4.21"
     const val kotlinStdlib = "1.4.21"
@@ -37,6 +40,12 @@ object Versions {
 
     const val slf4j = "1.7.30"
     const val log4j = "2.13.3"
+
+    const val yamlkt = "0.7.5"
+    const val intellijGradlePlugin = "0.4.16"
+    const val kotlinIntellijPlugin = "203-1.4.21-release-IJ5981.133" // keep to newest as kotlinCompiler
+    const val intellij = "2020.3" // don't update easily unless you want your disk space -= 500MB
+
 }
 
 @Suppress("unused")
@@ -69,4 +78,25 @@ val `ktor-client-serialization-jvm` = ktor("client-serialization-jvm", Versions.
 const val slf4j = "org.slf4j:slf4j-api:" + Versions.slf4j
 const val `log4j-api` = "org.apache.logging.log4j:log4j-api:" + Versions.log4j
 
-val ATTRIBUTE_MIRAI_TARGET_PLATFORM: Attribute<String> = Attribute.of("mirai.target.platform", String::class.java)
\ No newline at end of file
+val ATTRIBUTE_MIRAI_TARGET_PLATFORM: Attribute<String> = Attribute.of("mirai.target.platform", String::class.java)
+
+
+const val `kotlin-compiler` = "org.jetbrains.kotlin:kotlin-compiler:${Versions.kotlinCompiler}"
+
+const val `kotlin-stdlib` = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlinStdlib}"
+const val `kotlin-stdlib-jdk8` = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlinStdlib}"
+const val `kotlin-reflect` = "org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinStdlib}"
+const val `kotlin-test` = "org.jetbrains.kotlin:kotlin-test:${Versions.kotlinStdlib}"
+const val `kotlin-test-junit5` = "org.jetbrains.kotlin:kotlin-test-junit5:${Versions.kotlinStdlib}"
+
+
+const val `mirai-core-api` = "net.mamoe:mirai-core-api:${Versions.core}"
+const val `mirai-core` = "net.mamoe:mirai-core:${Versions.core}"
+const val `mirai-core-utils` = "net.mamoe:mirai-core-utils:${Versions.core}"
+
+const val yamlkt = "net.mamoe.yamlkt:yamlkt:${Versions.yamlkt}"
+
+const val `jetbrains-annotations` = "org.jetbrains:annotations:19.0.0"
+
+
+const val `caller-finder` = "io.github.karlatemp:caller:1.0.1"
diff --git a/mirai-console b/mirai-console
new file mode 160000
index 000000000..4432ae30d
--- /dev/null
+++ b/mirai-console
@@ -0,0 +1 @@
+Subproject commit 4432ae30df28b8d1cb66c8a5c0bd3b8ce23aa388
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 3cd60de3e..de3d2d8eb 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -28,4 +28,42 @@ include(":mirai-core-api")
 include(":mirai-core")
 include(":mirai-core-all")
 
-include(":binary-compatibility-validator")
\ No newline at end of file
+//include(":mirai-console")
+
+include(":binary-compatibility-validator")
+
+
+val disableOldFrontEnds = true
+
+fun includeConsoleProject(projectPath: String, path: String? = null) {
+    include(projectPath)
+    if (path != null) project(projectPath).projectDir = file("mirai-console/$path")
+}
+
+includeConsoleProject(":mirai-console", "backend/mirai-console")
+includeConsoleProject(":mirai-console.codegen", "backend/codegen")
+includeConsoleProject(":mirai-console-terminal", "frontend/mirai-console-terminal")
+includeConsoleProject(":mirai-console-compiler-common", "tools/compiler-common")
+includeConsoleProject(":mirai-console-intellij", "tools/intellij-plugin")
+includeConsoleProject(":mirai-console-gradle", "tools/gradle-plugin")
+
+@Suppress("ConstantConditionIf")
+if (!disableOldFrontEnds) {
+    includeConsoleProject(":mirai-console-terminal", "frontend/mirai-console-terminal")
+
+    val jdkVersion = kotlin.runCatching {
+        System.getProperty("java.version").let { v ->
+            v.toIntOrNull() ?: v.removePrefix("1.").substringBefore("-").toIntOrNull()
+        }
+    }.getOrNull() ?: -1
+
+    println("JDK version: $jdkVersion")
+
+    if (jdkVersion >= 9) {
+        includeConsoleProject(":mirai-console-graphical", "frontend/mirai-console-graphical")
+    } else {
+        println("当前使用的 JDK 版本为 ${System.getProperty("java.version")},  请使用 JDK 9 以上版本引入模块 `:mirai-console-graphical`\n")
+    }
+}
+
+enableFeaturePreview("GRADLE_METADATA")
\ No newline at end of file