From 988794343baa7d4bbe2b25c6a2eb3d008acf0a57 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 21 Jan 2021 10:08:21 +0800 Subject: [PATCH] Improve buildscript, fix publication --- build.gradle.kts | 4 +- buildSrc/build.gradle.kts | 30 ++-- buildSrc/src/main/kotlin/JvmPublishing.kt | 119 ++++++++++++++ buildSrc/src/main/kotlin/Mpp.kt | 58 +++++++ buildSrc/src/main/kotlin/MppPublishing.kt | 128 +++++++++++++++ buildSrc/src/main/kotlin/PublishingHelpers.kt | 149 +++--------------- buildSrc/src/main/kotlin/Versions.kt | 1 + buildSrc/src/main/kotlin/upload/Bintray.kt | 7 +- gradle/bintray.gradle | 21 --- gradle/mpp.gradle.kts | 8 + gradle/publish.gradle | 133 ++++++---------- gradle/publishOld.gradle | 138 ---------------- mirai-core-api/build.gradle.kts | 88 +++-------- mirai-core-utils/build.gradle.kts | 39 +---- mirai-core/build.gradle.kts | 47 +----- 15 files changed, 444 insertions(+), 526 deletions(-) create mode 100644 buildSrc/src/main/kotlin/JvmPublishing.kt create mode 100644 buildSrc/src/main/kotlin/Mpp.kt create mode 100644 buildSrc/src/main/kotlin/MppPublishing.kt delete mode 100644 gradle/bintray.gradle create mode 100644 gradle/mpp.gradle.kts delete mode 100644 gradle/publishOld.gradle diff --git a/build.gradle.kts b/build.gradle.kts index 7c73a1255..a74101bfd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -@file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE") +@file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY @@ -41,7 +41,7 @@ plugins { kotlin("plugin.serialization") version Versions.kotlinCompiler 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.jfrog.bintray") // version Versions.bintray } // https://github.com/kotlin/binary-compatibility-validator diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1002f44e3..7591340b9 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * 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. @@ -23,17 +23,29 @@ kotlin { } } + +fun version(name: String): String { + val versions = project.projectDir.resolve("src/main/kotlin/Versions.kt").readText() + + return versions.lineSequence() + .map { it.trim() } + .single { it.startsWith("const val $name") } + .substringAfter('"', "") + .substringBefore('"', "") + .also { + check(it.isNotBlank()) + logger.debug("$name=$it") + } +} + dependencies { fun kotlinx(id: String, version: String) = "org.jetbrains.kotlinx:kotlinx-$id:$version" fun ktor(id: String, version: String) = "io.ktor:ktor-$id:$version" - api("org.jsoup:jsoup:1.12.1") + // compileOnly(kotlin("gradle-plugin-api", "1.3.72")) // Gradle's Kotlin is 1.3.72 - api("com.google.code.gson:gson:2.8.6") - api(kotlinx("coroutines-core", "1.3.3")) - api(ktor("client-core", "1.3.2")) - api(ktor("client-cio", "1.3.2")) - api(ktor("client-json", "1.3.2")) - compileOnly("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5") - api("com.github.jengelman.gradle.plugins:shadow:6.0.0") + api("com.jfrog.bintray.gradle", "gradle-bintray-plugin", version("bintray")) + api("com.github.jengelman.gradle.plugins", "shadow", version("shadow")) + + api(gradleApi()) } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/JvmPublishing.kt b/buildSrc/src/main/kotlin/JvmPublishing.kt new file mode 100644 index 000000000..52f42e365 --- /dev/null +++ b/buildSrc/src/main/kotlin/JvmPublishing.kt @@ -0,0 +1,119 @@ +/* + * 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( + "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS", + "RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS" +) + +import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import org.gradle.api.Project +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.api.tasks.bundling.Jar +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.registering + +/* + * 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 + */ + + +fun Project.configureBintray() { + tasks.register("ensureBintrayAvailable") { + doLast { + if (!project.isBintrayAvailable()) { + error("bintray isn't available. ") + } + } + } + + if (isBintrayAvailable()) { + publishing { + repositories { + maven { + setUrl("https://api.bintray.com/maven/him188moe/mirai/mirai-core/;publish=1;override=1") + + credentials { + username = Bintray.getUser(project) + password = Bintray.getKey(project) + } + } + } + } + } +} + +@Suppress("NOTHING_TO_INLINE") +inline fun Project.configurePublishing( + artifactId: String, + bintrayRepo: String = "mirai", + bintrayPkgName: String = artifactId, + vcs: String = "https://github.com/mamoe/mirai" +) { + configureBintray() + apply() + + if (!project.isBintrayAvailable()) { + println("bintray isn't available. NO PUBLICATIONS WILL BE SET") + return + } + + bintray { + user = Bintray.getUser(project) + key = Bintray.getKey(project) + + setPublications("mavenJava") + setConfigurations("archives") + + publish = true + override = true + + pkg.apply { + repo = bintrayRepo + name = bintrayPkgName + setLicenses("AGPLv3") + publicDownloadNumbers = true + vcsUrl = vcs + } + } + + val sourcesJar by tasks.registering(Jar::class) { + archiveClassifier.set("sources") + from(sourceSets["main"].allSource) + } + + publishing { + publications { + register("mavenJava", MavenPublication::class) { + from(components["java"]) + + groupId = rootProject.group.toString() + setArtifactId(artifactId) + version = project.version.toString() + + pom.withXml { + val root = asNode() + root.appendNode("description", description) + root.appendNode("name", project.name) + root.appendNode("url", vcs) + root.children().last() + } + + artifact(sourcesJar.get()) + } + } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Mpp.kt b/buildSrc/src/main/kotlin/Mpp.kt new file mode 100644 index 000000000..f6387808a --- /dev/null +++ b/buildSrc/src/main/kotlin/Mpp.kt @@ -0,0 +1,58 @@ +/* + * 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 + */ + +import org.gradle.api.NamedDomainObjectCollection +import org.gradle.api.NamedDomainObjectProvider +import org.gradle.api.Project +import org.gradle.kotlin.dsl.provideDelegate + +/* + * 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 + */ + +val Project.isAndroidSDKAvailable: Boolean + get() { + val isAndroidSDKAvailable: Boolean by this + return isAndroidSDKAvailable + } + +val NamedDomainObjectCollection.androidMain: NamedDomainObjectProvider + get() = named("androidMain") + +val NamedDomainObjectCollection.jvmMain: NamedDomainObjectProvider + get() = named("jvmMain") + +val NamedDomainObjectCollection.androidTest: NamedDomainObjectProvider + get() = named("androidTest") + +val NamedDomainObjectCollection.jvmTest: NamedDomainObjectProvider + get() = named("jvmTest") + +val NamedDomainObjectCollection.commonMain: NamedDomainObjectProvider + get() = named("commonMain") + +fun Project.printAndroidNotInstalled() { +// 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() +// ) +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/MppPublishing.kt b/buildSrc/src/main/kotlin/MppPublishing.kt new file mode 100644 index 000000000..eaff17ab6 --- /dev/null +++ b/buildSrc/src/main/kotlin/MppPublishing.kt @@ -0,0 +1,128 @@ +/* + * 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.Project +import org.gradle.api.XmlProvider +import org.gradle.api.publish.maven.MavenArtifact +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.jvm.tasks.Jar +import org.gradle.kotlin.dsl.get +import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.withType + +fun logPublishing(message: String) { + println("[Publishing] Configuring $message") +} + +fun Project.configureMppPublishing() { + configureBintray() + + // mirai does some magic on MPP targets + afterEvaluate { + tasks.findByName("compileKotlinCommon")?.enabled = false + tasks.findByName("compileTestKotlinCommon")?.enabled = false + + tasks.findByName("compileCommonMainKotlinMetadata")?.enabled = false + tasks.findByName("compileKotlinMetadata")?.enabled = false + + tasks.findByName("generateMetadataFileForKotlinMultiplatformPublication")?.enabled = false // FIXME: 2021/1/21 + } + + tasks.withType { + doFirst { + 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" + }) + } + } + } + } + + val stubJavadoc = tasks.register("javadocJar", Jar::class) { + @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") + archiveClassifier.set("javadoc") + } + + afterEvaluate { + publishing { + logPublishing("Publications: ${publications.joinToString { it.name }}") + + publications.filterIsInstance().forEach { publication -> + if (publication.name != "kotlinMultiplatform") { + publication.artifact(stubJavadoc) + } + + logPublishing(publication.name) + when (val type = publication.name) { + "kotlinMultiplatform" -> { + publication.artifactId = project.name + publishPlatformArtifactsInRootModule(publications.getByName("jvm") as MavenPublication) + } + "metadata" -> { // TODO: 2021/1/21 seems no use. none `type` is "metadata" + publication.artifactId = "${project.name}-metadata" + } + "common" -> { + } + else -> { + // "jvm", "native", "js" + publication.artifactId = "${project.name}-$type" + } + } + } + } + } +} + +val publishPlatformArtifactsInRootModule: Project.(MavenPublication) -> Unit = { platformPublication -> + lateinit var platformPomBuilder: XmlProvider + platformPublication.pom.withXml { platformPomBuilder = this } + + publications.getByName("kotlinMultiplatform").let { it as MavenPublication }.run { + this.artifacts.removeIf { + it.classifier == null && it.extension == "jar" + // mirai-core\build\libs\mirai-core-2.0.0.jar, classifier=null, ext=jar + } + + logPublishing("Existing artifacts in kotlinMultiplatform: " + + this.artifacts.joinToString("\n", prefix = "\n") { it.smartToString() } + ) + + platformPublication.artifacts.forEach { + logPublishing("Adding artifact to kotlinMultiplatform: ${it.smartToString()}") + artifact(it) + } + + + // replace pom + pom.withXml { + val pomStringBuilder = asString() + pomStringBuilder.setLength(0) + platformPomBuilder.toString().lines().forEach { line -> + if (!line.contains("