app version based on git tags

This commit is contained in:
Ishan09811 2024-02-07 22:05:53 +05:30 committed by GitHub
parent b256c89e23
commit 1607d347d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,5 @@
import java.lang.ProcessBuilder.Redirect
plugins {
id("com.android.application")
}
@ -10,8 +12,8 @@ android {
applicationId = "com.panda3ds.pandroid"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
versionCode = getGitVersionCode()
versionName = getGitVersionName()
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
@ -54,3 +56,91 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.code.gson:gson:2.10.1")
}
/**
* Returns the version name based on the current git state
* If HEAD is a tag, the tag name is used as the version name
* e.g. `1.0.0`
* If HEAD is not a tag, the tag name, the branch name and the short commit hash are used
* e.g. `1.0.0-master-ab00cd11`
*/
fun getGitVersionName(): String {
var versionName = "0.0.0"
try {
val process = ProcessBuilder("git", "describe", "--abbrev=0")
.directory(project.rootDir)
.redirectOutput(Redirect.PIPE)
.start()
val tag = process.inputStream.bufferedReader().readText().trim()
if (!tag.isEmpty())
versionName = tag
versionName += "-" + getGitBranch() + "-" + getGitShortHash()
} catch (e: Exception) {
logger.quiet("$e: defaulting to dummy version number $versionName")
}
logger.quiet("Version name: $versionName")
return versionName
}
/**
* Returns the number of commits until the last tag
*/
fun getGitVersionCode(): Int {
var versionCode = 1
try {
val process = ProcessBuilder("git", "rev-list", "--first-parent", "--count", "--tags")
.directory(project.rootDir)
.redirectOutput(Redirect.PIPE)
.start()
val output = process.inputStream.bufferedReader().readText().toInt()
versionCode = maxOf(output, versionCode)
} catch (e: Exception) {
logger.error("$e: defaulting to dummy version code $versionCode")
}
logger.quiet("Version code: $versionCode")
return versionCode
}
/**
* Returns the short commit hash
*/
fun getGitShortHash(): String {
var gitHash = "0"
try {
val process = ProcessBuilder("git", "rev-parse", "--short", "HEAD")
.directory(project.rootDir)
.redirectOutput(Redirect.PIPE)
.start()
gitHash = process.inputStream.bufferedReader().readText().trim()
} catch (e: Exception) {
logger.error("$e: defaulting to dummy build hash $gitHash")
}
return gitHash
}
/**
* Returns the current branch name
*/
fun getGitBranch(): String {
var branch = "unk"
try {
val process = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD")
.directory(project.rootDir)
.redirectOutput(Redirect.PIPE)
.start()
branch = process.inputStream.bufferedReader().readText().trim()
} catch (e: Exception) {
logger.error("$e: defaulting to dummy branch $branch")
}
return branch
}