mirai/buildSrc/src/main/kotlin/MiraiConsoleBuildPlugin.kt

136 lines
4.9 KiB
Kotlin
Raw Normal View History

2020-06-20 22:55:07 +08:00
/*
2020-08-16 23:36:24 +08:00
* Copyright 2019-2020 Mamoe Technologies and contributors.
2020-06-20 22:55:07 +08:00
*
2020-08-23 17:46:51 +08:00
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link.
2020-06-20 22:55:07 +08:00
*
* 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-08-19 22:57:08 +08:00
import org.gradle.kotlin.dsl.getByName
2020-06-20 22:55:07 +08:00
import java.io.File
class MiraiConsoleBuildPlugin : Plugin<Project> {
override fun apply(target: Project) = target.run {
apply<ShadowPlugin>()
val ext = target.extensions.getByName("ext") as org.gradle.api.plugins.ExtraPropertiesExtension
2020-06-20 22:55:07 +08:00
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(
2020-08-16 23:21:11 +08:00
"${target.name}-${target.version}-all.jar"
2020-06-22 18:12:38 +08:00
)
manifest {
attributes(
"Manifest-Version" to "1",
"Implementation-Vendor" to "Mamoe Technologies",
2020-09-27 02:32:57 +08:00
"Implementation-Title" to target.name,
"Implementation-Version" to target.version.toString() //+ "+" + gitVersion
2020-06-22 18:12:38 +08:00
)
2020-06-20 22:55:07 +08:00
}
@Suppress("UNCHECKED_CAST")
kotlin.runCatching {
(ext["shadowJar"] as? ShadowJar.() -> Unit)?.invoke(this)
}
2020-06-20 22:55:07 +08:00
}
}
2020-06-29 16:40:09 +08:00
tasks.create("githubUpload") {
2020-06-20 22:55:07 +08:00
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,
project,
"mirai-repo",
"shadow/${project.name}/${filename.replace("-all", "")}"
2020-06-20 22:55:07 +08:00
)
}.exceptionOrNull()?.let {
System.err.println("GitHub Upload failed")
it.printStackTrace() // force show stacktrace
throw it
}
}
}
}
2020-06-29 16:40:09 +08:00
tasks.create("cuiCloudUpload") {
2020-06-20 22:55:07 +08:00
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
}
}
}
}
}
}
2020-08-19 22:57:08 +08:00
fun Project.findLatestFile(): Pair<String, File> {
return tasks.getByName("shadowJar", ShadowJar::class).run {
val file = archiveFile.get().asFile
this@findLatestFile.version.toString() to file
}/*
2020-06-20 22:55:07 +08:00
return File(projectDir, "build/libs").walk()
.filter { it.isFile }
.onEach { println("all files=$it") }
2020-08-16 23:21:11 +08:00
.filter { it.name.matches(Regex("""${project.name}-[0-9][0-9]*(\.[0-9]*)*.*\-all\.jar""")) }
2020-06-20 22:55:07 +08:00
.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)
}
2020-08-19 22:57:08 +08:00
} ?: error("cannot find any file to upload")*/
2020-06-20 22:55:07 +08:00
}
2020-06-22 18:12:38 +08:00
2020-09-27 02:32:57 +08:00
/*
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()
}
2020-09-27 02:32:57 +08:00
*/