build.gradle.kts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. val snapshotVersion by lazy { getSnapshotVersionImpl() }
  34. fun getSnapshotVersionImpl(): String {
  35. val branch = System.getenv("CURRENT_BRANCH_NAME")
  36. logger.info("Current branch name is '$branch'")
  37. val sha = getSha().trim().take(8)
  38. return "${Versions.project}-$branch-${sha}".also {
  39. logger.info("Snapshot version is '$it'")
  40. }
  41. }
  42. //tasks.register("createTagOnGitHub") {
  43. // group = "mirai"
  44. // dependsOn(gradle.includedBuild("snapshots-publishing").task(":check"))
  45. //
  46. // doLast {
  47. // val token = System.getenv("MAMOE_TOKEN")
  48. // require(!token.isNullOrBlank()) { "" }
  49. //
  50. // val out = ByteArrayOutputStream()
  51. // exec {
  52. // commandLine("git")
  53. // args("rev-parse", "HEAD")
  54. // standardOutput = out
  55. // workingDir = rootProject.projectDir
  56. // }
  57. // val sha = out.toString()
  58. // logger.info("Current sha is $sha")
  59. //
  60. // runBlocking {
  61. // val resp = HttpClient().post<String>("https://api.github.com/repos/mamoe/mirai/git/refs") {
  62. // header("Authorization", "token $token")
  63. // header("Accept", "application/vnd.github.v3+json")
  64. // body = Gson().toJson(
  65. // mapOf(
  66. // "ref" to "refs/tags/build-$nextVersion",
  67. // "sha" to sha,
  68. // )
  69. // )
  70. // }
  71. // logger.info(resp)
  72. // }
  73. // }
  74. //}
  75. fun getSha(): String {
  76. val out = ByteArrayOutputStream()
  77. exec {
  78. commandLine("git")
  79. args("rev-parse", "HEAD")
  80. standardOutput = out
  81. workingDir = rootProject.projectDir
  82. }
  83. val sha = out.toString()
  84. logger.info("Current commit sha is '$sha'")
  85. return sha
  86. }