build.gradle.kts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. tasks.register("updateSnapshotVersion") {
  59. group = "mirai"
  60. doLast {
  61. setProjectVersionForFutureBuilds(snapshotVersion)
  62. }
  63. }
  64. tasks.register("publishSnapshotPage") {
  65. doLast {
  66. UpdateSnapshotPage.run(project, getSha())
  67. }
  68. }
  69. val snapshotVersion by lazy { getSnapshotVersionImpl() }
  70. fun getSnapshotVersionImpl(): String {
  71. val branch = System.getenv("CURRENT_BRANCH_NAME")
  72. logger.info("Current branch name is '$branch'")
  73. val sha = getSha().trim().take(8)
  74. return "${Versions.project}-$branch-${sha}".also {
  75. logger.info("Snapshot version is '$it'")
  76. }
  77. }
  78. //tasks.register("createTagOnGitHub") {
  79. // group = "mirai"
  80. // dependsOn(gradle.includedBuild("snapshots-publishing").task(":check"))
  81. //
  82. // doLast {
  83. // val token = System.getenv("MAMOE_TOKEN")
  84. // require(!token.isNullOrBlank()) { "" }
  85. //
  86. // val out = ByteArrayOutputStream()
  87. // exec {
  88. // commandLine("git")
  89. // args("rev-parse", "HEAD")
  90. // standardOutput = out
  91. // workingDir = rootProject.projectDir
  92. // }
  93. // val sha = out.toString()
  94. // logger.info("Current sha is $sha")
  95. //
  96. // runBlocking {
  97. // val resp = HttpClient().post<String>("https://api.github.com/repos/mamoe/mirai/git/refs") {
  98. // header("Authorization", "token $token")
  99. // header("Accept", "application/vnd.github.v3+json")
  100. // body = Gson().toJson(
  101. // mapOf(
  102. // "ref" to "refs/tags/build-$nextVersion",
  103. // "sha" to sha,
  104. // )
  105. // )
  106. // }
  107. // logger.info(resp)
  108. // }
  109. // }
  110. //}
  111. fun getSha(): String {
  112. val out = ByteArrayOutputStream()
  113. exec {
  114. commandLine("git")
  115. args("rev-parse", "HEAD")
  116. standardOutput = out
  117. workingDir = rootProject.projectDir
  118. }
  119. val sha = out.toString()
  120. logger.info("Current commit sha is '$sha'")
  121. return sha
  122. }