Created
March 7, 2017 15:37
-
-
Save xrigau/6389b64315e24018cf6ff614006246a5 to your computer and use it in GitHub Desktop.
Version using gradle properties file
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 versionFile = project.file("version.properties") | |
Version version = Version.read(versionFile) | |
ext { | |
versionizerVersionCode = version.code | |
versionizerVersionName = version.name | |
} | |
task incrementVersionForRelease << { | |
group = 'release' | |
description = 'Loads the information about the last release and increases the version' | |
Version.increment(versionFile) | |
} | |
class Version { | |
private static final String KEY_CODE = 'VERSION_CODE' | |
private static final String KEY_NAME = 'VERSION_NAME' | |
private static final String VERSION_NAME_DATE_FORMAT = 'yyyy.MM.dd' | |
int code | |
String name | |
static void increment(File versionFile) { | |
read(versionFile).increment().store(versionFile) | |
} | |
static Version read(File versionFile) { | |
Properties properties = new Properties() | |
properties.load(versionFile.newDataInputStream()) | |
int versionCode = properties.getProperty(KEY_CODE) as int | |
String versionName = properties.getProperty(KEY_NAME) | |
return new Version(code: versionCode, name: versionName) | |
} | |
void store(File versionFile) { | |
Properties properties = new Properties() | |
properties.setProperty(KEY_CODE, "$code") | |
properties.setProperty(KEY_NAME, name) | |
properties.store(versionFile.newOutputStream(), null) | |
} | |
Version increment() { | |
new Version(code: code + 1, name: new Date().format(VERSION_NAME_DATE_FORMAT)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment