Skip to content

Instantly share code, notes, and snippets.

@ijo42
Created November 27, 2020 12:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijo42/5497480a99a7c8003d9862db5432625c to your computer and use it in GitHub Desktop.
Save ijo42/5497480a99a7c8003d9862db5432625c to your computer and use it in GitHub Desktop.
Build-versionising script
task release(){}
task feature(){}
task pre(){}
release.dependsOn build
feature.dependsOn build
pre.dependsOn build
if (gradle.startParameter.taskNames.any {it.contains('release') }) {
version = "${getVersion()}+${bumpBuildNumber()}b-RELEASE"
bumpMajor()
}else if (gradle.startParameter.taskNames.any {it.contains('feature') }) {
version = "${getVersion()}+${bumpBuildNumber()}b-${getGitHash()}"
bumpMinor()
}else if (gradle.startParameter.taskNames.any {it.contains('pre') }) {
version = "${getVersion()}+${bumpBuildNumber()}b-${getGitHash()}"
bumpPath()
}else if (gradle.startParameter.taskNames.any {it.contains('build') }){
version = "${getVersion()}+${bumpBuildNumber()}b-${getGitHash()}-SNAPSHOT"
}else{
version = "${getVersion()}+${getBuildNumber()}b-${getGitHash()}-SNAPSHOT"
}
static def getGitHash() {
def process = 'git rev-parse --short HEAD'.execute()
process.waitFor()
return (process.exitValue() ? 'unknown' : process.text.trim())
}
def bumpBuildNumber() {
def propsFile = file('./buildnumber')
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
def nextBuild = props.buildNumber
if (nextBuild == null) {
nextBuild = 0
} else {
nextBuild = nextBuild.toInteger() + 1
}
props.buildNumber = nextBuild.toString()
props.store(propsFile.newWriter(), null)
return nextBuild
} else {
throw new GradleException('Cannot read gradle.properties')
}
}
def getBuildNumber() {
def propsFile = file('./buildnumber')
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
return props.buildNumber
} else {
throw new GradleException('Cannot read gradle.properties')
}
}
def bumpMajor() {
def propsFile = file('./buildnumber')
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
def nextBuild = props.major
if (nextBuild == null) {
nextBuild = 0
} else {
nextBuild = nextBuild.toInteger() + 1
props.minor = "0"
props.path = "0"
}
props.major = nextBuild.toString()
props.store(propsFile.newWriter(), null)
} else {
throw new GradleException('Cannot read ./buildnumber')
}
}
def bumpMinor() {
def propsFile = file('./buildnumber')
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
def nextBuild = props.minor
if (nextBuild == null) {
nextBuild = 0
} else {
nextBuild = nextBuild.toInteger() + 1
props.path = "0"
}
props.minor = nextBuild.toString()
props.store(propsFile.newWriter(), null)
} else {
throw new GradleException('Cannot read ./buildnumber')
}
}
def bumpPath() {
def propsFile = file('./buildnumber')
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
def nextBuild = props.path
if (nextBuild == null) {
nextBuild = 1
} else {
nextBuild = nextBuild.toInteger() + 1
}
props.path = nextBuild.toString()
props.store(propsFile.newWriter(), null)
} else {
throw new GradleException('Cannot read ./buildnumber')
}
}
def getVersion() {
def propsFile = file('./buildnumber')
if(!propsFile.exists())
propsFile.createNewFile()
if (propsFile.canRead() && propsFile.canWrite()) {
Properties props = new Properties()
props.load(new FileInputStream(propsFile))
def major = props.major
def minor = props.minor
def path = props.path
if (major == null) {
major = 0
props.major = major.toString()
props.store(propsFile.newWriter(), null)
}
if (minor == null) {
minor = 0
props.minor = minor.toString()
props.store(propsFile.newWriter(), null)
}
if (path == null) {
path = 1
props.path = path.toString()
props.store(propsFile.newWriter(), null)
}
return "${major}.${minor}.${path}"
} else {
throw new GradleException('Cannot read ./buildnumber')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment