diff --git a/build.gradle.kts b/build.gradle.kts index e031529..1aa3cf4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -71,10 +71,11 @@ tasks { } processResources { + inputs.property("suffix", project.findProperty("suffix") ?: "") // track suffix value as input expand( "projectName" to rootProject.name, "projectVersion" to version, - "projectGitHash" to getGitHash(), + "projectSuffix" to getSuffix(), "projectSourceRepo" to "https://github.com/zachbr/Dis4IRC" ) } @@ -94,11 +95,21 @@ license { } } -fun getGitHash(): String { - val stdout = ByteArrayOutputStream() // cannot be fully qualified, ¯\_(ツ)_/¯ - exec { - commandLine = listOf("git", "rev-parse", "--short", "HEAD") - standardOutput = stdout +fun getSuffix(): String { + // If suffix was specified at build-time, use that. + // ./gradlew build -P suffix="suffixValue15" + project.findProperty("suffix")?.toString()?.let { return it } + + // Fall back to git hash if suffix not set + return ByteArrayOutputStream().let { stdout -> + runCatching { + exec { + commandLine("git", "rev-parse", "--short", "HEAD") + standardOutput = stdout + errorOutput = ByteArrayOutputStream() + isIgnoreExitValue = true + } + stdout.toString().trim().takeIf { it.isNotEmpty() } + }.getOrNull() ?: "0" // fall back to 0 if git falls through } - return stdout.toString().trim() } diff --git a/src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt b/src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt index f85c230..f42925d 100644 --- a/src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt +++ b/src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt @@ -46,7 +46,7 @@ class Dis4IRC(args: Array) { init { parseArguments(args) - logger.info("Dis4IRC v${Versioning.version}-${Versioning.gitHash}") + logger.info("Dis4IRC v${Versioning.version}-${Versioning.suffix}") logger.info("Source available at ${Versioning.sourceRepo}") logger.info("Available under the MIT License") @@ -190,7 +190,7 @@ class Dis4IRC(args: Array) { private fun printVersionInfo(minimal: Boolean = false) { // can't use logger, this has to be bare bones without prefixes or timestamps - println("Dis4IRC v${Versioning.version}-${Versioning.gitHash}") + println("Dis4IRC v${Versioning.version}-${Versioning.suffix}") if (minimal) { return } diff --git a/src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt b/src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt index cd8f030..806cedc 100644 --- a/src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt +++ b/src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt @@ -22,9 +22,9 @@ object Versioning { val version: String /** - * Gets the top of tree git hash this version was built against + * Gets the build suffix, this may be the git hash or a value specified at build time */ - val gitHash: String + val suffix: String /** * Gets the source repo of this project @@ -34,7 +34,7 @@ object Versioning { init { val resources = this.javaClass.classLoader.getResources(JAR_PATH_TO_VERSIONING_INFO) var verOut = "Unknown version" - var gitHashOut = "Unknown Git Commit" + var suffixOut = "Unknown suffix" var repoOut = "Unknown source repo" if (resources.hasMoreElements()) { @@ -45,14 +45,14 @@ object Versioning { } verOut = getValue("Version") - gitHashOut = getValue("Git-Hash") + suffixOut = getValue("Suffix") repoOut = getValue("Source-Repo") } } } version = verOut - gitHash = gitHashOut + suffix = suffixOut sourceRepo = repoOut } } diff --git a/src/main/resources/dis4irc-versioning.txt b/src/main/resources/dis4irc-versioning.txt index 4a34727..f48c55f 100644 --- a/src/main/resources/dis4irc-versioning.txt +++ b/src/main/resources/dis4irc-versioning.txt @@ -1,4 +1,4 @@ Name: ${projectName} Version: ${projectVersion} -Git-Hash: ${projectGitHash} +Suffix: ${projectSuffix} Source-Repo: ${projectSourceRepo}