Browse Source

Fix build

Him188 5 years ago
parent
commit
9fb167ee60

+ 1 - 19
backend/codegen/build.gradle.kts

@@ -1,5 +1,3 @@
-import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
-
 plugins {
     kotlin("jvm") version "1.4-M2"
     kotlin("plugin.serialization") version "1.4-M2"
@@ -23,21 +21,5 @@ kotlin {
 }
 
 dependencies {
-    api(kotlin("stdlib-jdk8", "1.4-M2"))
-}
-
-val compileKotlin: KotlinCompile by tasks
-compileKotlin.kotlinOptions {
-    jvmTarget = "1.8"
-}
-val compileTestKotlin: KotlinCompile by tasks
-compileTestKotlin.kotlinOptions {
-    jvmTarget = "1.8"
-}
-java {
-    sourceCompatibility = JavaVersion.VERSION_1_8
-    targetCompatibility = JavaVersion.VERSION_1_8
-}
-tasks.withType(JavaCompile::class.java) {
-    options.encoding = "UTF8"
+    api(kotlin("stdlib-jdk8"))
 }

+ 3 - 1
build.gradle.kts

@@ -8,8 +8,8 @@ allprojects {
     group = "net.mamoe"
 
     repositories {
+        mavenLocal()
         maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
-        maven(url = "https://mirrors.huaweicloud.com/repository/maven")
         jcenter()
         mavenCentral()
     }
@@ -18,5 +18,7 @@ allprojects {
 subprojects {
     afterEvaluate {
         apply<MiraiConsoleBuildPlugin>()
+
+        setJavaCompileTarget()
     }
 }

+ 3 - 1
buildSrc/build.gradle.kts

@@ -28,8 +28,10 @@ dependencies {
     api(ktor("client-cio", "1.3.2"))
     api(ktor("client-json", "1.3.2"))
 
-    api(gradleApi())
+    //api(gradleApi())
+    //compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
     compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
+    //runtimeOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
 
     api("com.github.jengelman.gradle.plugins:shadow:6.0.0")
 }

+ 45 - 0
buildSrc/src/main/kotlin/SetCompileTargetPlugin.kt

@@ -0,0 +1,45 @@
+/*
+ * 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
+ */
+
+import org.gradle.api.JavaVersion
+import org.gradle.api.Project
+import org.gradle.api.plugins.JavaPluginExtension
+import org.gradle.api.tasks.compile.JavaCompile
+import java.lang.reflect.Method
+import kotlin.reflect.KClass
+
+
+fun Any.reflectMethod(name: String, vararg params: KClass<out Any>): Pair<Any, Method> {
+    return this to this::class.java.getMethod(name, *params.map { it.java }.toTypedArray())
+}
+
+operator fun Pair<Any, Method>.invoke(vararg args: Any?): Any? {
+    return second.invoke(first, *args)
+}
+
+@Suppress("NOTHING_TO_INLINE") // or error
+fun Project.setJavaCompileTarget() {
+    tasks.filter { it.name in arrayOf("compileKotlin", "compileTestKotlin") }.forEach { task ->
+        task
+            .reflectMethod("getKotlinOptions")()!!
+            .reflectMethod("setJvmTarget", String::class)("1.8")
+    }
+
+
+    kotlin.runCatching { // apply only when java plugin is available
+        (extensions.getByName("java") as JavaPluginExtension).run {
+            sourceCompatibility = JavaVersion.VERSION_1_8
+            targetCompatibility = JavaVersion.VERSION_1_8
+        }
+
+        tasks.withType(JavaCompile::class.java) {
+            options.encoding = "UTF8"
+        }
+    }
+}

+ 2 - 20
frontend/mirai-console-pure/build.gradle.kts

@@ -1,5 +1,3 @@
-import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
-
 plugins {
     kotlin("jvm") version Versions.kotlin
     kotlin("plugin.serialization") version Versions.kotlin
@@ -45,22 +43,6 @@ dependencies {
     testApi(project(":mirai-console"))
 }
 
-version = Versions.Mirai.consolePure
-
-description = "Console Pure CLI frontend for mirai"
+version = Versions.consolePure
 
-val compileKotlin: KotlinCompile by tasks
-compileKotlin.kotlinOptions {
-    jvmTarget = "1.8"
-}
-val compileTestKotlin: KotlinCompile by tasks
-compileTestKotlin.kotlinOptions {
-    jvmTarget = "1.8"
-}
-java {
-    sourceCompatibility = JavaVersion.VERSION_1_8
-    targetCompatibility = JavaVersion.VERSION_1_8
-}
-tasks.withType(JavaCompile::class.java) {
-    options.encoding = "UTF8"
-}
+description = "Console Pure CLI frontend for mirai"

+ 1 - 1
gradle/wrapper/gradle-wrapper.properties

@@ -1,5 +1,5 @@
 #Wed Mar 04 22:27:09 CST 2020
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 zipStorePath=wrapper/dists

+ 24 - 38
settings.gradle.kts

@@ -1,8 +1,8 @@
 pluginManagement {
     repositories {
-        maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
-        maven(url = "https://mirrors.huaweicloud.com/repository/maven")
+        mavenLocal()
         jcenter()
+        maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
         mavenCentral()
     }
 
@@ -11,6 +11,8 @@ pluginManagement {
             val version = requested.version
             when (requested.id.id) {
                 "org.jetbrains.kotlin.jvm" -> useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${version}")
+                "org.jetbrains.kotlin.plugin.serialization" -> useModule("org.jetbrains.kotlin:kotlin-serialization:${version}")
+                "com.jfrog.bintray" -> useModule("com.jfrog.bintray.gradle:gradle-bintray-plugin:$version")
             }
         }
     }
@@ -18,50 +20,34 @@ pluginManagement {
 
 rootProject.name = "mirai-console"
 
-val onlyBackEnd = true
+val disableOldFrontEnds = true
 
-include(":mirai-console")
-project(":mirai-console").projectDir = file("backend/mirai-console")
+fun includeProject(projectPath: String, path: String? = null) {
+    include(projectPath)
+    if (path != null) project(projectPath).projectDir = file(path)
+}
 
-include(":codegen")
-project(":codegen").projectDir = file("backend/codegen")
+includeProject(":mirai-console", "backend/mirai-console")
+includeProject(":codegen", "backend/codegen")
+includeProject(":mirai-console-pure", "frontend/mirai-console-pure")
 
 @Suppress("ConstantConditionIf")
-if (!onlyBackEnd) {
-
-    include(":mirai-console-pure")
-    project(":mirai-console-pure").projectDir = file("frontend/mirai-console-pure")
-
-    include(":mirai-console-terminal")
-    project(":mirai-console-terminal").projectDir = file("frontend/mirai-console-terminal")
+if (!disableOldFrontEnds) {
+    includeProject(":mirai-console-terminal", "frontend/mirai-console-terminal")
 
-    try {
-        val javaVersion = System.getProperty("java.version")
-        var versionPos = javaVersion.indexOf(".")
-        var javaVersionNum = javaVersion.substring(0, 1).toInt()
-
-        if (javaVersion.startsWith("1.")) {
-            javaVersionNum = javaVersion.substring(2, 3).toInt()
-        } else {
-            if (versionPos == -1) versionPos = javaVersion.indexOf("-")
-            if (versionPos == -1) {
-                println("jdk version unknown")
-            } else {
-                javaVersionNum = javaVersion.substring(0, versionPos).toInt()
-            }
-        }
-        if (javaVersionNum >= 9) {
-            include(":mirai-console-graphical")
-            project(":mirai-console-graphical").projectDir = file("frontend/mirai-console-graphical")
-        } else {
-            println("JDK 版本为 $javaVersionNum")
-            println("当前使用的 JDK 版本为 ${System.getProperty("java.version")},  请使用JDK 9以上版本引入模块 `:mirai-console-graphical`\n")
+    val jdkVersion = kotlin.runCatching {
+        System.getProperty("java.version").let { v ->
+            v.toIntOrNull() ?: v.removePrefix("1.").substringBefore("-").toIntOrNull()
         }
+    }.getOrNull() ?: -1
 
-    } catch (ignored: Exception) {
-        println("无法确定 JDK 版本, 将不会引入 `:mirai-console-graphical`")
+    println("JDK version: $jdkVersion")
+
+    if (jdkVersion >= 9) {
+        includeProject(":mirai-console-graphical", "frontend/mirai-console-graphical")
+    } else {
+        println("当前使用的 JDK 版本为 ${System.getProperty("java.version")},  请使用 JDK 9 以上版本引入模块 `:mirai-console-graphical`\n")
     }
 }
 
-
 enableFeaturePreview("GRADLE_METADATA")