Skip to content

Commit 41f4fee

Browse files
committed
Implement stamping for passing down version info
1 parent ca275c6 commit 41f4fee

File tree

7 files changed

+115
-3
lines changed

7 files changed

+115
-3
lines changed

.bazelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ query --noexperimental_check_output_files
1515
build --nobuild_runfile_links
1616
coverage --build_runfile_links
1717

18+
## Stamping for release builds
19+
# Enable with --config=release
20+
build:release --stamp --workspace_status_command=./bazel_scripts/stamp.sh
21+
1822
## JS
1923

2024
# passes an argument `--skipLibCheck` to *every* spawn of tsc

app/electron-client/BUILD.bazel

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
2+
load("@aspect_rules_js//js:defs.bzl", "js_binary")
23
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
34
load("@aspect_rules_ts//ts:defs.bzl", "ts_config", "ts_project")
45
load("@npm//:defs.bzl", "npm_link_all_packages", "npm_link_targets")
56
load("@npm//app/electron-client:electron-builder/package_json.bzl", electron_builder_bin = "bin")
7+
load("//app/electron-client:bazel/buildInfo.bzl", "generate_build_info")
68

79
npm_link_all_packages(name = "node_modules")
810

@@ -39,9 +41,9 @@ npm_package(
3941
esbuild(
4042
name = "bundle",
4143
srcs = glob(["src/**/*.ts"]) + [
42-
"buildInfo.ts",
4344
"fileAssociations.ts",
4445
"paths.ts",
46+
":build_info",
4547
],
4648
bundle = True,
4749
# Some configuration options (like plugins) cannot be defined in Bazel rule, so we use a config file.
@@ -97,3 +99,16 @@ electron_builder_bin.electron_builder(
9799
out_dirs = ["ide-dist"],
98100
visibility = ["//visibility:public"],
99101
)
102+
103+
js_binary(
104+
name = "create_build_info_script",
105+
chdir = package_name(),
106+
entry_point = "create-build-info.mjs",
107+
visibility = ["//visibility:public"],
108+
)
109+
110+
generate_build_info(
111+
name = "build_info",
112+
out = "buildInfo.ts",
113+
visibility = ["//visibility:public"],
114+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Runs a script with a path to the stable status file as an argument.
3+
"""
4+
5+
load("@aspect_bazel_lib//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp")
6+
7+
def _generate_build_info_impl(ctx):
8+
args = ctx.actions.args()
9+
args.add(ctx.outputs.out.path)
10+
inputs = []
11+
outputs = [ctx.outputs.out]
12+
stamp = maybe_stamp(ctx)
13+
if stamp:
14+
args.add(stamp.stable_status_file.path)
15+
inputs = [stamp.stable_status_file]
16+
17+
ctx.actions.run(
18+
inputs = inputs,
19+
outputs = outputs,
20+
arguments = [args],
21+
env = {
22+
"BAZEL_BINDIR": ctx.bin_dir.path,
23+
},
24+
executable = ctx.executable._stamp_exec,
25+
)
26+
return [DefaultInfo(files = depset(outputs))]
27+
28+
generate_build_info = rule(
29+
implementation = _generate_build_info_impl,
30+
attrs = dict({
31+
"out": attr.output(mandatory = True),
32+
"_stamp_exec": attr.label(executable = True, default = Label("//app/electron-client:create_build_info_script"), cfg = "exec"),
33+
}, **STAMP_ATTRS),
34+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import fs from 'fs'
2+
import process from 'process'
3+
4+
process.chdir(process.env.JS_BINARY__EXECROOT)
5+
6+
const outputPath = process.argv[2]
7+
if (!outputPath) {
8+
console.error('Output path is required')
9+
process.exit(1)
10+
}
11+
const statusFilePath = process.argv[3]
12+
if (!statusFilePath) {
13+
// This is normal, stamping is disabled. Write a dummy build info file.
14+
fs.writeFileSync(
15+
outputPath,
16+
`export default {
17+
version: '0.0.0-dev',
18+
commit: '<snapshot>',
19+
}`,
20+
{
21+
create: true,
22+
},
23+
)
24+
process.exit(0)
25+
}
26+
const statusFile = fs.readFileSync(statusFilePath, 'utf8')
27+
28+
// The file is basically a list of space-separated key-value pairs.
29+
const lines = statusFile.split('\n')
30+
const getValue = (prefix) => lines.find((line) => line.startsWith(prefix)).split(' ')[1]
31+
const ideVersion = getValue('STABLE_IDE_VERSION')
32+
const ideCommitHash = getValue('STABLE_IDE_COMMIT_HASH')
33+
34+
fs.writeFileSync(
35+
outputPath,
36+
`export default {
37+
version: '${ideVersion}',
38+
commit: '${ideCommitHash}',
39+
}`,
40+
)

app/electron-client/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"@/*": ["./src/*"]
88
}
99
},
10-
"include": ["src/*.ts", "tasks/*.ts", "tests/*.ts", "src/config.json", "*.ts", "esbuild.config.mjs", "electron-builder-config.cjs"],
10+
"include": ["src/*.ts", "tasks/*.ts", "tests/*.ts", "src/config.json", "*.ts", "esbuild.config.mjs", "electron-builder-config.cjs", "create-build-info.mjs"],
1111
}

bazel_scripts/stamp.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -eou pipefail
4+
5+
if [[ -n "${GITHUB_SHA:-}" ]]; then
6+
IDE_COMMIT_HASH="$GITHUB_SHA"
7+
else
8+
IDE_COMMIT_HASH="$(git rev-parse --verify HEAD)"
9+
fi
10+
11+
if [[ -n "${ENSO_VERSION:-}" ]]; then
12+
IDE_VERSION="$ENSO_VERSION"
13+
else
14+
IDE_VERSION="$(date +%Y.%-m.%-d)-dev"
15+
fi
16+
17+
18+
echo "STABLE_IDE_VERSION $IDE_VERSION"
19+
echo "STABLE_IDE_COMMIT_HASH $IDE_COMMIT_HASH"

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ const config = [
190190
'**/generated',
191191
'**/*.json',
192192
'app/rust-ffi/pkg/',
193-
'app/electron-client/pre-electron-builder.cjs',
194193
'app/electron-client/electron-builder-config.cjs',
194+
'app/electron-client/create-build-info.cjs',
195195
],
196196
},
197197
{

0 commit comments

Comments
 (0)