Skip to content

Commit 2cef20b

Browse files
committed
👷 Config x_defs in bazel
1 parent 9415103 commit 2cef20b

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

‎.bazelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
# Enable Bzlmod for every Bazel command
22
common --enable_bzlmod
3+
4+
# build, test, and run commands
5+
build --stamp --workspace_status_command=./scripts/bazel-status.sh
6+
test --stamp --workspace_status_command=./scripts/bazel-status.sh
7+
run --stamp --workspace_status_command=./scripts/bazel-status.sh

‎BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ go_binary(
3838
name = "yutu",
3939
embed = [":yutu_lib"],
4040
visibility = ["//visibility:public"],
41+
x_defs = {
42+
"Version": "{STABLE_VERSION}",
43+
"Commit": "{STABLE_GIT_SHORT_SHA}",
44+
"CommitDate": "{STABLE_GIT_COMMIT_DATE}",
45+
"Os": "{STABLE_OS}",
46+
"Arch": "{STABLE_ARCH}",
47+
},
4148
)
4249

4350
go_cross_binary(

‎MODULE.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ go_sdk.from_file(go_mod = "//:go.mod")
88

99
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
1010
go_deps.from_file(go_mod = "//:go.mod")
11+
12+
go_deps.config(
13+
go_env = {
14+
"CGO_ENABLED": "0",
15+
"GO111MODULE": "on",
16+
},
17+
)
18+
1119
use_repo(
1220
go_deps,
1321
"com_github_savioxavier_termlink",

‎cmd/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ go_library(
99
],
1010
importpath = "github.com/eat-pray-ai/yutu/cmd",
1111
visibility = ["//visibility:public"],
12+
x_defs = {
13+
"Version": "{STABLE_VERSION}",
14+
"Commit": "{STABLE_GIT_SHORT_SHA}",
15+
"CommitDate": "{STABLE_GIT_COMMIT_DATE}",
16+
"Os": "{STABLE_OS}",
17+
"Arch": "{STABLE_ARCH}",
18+
},
1219
deps = [
1320
"//pkg/auth",
1421
"@com_github_savioxavier_termlink//:termlink",

‎scripts/bazel-status.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
GIT_SHORT_SHA=$(git rev-parse --short HEAD)
4+
echo STABLE_GIT_SHORT_SHA "$GIT_SHORT_SHA"
5+
echo STABLE_GIT_COMMIT_DATE "$(git log -1 --format=%cd --date=iso)"
6+
7+
GIT_TAG=$(git describe --tags --abbrev=0)
8+
GIT_TAG_DIRTY=$(git describe --tags --dirty --abbrev=0)
9+
# if the tag is dirty, we need to append the short SHA
10+
if [[ "$GIT_TAG_DIRTY" != "$GIT_TAG" ]]; then
11+
echo STABLE_VERSION "$GIT_TAG_DIRTY-$GIT_SHORT_SHA"
12+
else
13+
echo STABLE_VERSION "$GIT_TAG"
14+
fi
15+
16+
echo STABLE_OS "$(uname -s | tr '[:upper:]' '[:lower:]')"
17+
echo STABLE_ARCH "$(uname -m | sed 's/x86_64/amd64/' | tr '[:upper:]' '[:lower:]')"

0 commit comments

Comments
 (0)