build.gradle.kts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. import keys.SecretKeys
  10. import kotlinx.validation.sourceSets
  11. import java.io.ByteArrayOutputStream
  12. plugins {
  13. id("io.codearte.nexus-staging") version "0.22.0"
  14. kotlin("jvm")
  15. }
  16. tasks.register<JavaExec>("runcihelper") {
  17. this.classpath = sourceSets["main"].runtimeClasspath
  18. this.mainClass.set("cihelper.CiHelperKt")
  19. this.workingDir = rootProject.projectDir
  20. fun Project.findPublishingExt(): PublishingExtension? {
  21. val exts = (this@findPublishingExt as ExtensionAware).extensions
  22. return exts.findByName("publishing") as PublishingExtension?
  23. }
  24. doFirst {
  25. @Suppress("USELESS_CAST")
  26. environment("PROJ_VERSION", (project.version as Any?).toString())
  27. rootProject.allprojects.asSequence()
  28. .mapNotNull { it.findPublishingExt() }
  29. .flatMap { it.publications.asSequence() }
  30. .mapNotNull { it as? MavenPublication }
  31. .map { it.artifactId }
  32. .joinToString("|")
  33. .let { environment("PROJ_ARTIFACTS", it) }
  34. rootProject.allprojects.asSequence()
  35. .mapNotNull { it.findPublishingExt() }
  36. .flatMap { it.repositories.asSequence() }
  37. .mapNotNull { it as? MavenArtifactRepository }
  38. .filter { it.name == "MiraiStageRepo" }
  39. .first().url
  40. .let { environment("PROJ_MiraiStageRepo", it.toString()) }
  41. val additionProperties = rootProject.properties.asSequence()
  42. .filter { (k, _) -> k.startsWith("cihelper.") }
  43. .map { (k, v) -> "-D$k=$v" }
  44. .toList()
  45. if (additionProperties.isNotEmpty()) {
  46. val currentJvmArgs = jvmArgs ?: emptyList()
  47. jvmArgs = currentJvmArgs + additionProperties
  48. }
  49. }
  50. }
  51. description = "Mirai CI Methods for Releasing"
  52. nexusStaging {
  53. packageGroup = rootProject.group.toString()
  54. val keys = SecretKeys.getCache(project).loadKey("sonatype")
  55. username = keys.user
  56. password = keys.password
  57. }
  58. dependencies {
  59. implementation(`kotlinx-serialization-json`)
  60. }
  61. tasks.register("updateSnapshotVersion") {
  62. group = "mirai"
  63. doLast {
  64. setProjectVersionForFutureBuilds(snapshotVersion)
  65. }
  66. }
  67. tasks.register("publishSnapshotPage") {
  68. doLast {
  69. UpdateSnapshotPage.run(project, getSha())
  70. }
  71. }
  72. val snapshotVersion by lazy { getSnapshotVersionImpl() }
  73. fun getSnapshotVersionImpl(): String {
  74. val branch = System.getenv("CURRENT_BRANCH_NAME")
  75. logger.info("Current branch name is '$branch'")
  76. val sha = getSha().trim().take(8)
  77. return "${Versions.project}-$branch-${sha}".also {
  78. logger.info("Snapshot version is '$it'")
  79. }
  80. }
  81. //tasks.register("createTagOnGitHub") {
  82. // group = "mirai"
  83. // dependsOn(gradle.includedBuild("snapshots-publishing").task(":check"))
  84. //
  85. // doLast {
  86. // val token = System.getenv("MAMOE_TOKEN")
  87. // require(!token.isNullOrBlank()) { "" }
  88. //
  89. // val out = ByteArrayOutputStream()
  90. // exec {
  91. // commandLine("git")
  92. // args("rev-parse", "HEAD")
  93. // standardOutput = out
  94. // workingDir = rootProject.projectDir
  95. // }
  96. // val sha = out.toString()
  97. // logger.info("Current sha is $sha")
  98. //
  99. // runBlocking {
  100. // val resp = HttpClient().post<String>("https://api.github.com/repos/mamoe/mirai/git/refs") {
  101. // header("Authorization", "token $token")
  102. // header("Accept", "application/vnd.github.v3+json")
  103. // body = Gson().toJson(
  104. // mapOf(
  105. // "ref" to "refs/tags/build-$nextVersion",
  106. // "sha" to sha,
  107. // )
  108. // )
  109. // }
  110. // logger.info(resp)
  111. // }
  112. // }
  113. //}
  114. fun getSha(): String {
  115. val out = ByteArrayOutputStream()
  116. exec {
  117. commandLine("git")
  118. args("rev-parse", "HEAD")
  119. standardOutput = out
  120. workingDir = rootProject.projectDir
  121. }
  122. val sha = out.toString()
  123. logger.info("Current commit sha is '$sha'")
  124. return sha
  125. }