diff init20.gradle @ 215:5bebb590b30e

DEP-11: determine project version based on branch
author smith@nwoca.org
date Tue, 28 Jun 2016 23:01:21 +0100
parents 0ca4f3c952b7
children b628d49d2891
line wrap: on
line diff
--- a/init20.gradle	Mon Jun 27 19:50:00 2016 +0100
+++ b/init20.gradle	Tue Jun 28 23:01:21 2016 +0100
@@ -1,5 +1,7 @@
 import groovy.sql.Sql
+import groovy.transform.Sortable
 import groovy.transform.ToString
+import groovy.transform.TupleConstructor
 
 buildscript {
     repositories {
@@ -73,8 +75,7 @@
 gradle.ext.branchName = gradle.branch.name
 gradle.ext.branchStream = gradle.branch.stream
 gradle.ext.branchHash = gradle.branch.hash
-println "${gradle.hgRepositoryUrl} ${gradle.branch} ${gradle.branch.imageId} ${gradle.branch.deployName}" 
-println "$gradle.runtimeInfo"
+println "${gradle.hgRepositoryUrl} ${gradle.branch} ${gradle.branch.version}"
 
 loadEnvironments()
 
@@ -344,23 +345,33 @@
     def void projectsEvaluated(Gradle gradle) {
         def ssdtArtifactory = 'http://repos.ssdt.nwoca.org/artifactory'
         Project root = gradle.getRootProject()
+
+
+        def branchVersioning = gradle.rootProject.version == 'unspecified'
+
         root.allprojects {
 
             def thisProject = delegate
             thisProject.status = 'integration'
             if (gradle.branchStream) {
-                thisProject.status = 'integration'
-                def fixupVersion = thisProject.version - ".SNAPSHOT"
-                if (gradle.branchStream == 'feature') {
-                    fixupVersion = fixupVersion + ".SNAPSHOT"
+                if (branchVersioning) {
+                    thisProject.version = gradle.branch.version
+                    thisProject.status = gradle.branch.defaultDependencyStatus
+                } else {
+
+                    thisProject.status = 'integration'
+                    def fixupVersion = thisProject.version - ".SNAPSHOT"
+                    if (gradle.branchStream == 'feature') {
+                        fixupVersion = fixupVersion + ".SNAPSHOT"
+                    }
+                    if (gradle.branchStream == 'develop') {
+                        fixupVersion = fixupVersion + ".SNAPSHOT"
+                    }
+                    if (gradle.branchStream in ['production', 'release', 'hotfix']) {
+                        thisProject.status = 'release'
+                    }
+                    thisProject.version = fixupVersion
                 }
-                 if (gradle.branchStream == 'develop') {
-                    fixupVersion = fixupVersion + ".SNAPSHOT"
-                }
-                if (gradle.branchStream in ['production', 'release','hotfix']) {
-                    thisProject.status = 'release'
-                }
-                thisProject.version = fixupVersion
             }
 
             configurations.all {
@@ -582,12 +593,33 @@
 }
 
 
+@TupleConstructor
+@Sortable
+class Version {
+
+    Integer major = 0
+    Integer minor = 0
+    Integer patch = 0
+    Boolean snapshot = true
+
+    Version nextVersion() {
+        new Version(major, minor + 1, 0)
+    }
+
+    String toString() {
+        "${major}.${minor}.${patch}${snapshot ? '.SNAPSHOT' : ''}"
+    }
+
+}
+
+
 @ToString(includes=['name','shortName','buildVersion','imageId','deployName'],includeNames= true)
 class BranchInfo {
     def name
     def stream = "none"
 	def buildNumber = ""	
 	def changeset = ""
+    def version
 
     BranchInfo(name) {
         this.name = name
@@ -598,22 +630,37 @@
         determineStream()
 		buildNumber = System.getenv('bamboo_buildNumber') ?: ""
 		changeset = System.getenv('bamboo_planRepository_revision') ?: ""
-
 	}
 	
 	String getDefaultDependencyStatus() {
-		return stream in ['release','hotfix'] ? 'release' : 'integration'
+		return isRelease() ? 'release' : 'integration'
 	}
-	
-	def getShortName() {
+
+    private boolean isRelease() {
+        return stream in ['release', 'hotfix']
+    }
+
+    def getShortName() {
 		def result = name.contains('/') ? name.split('/')[1] : name 
 	} 
 	
-	def getBuildVersion() {
-		stream in ['release','hotfix'] ? shortName - "v": ""
+	String getBuildVersion() {
+		def v = isRelease() ? shortName - "v": ""
+        return v
 	}
-	
-	def getImageId() {
+
+    def Version getVersion() {
+        if (!version) {
+            if (isRelease()) {
+                version = new Version(*getBuildVersion().split('\\.')*.toInteger(), false)
+            } else {
+                version = findSnapshotVersion()
+            }
+        }
+        return version
+    }
+
+    def getImageId() {
 		(buildVersion ?:  shortName.take(25)) + (buildNumber ? "-${buildNumber}" : "-0")
 	}
 
@@ -632,6 +679,20 @@
         new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
     }
 
+    private findSnapshotVersion() {
+        def versions = "hg branches --closed".execute().text.split('\n').findAll {
+            it.startsWith( 'release') || it.startsWith( 'hotfix')
+        }.collect {
+            it.replaceAll('\\s+',' ').split(' ')[0].split('/')[1] - 'v'
+        }.collect {
+            new Version(*it.split('\\.')*.toInteger())
+        }.sort { v1, v2 -> v2 <=> v1 }
+
+        return versions ?  versions.first().nextVersion() : new Version().nextVersion()
+
+    }
+
+
     def determineName()  {
         try {
             def branch = "hg branch".execute().text.trim()