Skip to content

Commit eedaeba

Browse files
Read version from resource instead of hard-coding it (#220)
* 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
1 parent ebadb18 commit eedaeba

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

.bazelrc

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ run -c opt --show_loading_progress=false --show_progress=false --ui_event_filter
22
run:verbose -c dbg --show_loading_progress=true --show_progress=true --ui_event_filters=info,error,debug
33
# https://github.com/mockito/mockito/issues/1879
44
test --sandbox_tmpfs_path=/tmp
5+
6+
# To allow stamping git tag for version.
7+
build --workspace_status_command=$(pwd)/workspace_status.sh

cli/BUILD

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ config_setting(
88
},
99
)
1010

11+
genrule(
12+
name = "version_file",
13+
srcs = [],
14+
outs = ["version"],
15+
cmd_bash = """
16+
version_tag=$$(grep ^STABLE_GIT_TAG bazel-out/stable-status.txt | cut -d' ' -f2); \
17+
printf '%s' $$version_tag > $@;
18+
""",
19+
stamp = 1,
20+
)
21+
1122
java_binary(
1223
name = "bazel-diff",
1324
jvm_flags = select({
@@ -22,6 +33,7 @@ java_binary(
2233
kt_jvm_library(
2334
name = "cli-lib",
2435
srcs = glob(["src/main/kotlin/**/*.kt"]),
36+
resources = [":version_file"],
2537
deps = [
2638
"@bazel_diff_maven//:com_google_code_gson_gson",
2739
"@bazel_diff_maven//:com_google_guava_guava",
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.bazel_diff.cli
22

33
import picocli.CommandLine.IVersionProvider
4+
import java.io.BufferedReader
5+
import java.io.InputStreamReader
46

57
class VersionProvider : IVersionProvider {
68
override fun getVersion(): Array<String> {
7-
return arrayOf("7.0.0")
9+
val classLoader = this::class.java.classLoader
10+
val inputStream = classLoader.getResourceAsStream("cli/version")
11+
?: throw IllegalArgumentException("unknown version as version file not found in resources")
12+
13+
val version = BufferedReader(InputStreamReader(inputStream)).use { it.readText().trim() }
14+
return arrayOf(version)
815
}
916
}

workspace_status.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
# Get the current Git tag or default to the commit hash
4+
echo "STABLE_GIT_TAG $(git describe --tags --abbrev=0 2>/dev/null || git rev-parse HEAD)"

0 commit comments

Comments
 (0)