-
-
Save pfieffer/d03442f9ab3c1104dc2863853c27709a to your computer and use it in GitHub Desktop.
Npm version for gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
https://medium.com/@IlyaEremin/npm-version-for-gradle-android-9137a7dc273c