Skip to content

Commit

Permalink
chore: Get version name from latest Git tag
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs committed Nov 1, 2022
1 parent d1c2576 commit bf86e5d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {
minSdk = Android.minSdk
targetSdk = Android.targetSdk
versionCode = 238
versionName = "17"
versionName = latestTagName()

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private fun Content(
bottom = 8.dp,
end = 8.dp,
),
text = BuildConfig.VERSION_NAME,
text = "v${BuildConfig.VERSION_NAME}",
style = MaterialTheme.typography.bodySmall,
)
}
Expand Down
33 changes: 33 additions & 0 deletions buildSrc/src/main/kotlin/Git.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Léon - The URL Cleaner
* Copyright (C) 2022 Sven Jacobs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

fun latestTagName(): String {
val tag = execute("git", "describe", "--tags", "--abbrev=0", "--match", "v*")
if (tag.isBlank()) throw IllegalArgumentException("Could not determine tag")
return tag.substring(1)
}

private fun execute(vararg cmd: String): String {
var output: String
val process = ProcessBuilder(*cmd).start()
process.inputStream.reader(Charsets.UTF_8).use {
output = it.readText()
}
process.waitFor()
return output.trim()
}

0 comments on commit bf86e5d

Please sign in to comment.