2020-06-20 22:55:07 +08:00
|
|
|
/*
|
|
|
|
* 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("UnstableApiUsage")
|
|
|
|
|
|
|
|
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
|
2020-06-22 18:12:38 +08:00
|
|
|
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
2020-06-20 22:55:07 +08:00
|
|
|
import org.gradle.api.Plugin
|
|
|
|
import org.gradle.api.Project
|
|
|
|
import org.gradle.kotlin.dsl.apply
|
2020-06-22 18:12:38 +08:00
|
|
|
import org.gradle.kotlin.dsl.attributes
|
2020-06-20 22:55:07 +08:00
|
|
|
import org.gradle.kotlin.dsl.creating
|
|
|
|
import java.io.File
|
|
|
|
import kotlin.math.pow
|
|
|
|
|
|
|
|
class MiraiConsoleBuildPlugin : Plugin<Project> {
|
|
|
|
override fun apply(target: Project) = target.run {
|
|
|
|
apply<ShadowPlugin>()
|
|
|
|
|
|
|
|
if (tasks.none { it.name == "shadowJar" }) {
|
|
|
|
return@run
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks.getByName("shadowJar") {
|
2020-06-22 18:12:38 +08:00
|
|
|
with(this as ShadowJar) {
|
|
|
|
archiveFileName.set(
|
|
|
|
"${target.name}-${target.version}.jar"
|
|
|
|
)
|
|
|
|
manifest {
|
|
|
|
attributes(
|
|
|
|
"Manifest-Version" to "1",
|
|
|
|
"Implementation-Vendor" to "Mamoe Technologies",
|
|
|
|
"Implementation-Title" to target.name.toString(),
|
|
|
|
"Implementation-Version" to target.version.toString() + "-" + gitVersion
|
|
|
|
)
|
2020-06-20 22:55:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks.creating {
|
|
|
|
group = "mirai"
|
|
|
|
dependsOn(tasks.getByName("shadowJar"))
|
|
|
|
|
|
|
|
doFirst {
|
|
|
|
timeout.set(java.time.Duration.ofHours(3))
|
|
|
|
findLatestFile().let { (_, file) ->
|
|
|
|
val filename = file.name
|
|
|
|
println("Uploading file $filename")
|
|
|
|
runCatching {
|
|
|
|
upload.GitHub.upload(
|
|
|
|
file,
|
|
|
|
"https://api.github.com/repos/mamoe/mirai-repo/contents/shadow/${project.name}/$filename",
|
|
|
|
project
|
|
|
|
)
|
|
|
|
}.exceptionOrNull()?.let {
|
|
|
|
System.err.println("GitHub Upload failed")
|
|
|
|
it.printStackTrace() // force show stacktrace
|
|
|
|
throw it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks.creating {
|
|
|
|
group = "mirai"
|
|
|
|
dependsOn(tasks.getByName("shadowJar"))
|
|
|
|
|
|
|
|
doFirst {
|
|
|
|
timeout.set(java.time.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun Project.findLatestFile(): Map.Entry<String, File> {
|
|
|
|
return File(projectDir, "build/libs").walk()
|
|
|
|
.filter { it.isFile }
|
|
|
|
.onEach { println("all files=$it") }
|
|
|
|
.filter { it.name.matches(Regex("""${project.name}-[0-9][0-9]*(\.[0-9]*)*.*\.jar""")) }
|
|
|
|
.onEach { println("matched file: ${it.name}") }
|
|
|
|
.associateBy { it.nameWithoutExtension.substringAfterLast('-') }
|
|
|
|
.onEach { println("versions: $it") }
|
|
|
|
.maxBy { (version, _) ->
|
|
|
|
version.split('.').let {
|
|
|
|
if (it.size == 2) it + "0"
|
|
|
|
else it
|
|
|
|
}.reversed().foldIndexed(0) { index: Int, acc: Int, s: String ->
|
|
|
|
acc + 100.0.pow(index).toInt() * (s.toIntOrNull() ?: 0)
|
|
|
|
}
|
|
|
|
} ?: error("cannot find any file to upload")
|
|
|
|
}
|
2020-06-22 18:12:38 +08:00
|
|
|
|
|
|
|
val gitVersion: String by lazy {
|
|
|
|
runCatching {
|
|
|
|
val exec = Runtime.getRuntime().exec("git rev-parse HEAD")
|
|
|
|
exec.waitFor()
|
|
|
|
exec.inputStream.readBytes().toString(Charsets.UTF_8).trim().also {
|
|
|
|
println("Git commit id: $it")
|
|
|
|
}
|
|
|
|
}.onFailure {
|
|
|
|
it.printStackTrace()
|
|
|
|
return@lazy "UNKNOWN"
|
|
|
|
}.getOrThrow()
|
|
|
|
}
|