Skip to content

Commit 0d0a44a

Browse files
committed
Allow overriding the build suffix
Prior to this commit, the build suffix (displayed to users at startup) was always the top of tree git short hash. This is nice but it falls down if someone is pulling a release from a tarball, zip file, or other archive that does not include the .git folder or if the builder does not have git installed. As of this commit, the project will now build even in those cases (displaying a "0" suffix). This change also allows the builder to specify the suffix for packaging or other purposes via the "suffix" property. E.g. `./gradlew build -P suffix="build35"` I have no immediate plans to change my use of the git hash in releases on GitHub, but hopefully this allows others additional flexibility. Fixes #82
1 parent 1b9b753 commit 0d0a44a

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

build.gradle.kts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ tasks {
7171
}
7272

7373
processResources {
74+
inputs.property("suffix", project.findProperty("suffix") ?: "") // track suffix value as input
7475
expand(
7576
"projectName" to rootProject.name,
7677
"projectVersion" to version,
77-
"projectGitHash" to getGitHash(),
78+
"projectSuffix" to getSuffix(),
7879
"projectSourceRepo" to "https://github.com/zachbr/Dis4IRC"
7980
)
8081
}
@@ -94,11 +95,21 @@ license {
9495
}
9596
}
9697

97-
fun getGitHash(): String {
98-
val stdout = ByteArrayOutputStream() // cannot be fully qualified, ¯\_(ツ)_/¯
99-
exec {
100-
commandLine = listOf("git", "rev-parse", "--short", "HEAD")
101-
standardOutput = stdout
98+
fun getSuffix(): String {
99+
// If suffix was specified at build-time, use that.
100+
// ./gradlew build -P suffix="suffixValue15"
101+
project.findProperty("suffix")?.toString()?.let { return it }
102+
103+
// Fall back to git hash if suffix not set
104+
return ByteArrayOutputStream().let { stdout ->
105+
runCatching {
106+
exec {
107+
commandLine("git", "rev-parse", "--short", "HEAD")
108+
standardOutput = stdout
109+
errorOutput = ByteArrayOutputStream()
110+
isIgnoreExitValue = true
111+
}
112+
stdout.toString().trim().takeIf { it.isNotEmpty() }
113+
}.getOrNull() ?: "0" // fall back to 0 if git falls through
102114
}
103-
return stdout.toString().trim()
104115
}

src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Dis4IRC(args: Array<String>) {
4646
init {
4747
parseArguments(args)
4848

49-
logger.info("Dis4IRC v${Versioning.version}-${Versioning.gitHash}")
49+
logger.info("Dis4IRC v${Versioning.version}-${Versioning.suffix}")
5050
logger.info("Source available at ${Versioning.sourceRepo}")
5151
logger.info("Available under the MIT License")
5252

@@ -190,7 +190,7 @@ class Dis4IRC(args: Array<String>) {
190190

191191
private fun printVersionInfo(minimal: Boolean = false) {
192192
// can't use logger, this has to be bare bones without prefixes or timestamps
193-
println("Dis4IRC v${Versioning.version}-${Versioning.gitHash}")
193+
println("Dis4IRC v${Versioning.version}-${Versioning.suffix}")
194194
if (minimal) {
195195
return
196196
}

src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ object Versioning {
2222
val version: String
2323

2424
/**
25-
* Gets the top of tree git hash this version was built against
25+
* Gets the build suffix, this may be the git hash or a value specified at build time
2626
*/
27-
val gitHash: String
27+
val suffix: String
2828

2929
/**
3030
* Gets the source repo of this project
@@ -34,7 +34,7 @@ object Versioning {
3434
init {
3535
val resources = this.javaClass.classLoader.getResources(JAR_PATH_TO_VERSIONING_INFO)
3636
var verOut = "Unknown version"
37-
var gitHashOut = "Unknown Git Commit"
37+
var suffixOut = "Unknown suffix"
3838
var repoOut = "Unknown source repo"
3939

4040
if (resources.hasMoreElements()) {
@@ -45,14 +45,14 @@ object Versioning {
4545
}
4646

4747
verOut = getValue("Version")
48-
gitHashOut = getValue("Git-Hash")
48+
suffixOut = getValue("Suffix")
4949
repoOut = getValue("Source-Repo")
5050
}
5151
}
5252
}
5353

5454
version = verOut
55-
gitHash = gitHashOut
55+
suffix = suffixOut
5656
sourceRepo = repoOut
5757
}
5858
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Name: ${projectName}
22
Version: ${projectVersion}
3-
Git-Hash: ${projectGitHash}
3+
Suffix: ${projectSuffix}
44
Source-Repo: ${projectSourceRepo}

0 commit comments

Comments
 (0)