diff init20.gradle @ 219:efa545c6dd65

DEP-11: implement release process as gradle rule
author smith@nwoca.org
date Fri, 01 Jul 2016 20:56:04 +0100
parents b628d49d2891
children 837c9249053f
line wrap: on
line diff
--- a/init20.gradle	Thu Jun 30 21:14:21 2016 +0100
+++ b/init20.gradle	Fri Jul 01 20:56:04 2016 +0100
@@ -341,8 +341,99 @@
         }
     }
 
+    tasks.addRule("release{Major|Minor|Patch|n.n.n}: create release branch") { String taskName ->
+
+        def matcher = (taskName =~ /^release(Major|Minor|Patch|\d{1,3}\.\d{1,3}\.\d{1,3})$/)
+        if (matcher.matches()) {
+
+
+            dependencyLock.globalLockFile =  'release.lock' 
+
+
+            task('doReleaseBranch') {
+                ext.requested = matcher[0][1].toLowerCase()
+            } << {
+                def releaseVersion = determineReleaseVersion(requested)
+                def releaseStream = releaseVersion.isHotfix() ? 'hotfix' : 'release'
+
+                println "-" * 60
+                println "Preparing to create branch\n"
+                println "\tproject:\t${gradle.rootProject.name}"
+                println "\tcurrent:\t${gradle.branch} ($gradle.branch.version)"
+                println()
+                println "\ttype   :\t${releaseStream.toUpperCase()}"
+                println "\tversion:\t${releaseVersion}"
+                println "\ttarget :\t${releaseStream}/v${releaseVersion}"
+                println()
+                println("-" * 60)
+                println "DRY RUN".center(60)
+                println("-" * 60)
+
+                println "hg flow ${releaseStream} start v${releaseVersion} --dirty --dry-run".execute().text
+
+                println "-" * 60
+
+                if (!confirmPrompt("Continue?")) {
+                    throw new BuildCancelledException("release branching canceled by user request")
+                }
+
+                println "hg flow ${releaseStream} start v${releaseVersion} --dirty".execute().text
+                println "hg update ${releaseStream}/v${releaseVersion}".execute().text
+
+            }
+
+            task('doCommitLock') << {
+                println 'hg status'.execute().text
+                println 'hg commit release.lock -A -m "lock dynamic dependencies for release"'.execute().text
+            }
+
+            def branchTasks = ['doReleaseBranch', 'deleteGlobalLock', 'generateGlobalLock', 'saveGlobalLock', 'doCommitLock']
+
+            task(taskName) {
+                dependsOn branchTasks
+            }
+
+            branchTasks.tail().inject(branchTasks.head()) { a, b ->
+                tasks[b].mustRunAfter a
+                b
+            }
+
+        }
+    }
+
 }
 
+private static String readLine(String message, String defaultValue = null) {
+    String _message = "> $message" + (defaultValue ? " [$defaultValue] " : "")
+    if (System.console()) {
+        return System.console().readLine(_message) ?: defaultValue
+    }
+    println "$_message "
+
+    System.in.newReader().readLine() ?: defaultValue
+}
+
+private static boolean confirmPrompt(String message, boolean defaultValue = false) {
+    String defaultStr = defaultValue ? 'Y' : 'n'
+    String consoleVal = readLine("${message} (Y|n)", defaultStr)
+    if (consoleVal) {
+        return consoleVal.toLowerCase().startsWith('y')
+    }
+
+    defaultValue
+}
+
+private Version determineReleaseVersion(String requested) {
+    if (requested == 'major') {
+        return gradle.branch.version.nextMajor()
+    } else if (requested == 'minor') {
+        return gradle.branch.version.nextMinor()
+    } else if (requested == 'patch') {
+        return gradle.branch.version.nextPatch()
+    } else {
+        return new Version(*requested.split(/\./)*.toInteger(), false)
+    }
+}
 
 class ArtifactoryGradleSettings extends BuildAdapter implements BuildListener {
 
@@ -608,10 +699,30 @@
     Integer patch = 0
     Boolean snapshot = true
 
-    Version nextVersion() {
+    Version nextMajor() {
+        new Version(major + 1, 0, 0, false)
+    }
+
+    Version nextMinor() {
+        if (snapshot) {
+            new Version(major, minor , 0,false)
+        } else {
+            new Version(major, minor + 1, 0,false)
+        }
+    }
+
+    Version nextPatch() {
+        new Version(major, minor, patch + 1,false)
+    }
+
+    Version nextSnapshot() {
         new Version(major, minor + 1, 0)
     }
 
+    boolean isHotfix() {
+        !snapshot && patch > 0
+    }
+
     String toString() {
         "${major}.${minor}.${patch}${snapshot ? '.SNAPSHOT' : ''}"
     }
@@ -694,7 +805,7 @@
             new Version(*it.split('\\.')*.toInteger())
         }.sort { v1, v2 -> v2 <=> v1 }
 
-        return versions ?  versions.first().nextVersion() : new Version().nextVersion()
+        return versions ?  versions.first().nextSnapshot() : new Version().nextSnapshot()
 
     }