build.gradle.kts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2019-2021 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/master/LICENSE
  8. */
  9. import keys.SecretKeys
  10. import java.io.ByteArrayOutputStream
  11. plugins {
  12. id("io.codearte.nexus-staging") version "0.22.0"
  13. }
  14. description = "Mirai CI Methods for Releasing"
  15. nexusStaging {
  16. packageGroup = rootProject.group.toString()
  17. val keys = SecretKeys.getCache(project).loadKey("sonatype")
  18. username = keys.user
  19. password = keys.password
  20. }
  21. tasks.register("updateSnapshotVersion") {
  22. group = "mirai"
  23. doLast {
  24. rootProject.file("buildSrc/src/main/kotlin/Versions.kt").run {
  25. var text = readText()
  26. val template = { version: Any? -> "/*PROJECT_VERSION_START*/\"${version}\"/*PROJECT_VERSION_END*/" }
  27. check(text.indexOf(template(project.version)) != -1) { "Cannot find ${template(project.version)}" }
  28. text = text.replace(template(project.version), template(snapshotVersion))
  29. writeText(text)
  30. }
  31. }
  32. }
  33. tasks.register("publishSnapshotPage") {
  34. doLast {
  35. UpdateSnapshotPage.run(project, getSha())
  36. }
  37. }
  38. val snapshotVersion by lazy { getSnapshotVersionImpl() }
  39. fun getSnapshotVersionImpl(): String {
  40. val branch = System.getenv("CURRENT_BRANCH_NAME")
  41. logger.info("Current branch name is '$branch'")
  42. val sha = getSha().trim().take(8)
  43. return "${Versions.project}-$branch-${sha}".also {
  44. logger.info("Snapshot version is '$it'")
  45. }
  46. }
  47. //tasks.register("createTagOnGitHub") {
  48. // group = "mirai"
  49. // dependsOn(gradle.includedBuild("snapshots-publishing").task(":check"))
  50. //
  51. // doLast {
  52. // val token = System.getenv("MAMOE_TOKEN")
  53. // require(!token.isNullOrBlank()) { "" }
  54. //
  55. // val out = ByteArrayOutputStream()
  56. // exec {
  57. // commandLine("git")
  58. // args("rev-parse", "HEAD")
  59. // standardOutput = out
  60. // workingDir = rootProject.projectDir
  61. // }
  62. // val sha = out.toString()
  63. // logger.info("Current sha is $sha")
  64. //
  65. // runBlocking {
  66. // val resp = HttpClient().post<String>("https://api.github.com/repos/mamoe/mirai/git/refs") {
  67. // header("Authorization", "token $token")
  68. // header("Accept", "application/vnd.github.v3+json")
  69. // body = Gson().toJson(
  70. // mapOf(
  71. // "ref" to "refs/tags/build-$nextVersion",
  72. // "sha" to sha,
  73. // )
  74. // )
  75. // }
  76. // logger.info(resp)
  77. // }
  78. // }
  79. //}
  80. fun getSha(): String {
  81. val out = ByteArrayOutputStream()
  82. exec {
  83. commandLine("git")
  84. args("rev-parse", "HEAD")
  85. standardOutput = out
  86. workingDir = rootProject.projectDir
  87. }
  88. val sha = out.toString()
  89. logger.info("Current commit sha is '$sha'")
  90. return sha
  91. }