Skip to content

Commit

Permalink
Read version from resource instead of hard-coding it (#220)
Browse files Browse the repository at this point in the history
* Read version from resource instead of hard-coding it

* Moving to stamping to extract the version from tag or default to commit hash

* Cleaning up workspace_status.sh
  • Loading branch information
naveenOnarayanan authored Jul 4, 2024
1 parent ebadb18 commit eedaeba
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ run -c opt --show_loading_progress=false --show_progress=false --ui_event_filter
run:verbose -c dbg --show_loading_progress=true --show_progress=true --ui_event_filters=info,error,debug
# https://github.com/mockito/mockito/issues/1879
test --sandbox_tmpfs_path=/tmp

# To allow stamping git tag for version.
build --workspace_status_command=$(pwd)/workspace_status.sh
12 changes: 12 additions & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ config_setting(
},
)

genrule(
name = "version_file",
srcs = [],
outs = ["version"],
cmd_bash = """
version_tag=$$(grep ^STABLE_GIT_TAG bazel-out/stable-status.txt | cut -d' ' -f2); \
printf '%s' $$version_tag > $@;
""",
stamp = 1,
)

java_binary(
name = "bazel-diff",
jvm_flags = select({
Expand All @@ -22,6 +33,7 @@ java_binary(
kt_jvm_library(
name = "cli-lib",
srcs = glob(["src/main/kotlin/**/*.kt"]),
resources = [":version_file"],
deps = [
"@bazel_diff_maven//:com_google_code_gson_gson",
"@bazel_diff_maven//:com_google_guava_guava",
Expand Down
9 changes: 8 additions & 1 deletion cli/src/main/kotlin/com/bazel_diff/cli/VersionProvider.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.bazel_diff.cli

import picocli.CommandLine.IVersionProvider
import java.io.BufferedReader
import java.io.InputStreamReader

class VersionProvider : IVersionProvider {
override fun getVersion(): Array<String> {
return arrayOf("7.0.0")
val classLoader = this::class.java.classLoader
val inputStream = classLoader.getResourceAsStream("cli/version")
?: throw IllegalArgumentException("unknown version as version file not found in resources")

val version = BufferedReader(InputStreamReader(inputStream)).use { it.readText().trim() }
return arrayOf(version)
}
}
4 changes: 4 additions & 0 deletions workspace_status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# Get the current Git tag or default to the commit hash
echo "STABLE_GIT_TAG $(git describe --tags --abbrev=0 2>/dev/null || git rev-parse HEAD)"

0 comments on commit eedaeba

Please sign in to comment.