Skip to content

Commit

Permalink
Allow overriding the build suffix
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zachbr committed Nov 13, 2024
1 parent 1b9b753 commit 0d0a44a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
25 changes: 18 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/zachbr/dis4irc/Dis4IRC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Dis4IRC(args: Array<String>) {
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")

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

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
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/io/zachbr/dis4irc/util/Versioning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()) {
Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion src/main/resources/dis4irc-versioning.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Name: ${projectName}
Version: ${projectVersion}
Git-Hash: ${projectGitHash}
Suffix: ${projectSuffix}
Source-Repo: ${projectSourceRepo}

0 comments on commit 0d0a44a

Please sign in to comment.