Update buildscript, add shadow md5

This commit is contained in:
Him188 2020-09-22 13:23:08 +08:00
parent a774b8a706
commit 72a478f712
5 changed files with 97 additions and 55 deletions

View File

@ -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 {

View File

@ -9,7 +9,7 @@
object Versions {
object Mirai {
const val version = "1.3.1"
const val version = "1.3.2-dev-5"
}
object Kotlin {

View File

@ -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)

View File

@ -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()
}

View File

@ -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':