chore: remove expand_template in exchange for bazel-lib
diff --git a/MODULE.bazel b/MODULE.bazel index 993df29..baf8bde 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -6,7 +6,7 @@ compatibility_level = 1, ) -bazel_dep(name = "aspect_bazel_lib", version = "1.31.2") +bazel_dep(name = "aspect_bazel_lib", version = "1.32.0") bazel_dep(name = "bazel_skylib", version = "1.4.1") bazel_dep(name = "rules_nodejs", version = "5.8.2") bazel_dep(name = "platforms", version = "0.0.4")
diff --git a/js/private/BUILD.bazel b/js/private/BUILD.bazel index f427f79..79b5efb 100644 --- a/js/private/BUILD.bazel +++ b/js/private/BUILD.bazel
@@ -133,12 +133,6 @@ ) bzl_library( - name = "expand_template", - srcs = ["expand_template.bzl"], - visibility = ["//visibility:public"], -) - -bzl_library( name = "js_image_layer", srcs = ["js_image_layer.bzl"], visibility = [ @@ -151,14 +145,6 @@ ], ) -js_binary( - name = "expand_template_binary", - entry_point = "expand_template.js", - # meant to run out of the execroot - env = {"BAZEL_BINDIR": "."}, - visibility = ["//visibility:public"], -) - copy_to_bin( name = "js_devserver_entrypoint", srcs = ["js_run_devserver.mjs"],
diff --git a/js/private/expand_template.bzl b/js/private/expand_template.bzl deleted file mode 100644 index 2a7ffc9..0000000 --- a/js/private/expand_template.bzl +++ /dev/null
@@ -1,119 +0,0 @@ -"expand_template rule" - -load("@aspect_bazel_lib//lib:expand_make_vars.bzl", _expand_locations = "expand_locations", _expand_variables = "expand_variables") -load("@aspect_bazel_lib//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp") -load("@bazel_skylib//lib:dicts.bzl", "dicts") - -def _expand_substitutions(ctx, substitutions): - result = {} - for k, v in substitutions.items(): - result[k] = " ".join([ - _expand_variables(ctx, e, outs = [ctx.outputs.out], attribute_name = "substitutions") - for e in _expand_locations(ctx, v, ctx.attr.data).split(" ") - ]) - return result - -def _impl(ctx): - substitutions = _expand_substitutions(ctx, ctx.attr.substitutions) - - stamp = maybe_stamp(ctx) - if stamp: - substitutions = dicts.add(substitutions, _expand_substitutions(ctx, ctx.attr.stamp_substitutions)) - - inputs = [ - ctx.file.template, - stamp.volatile_status_file, - stamp.stable_status_file, - ] - - args = ctx.actions.args() - args.add(ctx.file.template.path) - args.add(ctx.outputs.out.path) - args.add(stamp.volatile_status_file) - args.add(stamp.stable_status_file) - args.add(substitutions) - args.add(ctx.attr.is_executable) - args.use_param_file("%s", use_always = True) - - ctx.actions.run( - arguments = [args], - outputs = [ctx.outputs.out], - inputs = inputs, - executable = ctx.executable._expand_template_binary, - ) - else: - ctx.actions.expand_template( - template = ctx.file.template, - output = ctx.outputs.out, - substitutions = substitutions, - is_executable = ctx.attr.is_executable, - ) - - all_outs = [ctx.outputs.out] - runfiles = ctx.runfiles(files = all_outs) - return [DefaultInfo(files = depset(all_outs), runfiles = runfiles)] - -expand_template_lib = struct( - doc = """Template expansion with stamp var support. - -This performs a simple search over the template file for the keys in substitutions, -and replaces them with the corresponding values. - -Values may also use location templates as documented in -[expand_locations](https://github.com/aspect-build/bazel-lib/blob/main/docs/expand_make_vars.md#expand_locations) -as well as [configuration variables](https://docs.bazel.build/versions/main/skylark/lib/ctx.html#var) -such as `$(BINDIR)`, `$(TARGET_CPU)`, and `$(COMPILATION_MODE)` as documented in -[expand_variables](https://github.com/aspect-build/bazel-lib/blob/main/docs/expand_make_vars.md#expand_variables). - -If stamp attribute is True, stamp variable substitutions are supported with {{STAMP_VAR}} tokens. -""", - implementation = _impl, - attrs = dict({ - "data": attr.label_list( - doc = "List of targets for additional lookup information.", - allow_files = True, - ), - "is_executable": attr.bool( - doc = "Whether to mark the output file as executable.", - ), - "out": attr.output( - doc = "Where to write the expanded file.", - mandatory = True, - ), - "stamp_substitutions": attr.string_dict( - doc = """Mapping of strings to substitutions. - - There are overlayed on top of substitutions when stamping is enabled - for the target. - - Substitutions can contain $(execpath :target) and $(rootpath :target) - expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when - stamping is enabled for the target. - """, - ), - "substitutions": attr.string_dict( - doc = """Mapping of strings to substitutions. - - Substitutions can contain $(execpath :target) and $(rootpath :target) - expansions, $(MAKEVAR) expansions and {{STAMP_VAR}} expansions when - stamping is enabled for the target. - """, - ), - "template": attr.label( - doc = "The template file to expand.", - mandatory = True, - allow_single_file = True, - ), - "_expand_template_binary": attr.label( - executable = True, - default = Label("//js/private:expand_template_binary"), - cfg = "exec", - ), - }, **STAMP_ATTRS), -) - -expand_template = rule( - doc = expand_template_lib.doc, - implementation = expand_template_lib.implementation, - attrs = expand_template_lib.attrs, -)
diff --git a/js/private/expand_template.js b/js/private/expand_template.js deleted file mode 100644 index 3628c13..0000000 --- a/js/private/expand_template.js +++ /dev/null
@@ -1,89 +0,0 @@ -const fs = require('fs') - -/** - * The status files are expected to look like - * BUILD_SCM_HASH 83c699db39cfd74526cdf9bebb75aa6f122908bb - * BUILD_SCM_LOCAL_CHANGES true - * STABLE_BUILD_SCM_VERSION 6.0.0-beta.6+12.sha-83c699d.with-local-changes - * BUILD_TIMESTAMP 1520021990506 - * - * Parsing regex is created based on Bazel documentation describing the status file schema: - * The key names can be anything but they may only use upper case letters and underscores. The - * first space after the key name separates it from the value. The value is the rest of the line - * (including additional whitespace). - */ -function _parseStatusFile(statusFilePath) { - const results = {} - const statusFile = fs.readFileSync(statusFilePath, { encoding: 'utf-8' }) - for (const match of `\n${statusFile}`.matchAll(/^([^\s]+)\s+(.*)/gm)) { - // Lines which go unmatched define an index value of `0` and should be skipped. - if (match.index === 0) { - continue - } - results[match[1]] = match[2] - } - return results -} - -function _unquoteArgs(s) { - return s.replace(/^'(.*)'$/, '$1') -} - -function _replaceAll(value, token, replacement) { - // String.prototype.replaceAll was only added in Node.js 5; polyfill - // if it is not available - if (value.replaceAll) { - return value.replaceAll(token, replacement) - } else { - while (value.indexOf(token) != -1) { - value = value.replace(token, replacement) - } - return value - } -} - -function main(args) { - args = fs - .readFileSync(args[0], { encoding: 'utf-8' }) - .split('\n') - .map(_unquoteArgs) - const [ - template, - out, - volatileStatusFile, - stableStatusFile, - substitutionsJson, - isExecutable, - ] = args - - const substitutions = JSON.parse(substitutionsJson) - - const statuses = { - ..._parseStatusFile(volatileStatusFile), - ..._parseStatusFile(stableStatusFile), - } - - const statusSubstitutions = [] - for (const key of Object.keys(statuses)) { - statusSubstitutions.push([`{{${key}}}`, statuses[key]]) - } - - for (const key of Object.keys(substitutions)) { - let value = substitutions[key] - statusSubstitutions.forEach(([token, replacement]) => { - value = _replaceAll(value, token, replacement) - }) - substitutions[key] = value - } - - let content = fs.readFileSync(template, { encoding: 'utf-8' }) - for (const key of Object.keys(substitutions)) { - content = _replaceAll(content, key, substitutions[key]) - } - const mode = isExecutable ? 0o777 : 0x666 - fs.writeFileSync(out, content, { mode }) -} - -if (require.main === module) { - process.exitCode = main(process.argv.slice(2)) -}
diff --git a/js/private/test/expand_template/BUILD.bazel b/js/private/test/expand_template/BUILD.bazel deleted file mode 100644 index c68661b..0000000 --- a/js/private/test/expand_template/BUILD.bazel +++ /dev/null
@@ -1,60 +0,0 @@ -load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test") -load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains") -load("//js/private:expand_template.bzl", "expand_template") - -expand_template( - name = "a_tmpl_stamp", - out = "a_stamp", - data = ["a.tmpl"], - stamp = 1, - stamp_substitutions = { - "{{VERSION}}": "v{{BUILD_SCM_VERSION}}", - }, - substitutions = { - "{{TMPL_PATH}}": "$(rootpath a.tmpl)", - "{{VERSION}}": "v0.0.0", - "{{WORKSPACE}}": "$(WORKSPACE)", - }, - template = "a.tmpl", -) - -diff_test( - name = "a_stamp_test", - file1 = ":a_stamp", - file2 = select({ - "@aspect_bazel_lib//lib:bzlmod": "a_stamp_expected_bzlmod", - "//conditions:default": "a_stamp_expected", - }), -) - -expand_template( - name = "a_tmpl", - out = "a", - data = ["a.tmpl"], - stamp = 0, - stamp_substitutions = { - "{{VERSION}}": "v{{BUILD_SCM_VERSION}}", - }, - substitutions = { - "{{TMPL_PATH}}": "$(rootpath a.tmpl)", - "{{VERSION}}": "v0.0.0", - "{{WORKSPACE}}": "$(WORKSPACE)", - }, - template = "a.tmpl", -) - -diff_test( - name = "a_test", - file1 = ":a", - file2 = select({ - "@aspect_bazel_lib//lib:bzlmod": "a_expected_bzlmod", - "//conditions:default": "a_expected", - }), -) - -# This test requires that DefaultInfo be returned with the proper outputs. -assert_contains( - name = "default_info_test", - actual = ":a_tmpl_stamp", - expected = "WORKSPACE:", -)
diff --git a/js/private/test/expand_template/a.tmpl b/js/private/test/expand_template/a.tmpl deleted file mode 100644 index abe382f..0000000 --- a/js/private/test/expand_template/a.tmpl +++ /dev/null
@@ -1,6 +0,0 @@ -WORKSPACE: {{WORKSPACE}} -VERSION: {{VERSION}} -TMPL_PATH: {{TMPL_PATH}} -WORKSPACE: {{WORKSPACE}} -VERSION: {{VERSION}} -TMPL_PATH: {{TMPL_PATH}}
diff --git a/js/private/test/expand_template/a_expected b/js/private/test/expand_template/a_expected deleted file mode 100644 index d67a616..0000000 --- a/js/private/test/expand_template/a_expected +++ /dev/null
@@ -1,6 +0,0 @@ -WORKSPACE: aspect_rules_js -VERSION: v0.0.0 -TMPL_PATH: js/private/test/expand_template/a.tmpl -WORKSPACE: aspect_rules_js -VERSION: v0.0.0 -TMPL_PATH: js/private/test/expand_template/a.tmpl
diff --git a/js/private/test/expand_template/a_expected_bzlmod b/js/private/test/expand_template/a_expected_bzlmod deleted file mode 100644 index 103336c..0000000 --- a/js/private/test/expand_template/a_expected_bzlmod +++ /dev/null
@@ -1,6 +0,0 @@ -WORKSPACE: _main -VERSION: v0.0.0 -TMPL_PATH: js/private/test/expand_template/a.tmpl -WORKSPACE: _main -VERSION: v0.0.0 -TMPL_PATH: js/private/test/expand_template/a.tmpl
diff --git a/js/private/test/expand_template/a_stamp_expected b/js/private/test/expand_template/a_stamp_expected deleted file mode 100644 index 668b850..0000000 --- a/js/private/test/expand_template/a_stamp_expected +++ /dev/null
@@ -1,6 +0,0 @@ -WORKSPACE: aspect_rules_js -VERSION: v1.2.3 -TMPL_PATH: js/private/test/expand_template/a.tmpl -WORKSPACE: aspect_rules_js -VERSION: v1.2.3 -TMPL_PATH: js/private/test/expand_template/a.tmpl
diff --git a/js/private/test/expand_template/a_stamp_expected_bzlmod b/js/private/test/expand_template/a_stamp_expected_bzlmod deleted file mode 100644 index e099fa5..0000000 --- a/js/private/test/expand_template/a_stamp_expected_bzlmod +++ /dev/null
@@ -1,6 +0,0 @@ -WORKSPACE: _main -VERSION: v1.2.3 -TMPL_PATH: js/private/test/expand_template/a.tmpl -WORKSPACE: _main -VERSION: v1.2.3 -TMPL_PATH: js/private/test/expand_template/a.tmpl
diff --git a/js/repositories.bzl b/js/repositories.bzl index b5f9fbc..33a2fd2 100644 --- a/js/repositories.bzl +++ b/js/repositories.bzl
@@ -21,7 +21,7 @@ http_archive( name = "aspect_bazel_lib", - sha256 = "0da75299c5a52737b2ac39458398b3f256e41a1a6748e5457ceb3a6225269485", - strip_prefix = "bazel-lib-1.31.2", - url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.31.2/bazel-lib-v1.31.2.tar.gz", + sha256 = "f1c181b910f821072f38ee45bb87db6b56275458c7f832c54c54ba6334119eca", + strip_prefix = "bazel-lib-1.32.0", + url = "https://github.com/aspect-build/bazel-lib/releases/download/v1.32.0/bazel-lib-v1.32.0.tar.gz", )