diff --git a/expand_template/.aspect/bazelrc/.gitignore b/expand_template/.aspect/bazelrc/.gitignore new file mode 100644 index 00000000..74824b01 --- /dev/null +++ b/expand_template/.aspect/bazelrc/.gitignore @@ -0,0 +1 @@ +user.bazelrc diff --git a/expand_template/.aspect/bazelrc/ci.bazelrc b/expand_template/.aspect/bazelrc/ci.bazelrc new file mode 120000 index 00000000..4bcad15d --- /dev/null +++ b/expand_template/.aspect/bazelrc/ci.bazelrc @@ -0,0 +1 @@ +../../../bazelrc/.aspect/bazelrc/ci.bazelrc \ No newline at end of file diff --git a/expand_template/.aspect/bazelrc/common.bazelrc b/expand_template/.aspect/bazelrc/common.bazelrc new file mode 120000 index 00000000..7e04a480 --- /dev/null +++ b/expand_template/.aspect/bazelrc/common.bazelrc @@ -0,0 +1 @@ +../../../bazelrc/.aspect/bazelrc/common.bazelrc \ No newline at end of file diff --git a/expand_template/.aspect/bazelrc/common6.bazelrc b/expand_template/.aspect/bazelrc/common6.bazelrc new file mode 120000 index 00000000..20d7f2c6 --- /dev/null +++ b/expand_template/.aspect/bazelrc/common6.bazelrc @@ -0,0 +1 @@ +../../../bazelrc/.aspect/bazelrc/common6.bazelrc \ No newline at end of file diff --git a/expand_template/.aspect/bazelrc/javascript.bazelrc b/expand_template/.aspect/bazelrc/javascript.bazelrc new file mode 120000 index 00000000..92156090 --- /dev/null +++ b/expand_template/.aspect/bazelrc/javascript.bazelrc @@ -0,0 +1 @@ +../../../bazelrc/.aspect/bazelrc/javascript.bazelrc \ No newline at end of file diff --git a/expand_template/.bazeliskrc b/expand_template/.bazeliskrc new file mode 120000 index 00000000..270b9ce4 --- /dev/null +++ b/expand_template/.bazeliskrc @@ -0,0 +1 @@ +../bazelrc/.bazeliskrc \ No newline at end of file diff --git a/expand_template/.bazelrc b/expand_template/.bazelrc new file mode 100644 index 00000000..9ddf603e --- /dev/null +++ b/expand_template/.bazelrc @@ -0,0 +1,21 @@ +# Import Aspect recommended Bazel settings for all projects +import %workspace%/.aspect/bazelrc/common.bazelrc + +# Import Aspect recommended Bazel 6 only settings for all projects +import %workspace%/.aspect/bazelrc/common6.bazelrc + +# Import Aspect recommended Bazel settings for JavaScript projects +import %workspace%/.aspect/bazelrc/javascript.bazelrc + +common --enable_bzlmod + +# TODO: something is up with expand_template in the sandbox in this simple example +build --spawn_strategy=local +build --stamp +build --workspace_status_command "${PWD}/workspace_status.sh" + +# Load any settings & overrides specific to the current user from `.aspect/bazelrc/user.bazelrc`. +# This file should appear in `.gitignore` so that settings are not shared with team members. This +# should be last statement in this config so the user configuration is able to overwrite flags from +# this file. See https://bazel.build/configure/best-practices#bazelrc-file. +try-import %workspace%/.aspect/bazelrc/user.bazelrc \ No newline at end of file diff --git a/expand_template/.bazelversion b/expand_template/.bazelversion new file mode 120000 index 00000000..af130ee0 --- /dev/null +++ b/expand_template/.bazelversion @@ -0,0 +1 @@ +../bazelrc/.bazelversion \ No newline at end of file diff --git a/expand_template/.gitignore b/expand_template/.gitignore new file mode 100644 index 00000000..a6ef824c --- /dev/null +++ b/expand_template/.gitignore @@ -0,0 +1 @@ +/bazel-* diff --git a/expand_template/BUILD.bazel b/expand_template/BUILD.bazel new file mode 100644 index 00000000..63aaebc1 --- /dev/null +++ b/expand_template/BUILD.bazel @@ -0,0 +1,32 @@ +# We can't use the bazel-lib one, because it doesn't have a program to read stamp vars. +# see https://github.com/aspect-build/rules_js/pull/384#issue-1337742941 +# buildifier: disable=bzl-visibility +load("@aspect_rules_js//js/private:expand_template.bzl", "expand_template") +load("@aspect_rules_js//npm:defs.bzl", "npm_package") +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +expand_template( + name = "stamped_package_json", + out = "stamped_package.json", + stamp = 1, + substitutions = { + "{{VERSION}}": "{{STABLE_BUILD_SCM_TAG}}", + }, + template = "package.json", +) + +npm_package( + name = "package", + srcs = [ + "index.js", + ":stamped_package_json", + ], + replace_prefixes = { + "stamped_package.json": "package.json", + }, +) + +build_test( + name = "build_test", + targets = [":package"], +) diff --git a/expand_template/MODULE.bazel b/expand_template/MODULE.bazel new file mode 100644 index 00000000..ff6ce1d1 --- /dev/null +++ b/expand_template/MODULE.bazel @@ -0,0 +1,2 @@ +bazel_dep(name = "aspect_rules_js", version = "1.16.0") +bazel_dep(name = "bazel_skylib", version = "1.3.0") diff --git a/expand_template/WORKSPACE.bazel b/expand_template/WORKSPACE.bazel new file mode 100644 index 00000000..e69de29b diff --git a/expand_template/index.js b/expand_template/index.js new file mode 100644 index 00000000..bd816eab --- /dev/null +++ b/expand_template/index.js @@ -0,0 +1 @@ +module.exports = 1; diff --git a/expand_template/package.json b/expand_template/package.json new file mode 100644 index 00000000..72594bd5 --- /dev/null +++ b/expand_template/package.json @@ -0,0 +1,3 @@ +{ + "version": "{{VERSION}}" +} diff --git a/expand_template/workspace_status.sh b/expand_template/workspace_status.sh new file mode 100755 index 00000000..dd7d81b3 --- /dev/null +++ b/expand_template/workspace_status.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# This script is called by Bazel when it needs info about the git state. +# The --workspace_status_command flag tells Bazel the location of this script. +# This is configured in `/.bazelrc`. +set -o pipefail -o errexit -o nounset + +function has_local_changes { + if [ "$(git status --porcelain)" != "" ]; then + echo dirty + else + echo clean + fi +} + +# "volatile" keys, these will not cause a re-build because they're assumed to change on every build +# and its okay to use a stale value in a stamped binary +echo "BUILD_TIME $(date "+%Y-%m-%d %H:%M:%S %Z")" + +# "stable" keys, should remain constant over rebuilds, therefore changed values will cause a +# rebuild of any stamped action that uses ctx.info_file or genrule with stamp = True +# Note, BUILD_USER is automatically available in the stable-status.txt, it matches $USER +echo "STABLE_BUILD_SCM_SHA $(git rev-parse HEAD)" +echo "STABLE_BUILD_SCM_LOCAL_CHANGES $(has_local_changes)" + +# For demonstration, STABLE_BUILD_SCM_TAG is hard-coded to 1.2.3. Typically you would set this to +# the output of `git describe --tags` or use a program such as https://github.com/choffmeister/git-describe-semver +# to produce a semver compliant version. Alex's blog post, https://blog.aspect.dev/versioning-releases-from-a-monorepo, +# describes an alternative monorepo versioning scheme. +echo "STABLE_BUILD_SCM_TAG 1.2.3"