version.gradle 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import java.text.SimpleDateFormat
  2. import org.ajoberstar.grgit.*
  3. buildscript {
  4. repositories {
  5. mavenCentral()
  6. }
  7. dependencies {
  8. classpath 'org.ajoberstar:gradle-git:1.2.0'
  9. }
  10. }
  11. ext {
  12. jmeRevision = 0
  13. jmeGitHash = ""
  14. jmeGitTag = ""
  15. jmeShortGitHash = ""
  16. jmeBuildDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  17. jmeBranchName = "unknown"
  18. jmeFullVersion = "${jmeVersion}-SNAPSHOT"
  19. jmeVersionTag="SNAPSHOT"
  20. }
  21. task configureVersionInfo {
  22. try {
  23. // Users can configure behavior by setting properties on the command
  24. // line:
  25. //
  26. // jmeVersionName:
  27. // If set this will override all automatic version detection.
  28. //
  29. // useCommitHashAsVersionName:
  30. // If there is no jmeVersionName set and the current commit has no
  31. // specific tag then setting this to 'true' will cause the version to
  32. // be the full commit ID.
  33. //
  34. // includeBranchInVersion:
  35. // Set to true if a non-master branch name should be included in the automatically
  36. // generated version.
  37. def grgit = Grgit.open(project.file('.'))
  38. def head = grgit.head()
  39. jmeRevision = grgit.log(includes: [head]).size()
  40. jmeGitHash = head.id
  41. jmeShortGitHash = head.abbreviatedId
  42. jmeBranchName = grgit.branch.current.name
  43. // This code will find an exact-match tag if the current
  44. // commit is the same as the tag commit.
  45. jmeGitTag = grgit.tag.list().find { it.commit == head }
  46. def latestTag;
  47. if( jmeGitTag ) {
  48. // Just use the name. We keep jmeGitTag separate because there
  49. // is some logic that wants to know if this specific commit has
  50. // a tag versus 'whatever tag we are a child of'... which is what
  51. // 'latestTag' will be.
  52. jmeGitTag = jmeGitTag.name
  53. latestTag = jmeGitTag;
  54. } else {
  55. // Use describe to find the most recent tag. Unfortunately,
  56. // in this version of grgit, we don't have the 'always' options
  57. // so we can't make as many assumptions about the format of the
  58. // string.
  59. // If the commit is an exact match then it will return just the
  60. // tag name... else it will be tagName-commitCount-abbreviatedId
  61. // We'll use some groovy regex magic to get the tag either way.
  62. def describe = grgit.describe()
  63. def fullDescribe = (describe =~/(.*?)-(\d+)-g$jmeShortGitHash/)
  64. latestTag = fullDescribe ? fullDescribe[0][1] : describe
  65. println "Latest tag:" + latestTag
  66. }
  67. // We could enhance this with some more regex if we wanted to sanity
  68. // check that it was formatted like our versions.
  69. def tagVersion = (latestTag =~/v?(.*)/)[0][1];
  70. // If the branch is not master then use the tag.
  71. if( jmeBranchName != "master" ) {
  72. jmeVersion = tagVersion
  73. }
  74. // Parse out just the base version part. -SNAPSHOT versions
  75. // shouldn't really include our release suffixes
  76. def baseVersion = (jmeVersion =~/(\d+\.\d+.\d+)/)
  77. baseVersion = baseVersion.size() > 0 ? baseVersion[0][0] : jmeVersion
  78. if( !jmeVersionName ) {
  79. // If there is a specific tag for the top commit then we always
  80. // use that.
  81. if( jmeGitTag ) {
  82. println "Using GIT tag as version"
  83. jmeFullVersion = tagVersion; // already cleaned up
  84. jmeVersionTag = "" // and no SNAPSHOT suffix for an exact version tag
  85. // Note: this will not automatically add SNAPSHOT if the user has
  86. // local changes that they haven't committed. Technically, only
  87. // real CI builds should be using non-SNAPSHOT versions so we may
  88. // eventually want to change the script to always use -SNAPSHOT with
  89. // a CI option to turn it off.
  90. // We could also check the grgit.status for unstaged modified/removed files.
  91. // def unstaged = grgit.status().unstaged;
  92. // def modCount = unstaged.modified.size() + unstaged.removed.size()
  93. // ...but that seems like a wasteful check considering only official
  94. // version builds should not have a -SNAPSHOT.
  95. } else if( useCommitHashAsVersionName == "true" && jmeGitHash ) {
  96. // The user has opted to use the hash... and we actually have
  97. // a hash.
  98. println "Using commit ID as version"
  99. jmeFullVersion = jmeGitHash;
  100. jmeVersionTag = ""
  101. } else {
  102. println "Auto-detecting version"
  103. jmeVersionTag = "SNAPSHOT"
  104. if( includeBranchInVersion == "true" && jmeBranchName != "master" ) {
  105. jmeFullVersion = baseVersion + "-" + jmeBranchName + "-" + jmeVersionTag;
  106. } else {
  107. jmeFullVersion = baseVersion + "-" + jmeVersionTag;
  108. }
  109. }
  110. } else {
  111. // Just use defaults
  112. println "Using user-defined version"
  113. jmeFullVersion=jmeVersionName
  114. jmeVersionTag = "SNAPSHOT"
  115. }
  116. println("Revision: ${jmeRevision}")
  117. println("Hash: ${jmeGitHash}")
  118. println("Short Hash: ${jmeShortGitHash}")
  119. println("Tag: ${jmeGitTag}")
  120. println("Build Date: ${jmeBuildDate}")
  121. println("Build Branch: ${jmeBranchName}")
  122. println("Use commit hash as version ${useCommitHashAsVersionName}")
  123. println("Base Version: ${baseVersion}")
  124. println("Build Suffix: ${jmeVersionTag}")
  125. println("Build Version: ${jmeFullVersion}")
  126. } catch (ex) {
  127. // Failed to get repo info
  128. logger.warn("Failed to get repository info: " + ex.message + ". " + \
  129. "Only partial build info will be generated.")
  130. }
  131. }