version.gradle 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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) {
  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("-", 2);
  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 {
  81. // error
  82. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Tag didn't contain the expected number of dash-seperated keywords"
  83. return null;
  84. }
  85. if (mainVersion.size() == 0) {
  86. // syntax error
  87. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Main Version (e.g. 3.1) couldn't be extracted successfully."
  88. return null;
  89. }
  90. parts = mainVersion.split("\\.");
  91. if (parts.size() == 2) {
  92. mainVersion = mainVersion + ".0" // Assume Revision Zero
  93. parts = mainVersion.split("\\.");
  94. }
  95. if (parts.size() != 3) {
  96. // syntax error
  97. println "Warning: getReleaseInfo() found an errorneous tag: \"" + tag + "\". Syntax Error: The Tags Main Version didn't consist of two/three parts"
  98. return null;
  99. }
  100. String baseVersion = parts[0] + "." + parts[1];
  101. return [
  102. "tag" : tag,
  103. "baseVersion" : baseVersion,
  104. "mainVersion" : mainVersion,
  105. "prerelease" : prerelease,
  106. "releaseName" : releaseName,
  107. "releaseSuffix": (prerelease ? "-${releaseName}": "")
  108. ]
  109. }
  110. task configureVersionInfo {
  111. try {
  112. def grgit = Grgit.open(project.file('.'))
  113. def head = grgit.head()
  114. jmeRevision = grgit.log(includes: [head]).size()
  115. jmeGitHash = head.id
  116. jmeShortGitHash = head.abbreviatedId
  117. jmeBranchName = grgit.branch.current.name
  118. jmeGitTag = grgit.tag.list().find { it.commit == head }
  119. if (jmeGitTag != null) {
  120. jmeGitTag = jmeGitTag.name
  121. } else {
  122. jmeGitTag = System.env.TRAVIS_TAG
  123. }
  124. def releaseInfo = getReleaseInfo(jmeGitTag)
  125. if (releaseInfo != null) {
  126. jmeFullVersion = "${releaseInfo.baseVersion}${releaseInfo.releaseSuffix}"
  127. jmePomVersion = "${releaseInfo.mainVersion}${releaseInfo.releaseSuffix}"
  128. jmeNbmRevision = jmeRevision
  129. jmeNbmUcSuffix = "stable/${releaseInfo.baseVersion}/plugins"
  130. } else {
  131. // SNAPSHOT
  132. jmeFullVersion = jmeMainVersion
  133. jmePomVersion = jmeVersion
  134. if (System.env.TRAVIS_BRANCH != null) {
  135. jmeBranchName = System.env.TRAVIS_BRANCH
  136. }
  137. if (System.env.TRAVIS_PULL_REQUEST != null &&
  138. System.env.TRAVIS_PULL_REQUEST != "false") {
  139. jmeBranchName += "-pr-" + System.env.TRAVIS_PULL_REQUEST
  140. }
  141. if (jmeBranchName != "master") {
  142. jmeFullVersion += "-${jmeBranchName}"
  143. jmePomVersion += "-${jmeBranchName}"
  144. jmeNbmUcSuffix = "${jmeBranchName}-"
  145. } else {
  146. jmeNbmUcSuffix = ""
  147. }
  148. jmeNbmUcSuffix += "nightly/" + jmeMainVersion + "/plugins"
  149. jmeFullVersion += "-${jmeRevision}"
  150. jmePomVersion += "-SNAPSHOT"
  151. jmeNbmRevision = jmeRevision
  152. }
  153. logger.warn("Full Version: ${jmeFullVersion}")
  154. logger.warn("POM Version: ${jmePomVersion}")
  155. logger.warn("NBM Revision: ${jmeNbmRevision}")
  156. logger.warn("NBM UC Suffix: ${jmeNbmUcSuffix}")
  157. } catch (ex) {
  158. // Failed to get repo info
  159. logger.warn("Failed to get repository info: " + ex.message + ". " + \
  160. "Only partial build info will be generated.")
  161. }
  162. }