version.gradle 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Version Info Examples
  3. =====================
  4. Nightly Build Snapshot
  5. * git tag:
  6. * Full Version: 3.1-5124
  7. * POM Version: 3.1.0-SNAPSHOT
  8. * NBM Revision: 5124
  9. * NBM UC Suffix: nightly/3.1/plugins
  10. Nightly Build Snapshot (PBRIsComing branch)
  11. * git tag:
  12. * Full Version: 3.1-PBRIsComing-5124
  13. * POM Version: 3.1.0-PBRIsComing-SNAPSHOT
  14. * NBM Revision: 5124
  15. * NBM UC Suffix: PBRIsComing-nightly/3.1/plugins
  16. Alpha1 Release
  17. * git tag: v3.1.0-alpha1
  18. * Full Version: 3.1-alpha1
  19. * POM Version: 3.1.0-alpha1
  20. * NBM Revision: 0
  21. * NBM UC Suffix: stable/3.1/plugins
  22. Final Release
  23. * git tag: v3.1.0
  24. * Full Version: 3.1
  25. * POM Version: 3.1.0
  26. * NBM Revision: 0
  27. * NBM UC Suffix: stable/3.1/plugins
  28. */
  29. import java.text.SimpleDateFormat
  30. import org.ajoberstar.grgit.*
  31. buildscript {
  32. repositories {
  33. mavenCentral()
  34. }
  35. dependencies {
  36. classpath 'org.ajoberstar:gradle-git:1.2.0'
  37. }
  38. }
  39. ext {
  40. jmeRevision = 0
  41. jmeNbmRevision = 0
  42. jmeGitHash = ""
  43. jmeGitTag = ""
  44. jmeShortGitHash = ""
  45. jmeBuildDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  46. jmeBranchName = "unknown"
  47. jmeFullVersion = "${jmeVersion}-UNKNOWN"
  48. jmePomVersion = "unknown"
  49. jmeNbmUcSuffix = "unknown"
  50. }
  51. def getReleaseInfo(String tag) {
  52. if (tag == null || tag == "") {
  53. // not a tagged commit
  54. return null;
  55. }
  56. /*if (!tag.startsWith("v")) {
  57. // syntax error
  58. return null;
  59. }
  60. tag = tag.substring(1)
  61. The SDK has it’s own versioning scheme which doesn’t start with v…*/
  62. String[] parts = tag.split("-");
  63. String mainVersion;
  64. boolean prerelease;
  65. String releaseName = null;
  66. if (parts.length == 2) {
  67. // prerelease
  68. prerelease = true;
  69. mainVersion = parts[0];
  70. releaseName = parts[1];
  71. if (releaseName.size() == 0) {
  72. // syntax error
  73. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: Release Name is of Length 0!";
  74. return null;
  75. }
  76. } else if (parts.length == 1) {
  77. // final release
  78. prerelease = false;
  79. mainVersion = parts[0];
  80. } else if (parts.length == 3) {
  81. // sdk doesn't really differentiate.
  82. prerelease = true;
  83. mainVersion = parts[0];
  84. releaseName = parts[1];
  85. } else {
  86. // error
  87. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Tag didn't contain the expected number of dash-seperated keywords"
  88. return null;
  89. }
  90. if (mainVersion.size() == 0) {
  91. // syntax error
  92. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Main Version (e.g. 3.1) couldn't be extracted successfully."
  93. return null;
  94. }
  95. parts = mainVersion.split("\\.");
  96. if (parts.size() == 2) {
  97. mainVersion = mainVersion + ".0" // Assume Revision Zero
  98. parts = mainVersion.split("\\.");
  99. }
  100. if (parts.size() != 3) {
  101. // syntax error
  102. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Tags Main Version didn't consist of two/three parts"
  103. return null;
  104. }
  105. String baseVersion = parts[0] + "." + parts[1];
  106. return [
  107. "tag" : tag,
  108. "baseVersion" : baseVersion,
  109. "mainVersion" : mainVersion,
  110. "prerelease" : prerelease,
  111. "releaseName" : releaseName,
  112. "releaseSuffix": (prerelease ? "-${releaseName}": "")
  113. ]
  114. }
  115. task configureVersionInfo {
  116. try {
  117. def grgit = Grgit.open(project.file('.'))
  118. def head = grgit.head()
  119. jmeRevision = grgit.log(includes: [head]).size()
  120. jmeGitHash = head.id
  121. jmeShortGitHash = head.abbreviatedId
  122. jmeBranchName = grgit.branch.current.name
  123. if (project.hasProperty("tag_name")) {
  124. jmeGitTag = project.getProperty("tag_name")
  125. } else {
  126. jmeGitTag = grgit.tag.list().find { it.commit == head }
  127. }
  128. def releaseInfo = getReleaseInfo(jmeGitTag)
  129. if (releaseInfo != null) {
  130. jmeFullVersion = "${releaseInfo.baseVersion}${releaseInfo.releaseSuffix}"
  131. jmePomVersion = "${releaseInfo.mainVersion}${releaseInfo.releaseSuffix}"
  132. jmeNbmRevision = jmeRevision
  133. jmeNbmUcSuffix = "stable/${releaseInfo.baseVersion}/plugins"
  134. } else {
  135. // SNAPSHOT
  136. jmeFullVersion = jmeMainVersion
  137. jmePomVersion = jmeVersion
  138. if (System.env.TRAVIS_BRANCH != null) {
  139. jmeBranchName = System.env.TRAVIS_BRANCH
  140. }
  141. if (System.env.TRAVIS_PULL_REQUEST != null &&
  142. System.env.TRAVIS_PULL_REQUEST != "false") {
  143. jmeBranchName += "-pr-" + System.env.TRAVIS_PULL_REQUEST
  144. }
  145. if (jmeBranchName != "master") {
  146. jmeFullVersion += "-${jmeBranchName}"
  147. jmePomVersion += "-${jmeBranchName}"
  148. jmeNbmUcSuffix = "${jmeBranchName}-"
  149. } else {
  150. jmeNbmUcSuffix = ""
  151. }
  152. jmeNbmUcSuffix += "nightly/" + jmeMainVersion + "/plugins"
  153. jmeFullVersion += "-${jmeRevision}"
  154. jmePomVersion += "-SNAPSHOT"
  155. jmeNbmRevision = jmeRevision
  156. }
  157. logger.warn("Full Version: ${jmeFullVersion}")
  158. logger.warn("POM Version: ${jmePomVersion}")
  159. logger.warn("NBM Revision: ${jmeNbmRevision}")
  160. logger.warn("NBM UC Suffix: ${jmeNbmUcSuffix}")
  161. } catch (ex) {
  162. // Failed to get repo info
  163. logger.warn("Failed to get repository info: " + ex.message + ". " + \
  164. "Only partial build info will be generated.")
  165. }
  166. }