Skip to content

Instantly share code, notes, and snippets.

@pfieffer
Forked from IlyaEremin/app_version.gradle
Created July 5, 2023 06:35
Show Gist options
  • Save pfieffer/d03442f9ab3c1104dc2863853c27709a to your computer and use it in GitHub Desktop.
Save pfieffer/d03442f9ab3c1104dc2863853c27709a to your computer and use it in GitHub Desktop.
Npm version for gradle
def getVersionName = { getVersionProps()['appVersionName'] }
def getVersionProps() {
def versionPropsFile = file('gradle.properties')
if (!versionPropsFile.exists()) {
versionPropsFile.createNewFile()
}
def versionProps = new Properties()
versionProps.load(new FileInputStream(versionPropsFile))
return versionProps
}
def getVersionNamePatch = { (getVersionName() =~ /\d+/)[2].toInteger() }
def getVersionNameMinor = { (getVersionName() =~ /\d+/)[1].toInteger() }
def getVersionNameMajor = { (getVersionName() =~ /\d+/)[0].toInteger() }
private void commitAndSetTag(versionName) {
Process addChanges = ['git', 'add', 'app/gradle.properties'].execute(null, project.rootDir)
addChanges.waitForProcessOutput(System.out, System.err)
Process createCommit = ['git', 'commit', "-m Release ${versionName}"].execute(null, project.rootDir)
createCommit.waitForProcessOutput(System.out, System.err)
Process createTag = ['git', 'tag', "v${versionName}".toString()].execute(null, project.rootDir)
createTag.waitForProcessOutput(System.out, System.err)
}
private def getAppVersionCode() { getVersionProps()['appVersionCode'].toInteger() }
private void save(major, minor, patch, versionCode) {
save("${major}.${minor}.${patch}".toString(), versionCode.toString())
}
private void save(versionName, versionCode) {
def versionProps = getVersionProps()
versionProps['appVersionName'] = versionName
versionProps['appVersionCode'] = versionCode
versionProps.store(file('gradle.properties').newWriter(), null)
commitAndSetTag(versionName)
}
task bumperInit() {
group = 'bumper'
doLast {
def versionName = project.hasProperty('bumperVersionName') ? bumperVersionName : '1.0.0'
def versionCode = project.hasProperty('bumperVersionCode') ? bumperVersionCode : '1'
save(versionName, versionCode)
}
}
task bumperVersionPatch () {
group = 'bumper'
doLast {
save(getVersionNameMajor(), getVersionNameMinor(), getVersionNamePatch() + 1, getAppVersionCode() + 1)
}
}
task bumperVersionMinor () {
group = 'bumper'
doLast {
save(getVersionNameMajor(), getVersionNameMinor() + 1, 0, getAppVersionCode() + 1)
}
}
task bumperVersionMajor () {
group = 'bumper'
doLast {
save(getVersionNameMajor() + 1, 0, 0, getAppVersionCode() + 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment