Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNL: Add example of using expand_template to stamp a pacakge.json for use in an npm_package #179

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions expand_template/.aspect/bazelrc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user.bazelrc
1 change: 1 addition & 0 deletions expand_template/.aspect/bazelrc/ci.bazelrc
1 change: 1 addition & 0 deletions expand_template/.aspect/bazelrc/common.bazelrc
1 change: 1 addition & 0 deletions expand_template/.aspect/bazelrc/common6.bazelrc
1 change: 1 addition & 0 deletions expand_template/.aspect/bazelrc/javascript.bazelrc
1 change: 1 addition & 0 deletions expand_template/.bazeliskrc
21 changes: 21 additions & 0 deletions expand_template/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions expand_template/.bazelversion
1 change: 1 addition & 0 deletions expand_template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bazel-*
32 changes: 32 additions & 0 deletions expand_template/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
2 changes: 2 additions & 0 deletions expand_template/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bazel_dep(name = "aspect_rules_js", version = "1.16.0")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
Empty file added expand_template/WORKSPACE.bazel
Empty file.
1 change: 1 addition & 0 deletions expand_template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 1;
3 changes: 3 additions & 0 deletions expand_template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "{{VERSION}}"
}
29 changes: 29 additions & 0 deletions expand_template/workspace_status.sh
Original file line number Diff line number Diff line change
@@ -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"