From 72a478f712bfec439f09ad19c0f189e79ad2ebc3 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 22 Sep 2020 13:23:08 +0800 Subject: [PATCH] Update buildscript, add shadow md5 --- build.gradle.kts | 66 ++++++++++++----------- buildSrc/src/main/kotlin/Versions.kt | 2 +- buildSrc/src/main/kotlin/upload/GitHub.kt | 22 -------- buildSrc/src/main/kotlin/utils.kt | 56 +++++++++++++++++++ gradle/publish.gradle | 6 ++- 5 files changed, 97 insertions(+), 55 deletions(-) create mode 100644 buildSrc/src/main/kotlin/utils.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6ecf2e3ad..40cdeda81 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ @file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE") +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import org.jetbrains.dokka.gradle.DokkaTask import java.time.Duration import kotlin.math.pow @@ -16,7 +17,7 @@ buildscript { } dependencies { - classpath("com.github.jengelman.gradle.plugins:shadow:5.2.0") + classpath("com.github.jengelman.gradle.plugins:shadow:6.0.0") classpath("com.android.tools.build:gradle:${Versions.Android.androidGradlePlugin}") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.Kotlin.compiler}") classpath("org.jetbrains.kotlin:kotlin-serialization:${Versions.Kotlin.compiler}") @@ -74,15 +75,19 @@ subprojects { if (this@subprojects.name == "java-test") { return@subprojects } + afterEvaluate { + apply(plugin = "com.github.johnrengelman.shadow") val kotlin = runCatching { (this as ExtensionAware).extensions.getByName("kotlin") as? org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension }.getOrNull() ?: return@afterEvaluate - val shadowJvmJar by tasks.creating(com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar::class) { + val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{ + group = "mirai" + archiveClassifier.set("-all") val compilations = kotlin.targets.filter { it.platformType == org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.jvm } @@ -90,17 +95,17 @@ subprojects { compilations.forEach { dependsOn(it.compileKotlinTask) - } - - compilations.forEach { from(it.output) } - configurations = compilations.map { it.compileDependencyFiles as Configuration } + + println(project.configurations.joinToString()) + + from(project.configurations.getByName("jvmRuntimeClasspath")) this.exclude { file -> file.name.endsWith(".sf", ignoreCase = true) - .also { if (it) println("excluded ${file.name}") } } + this.manifest { this.attributes( "Manifest-Version" to 1, @@ -111,6 +116,29 @@ subprojects { } } + val shadowJarMd5 = tasks.register("shadowJarMd5") { + dependsOn("shadowJvmJar") + + val outFiles = shadowJvmJar.outputs.files.associateWith { file -> + File(file.parentFile, file.name.removeSuffix(".jar").removeSuffix("-all") + "-all.jar.md5") + } + + outFiles.forEach { (_, output) -> + outputs.files(output) + } + + doLast { + for ((origin, output) in outFiles) { + output + .also { it.createNewFile() } + .writeText(origin.inputStream().md5().toUHexString("").trim(Char::isWhitespace)) + } + } + + tasks.getByName("publish").dependsOn(this) + tasks.getByName("bintrayUpload").dependsOn(this) + }.get() + val githubUpload by tasks.creating { group = "mirai" dependsOn(shadowJvmJar) @@ -218,30 +246,6 @@ subprojects { } } } - - val cuiCloudUpload by tasks.creating { - group = "mirai" - dependsOn(shadowJvmJar) - - doFirst { - timeout.set(Duration.ofHours(3)) - findLatestFile().let { (_, file) -> - val filename = file.name - println("Uploading file $filename") - runCatching { - upload.CuiCloud.upload( - file, - project - ) - }.exceptionOrNull()?.let { - System.err.println("CuiCloud Upload failed") - it.printStackTrace() // force show stacktrace - throw it - } - } - } - - } } afterEvaluate { diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 4c41aa563..94e04c8d8 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -9,7 +9,7 @@ object Versions { object Mirai { - const val version = "1.3.1" + const val version = "1.3.2-dev-5" } object Kotlin { diff --git a/buildSrc/src/main/kotlin/upload/GitHub.kt b/buildSrc/src/main/kotlin/upload/GitHub.kt index d57aef119..ace819be9 100644 --- a/buildSrc/src/main/kotlin/upload/GitHub.kt +++ b/buildSrc/src/main/kotlin/upload/GitHub.kt @@ -26,9 +26,6 @@ import org.gradle.kotlin.dsl.provideDelegate import org.jsoup.Connection import org.jsoup.Jsoup import java.io.File -import java.io.InputStream -import java.io.OutputStream -import java.security.MessageDigest import java.util.* internal val Http = HttpClient(CIO) { @@ -74,25 +71,6 @@ object GitHub { ) } - fun InputStream.md5(): ByteArray { - val digest = MessageDigest.getInstance("md5") - digest.reset() - use { input -> - object : OutputStream() { - override fun write(b: Int) { - digest.update(b.toByte()) - } - - override fun write(b: ByteArray, off: Int, len: Int) { - digest.update(b, off, len) - } - }.use { output -> - input.copyTo(output) - } - } - return digest.digest() - } - fun ByteArray.hex(): String = buildString(size * 2) { this@hex.forEach { byte -> val uint = Integer.toHexString(byte.toInt() and 0xFF) diff --git a/buildSrc/src/main/kotlin/utils.kt b/buildSrc/src/main/kotlin/utils.kt new file mode 100644 index 000000000..3b7230f4b --- /dev/null +++ b/buildSrc/src/main/kotlin/utils.kt @@ -0,0 +1,56 @@ +import java.io.InputStream +import java.io.OutputStream +import java.security.MessageDigest + +/* + * + * * 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 + * + */ + +@OptIn(ExperimentalUnsignedTypes::class) +fun ByteArray.toUHexString( + separator: String = " ", + offset: Int = 0, + length: Int = this.size - offset +): String { + if (length == 0) { + return "" + } + val lastIndex = offset + length + return buildString(length * 2) { + this@toUHexString.forEachIndexed { index, it -> + if (index in offset until lastIndex) { + var ret = it.toUByte().toString(16).toUpperCase() + if (ret.length == 1) ret = "0$ret" + append(ret) + if (index < lastIndex - 1) append(separator) + } + } + } +} + + +fun InputStream.md5(): ByteArray { + val digest = MessageDigest.getInstance("md5") + digest.reset() + use { input -> + object : OutputStream() { + override fun write(b: Int) { + digest.update(b.toByte()) + } + + override fun write(b: ByteArray, off: Int, len: Int) { + digest.update(b, off, len) + } + }.use { output -> + input.copyTo(output) + } + } + return digest.digest() +} diff --git a/gradle/publish.gradle b/gradle/publish.gradle index 5ce9b910d..e062df133 100644 --- a/gradle/publish.gradle +++ b/gradle/publish.gradle @@ -53,7 +53,7 @@ try { task stubJavadoc(type: Jar) { archiveClassifier = 'javadoc' } -}catch (Exception ignored) { +} catch (Exception ignored) { } @@ -123,6 +123,10 @@ afterEvaluate { case 'jvm': it.artifactId = isKotlin137x ? "$variantName" : "$variantName-jvm" + def files = tasks.getByName("shadowJarMd5").outputs.files + tasks.getByName("shadowJvmJar").outputs.files + for (f in files) { + artifact f + } break case 'js':