build.gradle.kts 2.9 KB

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