package ch.dissem.gradle import org.gradle.api.Plugin import org.gradle.api.Project /** * Sets the version as follows: * */ class GitFlowVersion implements Plugin { def getBranch(Project project) { def stdout = new ByteArrayOutputStream() project.exec { commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD' standardOutput = stdout } return stdout.toString().trim() } def getTag(Project project) { def stdout = new ByteArrayOutputStream() project.exec { commandLine 'git', 'describe', '--abbrev=0' standardOutput = stdout } return stdout.toString().trim() } def isRelease(Project project) { return "master" == getBranch(project); } def getVersion(Project project) { if (project.ext.isRelease) { return getTag(project) } else { return getBranch(project).replaceAll("/", "-") + "-SNAPSHOT" } } @Override void apply(Project project) { project.ext.isRelease = isRelease(project) project.version = getVersion(project) project.task('version') << { println "Version deduced from git: '${project.version}'" } } }