Skip to content

Commit

Permalink
Stamps @aspect_rules_js NPM packages.
Browse files Browse the repository at this point in the history
Refs #48.

This the `rules_prerender` and `@rules_prerender/declarative_shadow_dom` `package.json` files which the current version. I initially tried to use [`stamped_package_json()`](https://docs.aspect.build/rules/aspect_rules_js/docs/npm_package#stamped_package_json), but found it insufficient because `@rules_prerender/declarative_shadow_dom` includes a peer dep on `rules_prerender` which was not being stamped. I filed aspect-build/rules_js#866 to suggest this as a general feature, but opted to implement my own version for now. It also uses `jq()` under the hood to transform the input JSON. It stamps the `version` property and any `rules_prerender` or `@rules_prerender/*` dependencies.

I also add a `default_version` attribute. Not so much because it is actually useful, but because it makes the rule easier to test. Technically these tests fail with `--stamp`, but I don't think that should be too much of an issue.
  • Loading branch information
dgp1130 committed Feb 12, 2023
1 parent 6694e6a commit f42c1ea
Show file tree
Hide file tree
Showing 13 changed files with 257 additions and 4 deletions.
5 changes: 3 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm_rules_js//:defs.bzl", "npm_link_all_packages")
load("//tools:publish.bzl", "publish_files")
load("//tools/stamping:stamp_package.bzl", "stamp_package")
load("//:index.bzl", "link_prerender_component", "prerender_component", "prerender_component_publish_files")

ts_config(
Expand All @@ -24,13 +25,13 @@ ts_config(
npm_link_all_packages(name = "node_modules")

# Makes an internal NPM package for `rules_prerender` and links it.
# TODO(#48): Stamp versions.
# TODO(#48): Move to `//packages/rules_prerender` once dependencies outside
# that package are addressed.
stamp_package(name = "package")
npm_package(
name = "rules_prerender_pkg",
srcs = [
":package.json",
":package",
"//packages/rules_prerender:rules_prerender_lib",
],
package = "rules_prerender",
Expand Down
7 changes: 6 additions & 1 deletion WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ http_archive(
url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.23.3.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")
load(
"@aspect_bazel_lib//lib:repositories.bzl",
"aspect_bazel_lib_dependencies",
"register_jq_toolchains",
)
aspect_bazel_lib_dependencies()
register_jq_toolchains()

# Install the nodejs "bootstrap" package
# This provides the basic tools for running and packaging nodejs programs in Bazel
Expand Down
14 changes: 14 additions & 0 deletions examples/external/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ workspace(name = "external")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "aspect_bazel_lib",
sha256 = "79623d656aa23ad3fd4692ab99786c613cd36e49f5566469ed97bc9b4c655f03",
strip_prefix = "bazel-lib-1.23.3",
url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.23.3.tar.gz",
)
load(
"@aspect_bazel_lib//lib:repositories.bzl",
"aspect_bazel_lib_dependencies",
"register_jq_toolchains",
)
aspect_bazel_lib_dependencies()
register_jq_toolchains()

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "dcc55f810142b6cf46a44d0180a5a7fb923c04a5061e2e8d8eb05ccccc60864b",
Expand Down
4 changes: 3 additions & 1 deletion packages/rules_prerender/declarative_shadow_dom/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load(
load("//tools:jasmine.bzl", "jasmine_node_test")
load("//tools:publish.bzl", "publish_files")
load("//tools:typescript.bzl", "ts_project")
load("//tools/stamping:stamp_package.bzl", "stamp_package")

publish_files(
name = "publish_files",
Expand All @@ -22,11 +23,12 @@ prerender_component_publish_files(
dep = ":declarative_shadow_dom",
)

stamp_package(name = "package")
npm_package(
name = "pkg",
srcs = [
":declarative_shadow_dom_publish_files",
"package.json",
":package",
],
package = "@rules_prerender/declarative_shadow_dom",
visibility = ["//visibility:public"],
Expand Down
49 changes: 49 additions & 0 deletions tools/stamping/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("@aspect_bazel_lib//lib:diff_test.bzl", "diff_test")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":stamp_package.bzl", "stamp_package")

exports_files(["stamp.jq"], visibility = ["//visibility:public"])

bzl_library(
name = "stamp_package",
srcs = ["stamp_package.bzl"],
)

stamp_package(
name = "stamp_package_version",
package = "package_version.json",
default_version = "1.2.3",
testonly = True,
)

diff_test(
name = "stamp_package_version_test",
file1 = ":stamp_package_version",
file2 = "package_version_expected.json",
)

stamp_package(
name = "stamp_package_dependencies",
package = "package_dependencies.json",
default_version = "1.2.3",
testonly = True,
)

diff_test(
name = "stamp_package_dependencies_test",
file1 = ":stamp_package_dependencies",
file2 = "package_dependencies_expected.json",
)

stamp_package(
name = "stamp_package_ignored_dependencies",
package = "package_ignored_dependencies.json",
default_version = "1.2.3",
testonly = True,
)

diff_test(
name = "stamp_package_ignored_dependencies_test",
file1 = ":stamp_package_ignored_dependencies",
file2 = "package_ignored_dependencies_expected.json",
)
24 changes: 24 additions & 0 deletions tools/stamping/package_dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "test",
"version": "0.0.0-PLACEHOLDER",
"dependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"@rules_prerender/foo": "^0.0.0-PLACEHOLDER",
"@rules_prerender/bar": "~0.0.0-PLACEHOLDER"
},
"devDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"@rules_prerender/foo": "^0.0.0-PLACEHOLDER",
"@rules_prerender/bar": "~0.0.0-PLACEHOLDER"
},
"peerDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"@rules_prerender/foo": "^0.0.0-PLACEHOLDER",
"@rules_prerender/bar": "~0.0.0-PLACEHOLDER"
},
"optionalDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"@rules_prerender/foo": "^0.0.0-PLACEHOLDER",
"@rules_prerender/bar": "~0.0.0-PLACEHOLDER"
}
}
24 changes: 24 additions & 0 deletions tools/stamping/package_dependencies_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "test",
"version": "1.2.3",
"dependencies": {
"rules_prerender": "1.2.3",
"@rules_prerender/foo": "^1.2.3",
"@rules_prerender/bar": "~1.2.3"
},
"devDependencies": {
"rules_prerender": "1.2.3",
"@rules_prerender/foo": "^1.2.3",
"@rules_prerender/bar": "~1.2.3"
},
"peerDependencies": {
"rules_prerender": "1.2.3",
"@rules_prerender/foo": "^1.2.3",
"@rules_prerender/bar": "~1.2.3"
},
"optionalDependencies": {
"rules_prerender": "1.2.3",
"@rules_prerender/foo": "^1.2.3",
"@rules_prerender/bar": "~1.2.3"
}
}
24 changes: 24 additions & 0 deletions tools/stamping/package_ignored_dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "test",
"version": "0.0.0-PLACEHOLDER",
"dependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"devDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"peerDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"optionalDependencies": {
"rules_prerender": "0.0.0-PLACEHOLDER",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
}
}
24 changes: 24 additions & 0 deletions tools/stamping/package_ignored_dependencies_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "test",
"version": "1.2.3",
"dependencies": {
"rules_prerender": "1.2.3",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"devDependencies": {
"rules_prerender": "1.2.3",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"peerDependencies": {
"rules_prerender": "1.2.3",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
},
"optionalDependencies": {
"rules_prerender": "1.2.3",
"ignored-dep": "0.0.0-PLACEHOLDER",
"@ignored/other-dep": "0.0.0-PLACEHOLDER"
}
}
4 changes: 4 additions & 0 deletions tools/stamping/package_version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test",
"version": "0.0.0-PLACEHOLDER"
}
4 changes: 4 additions & 0 deletions tools/stamping/package_version_expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "test",
"version": "1.2.3"
}
43 changes: 43 additions & 0 deletions tools/stamping/stamp.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Read arguments.
$ARGS.named.STAMP as $stamp |
$ARGS.named.DEFAULT_VERSION as $default_version |
($stamp.STABLE_RULES_PRERENDER_VERSION // $default_version) as $version |
$ARGS.named.PACKAGE_FILTER as $package_filter |

# Replace a semver string with the stamped version.
# `0.0.0-PLACEHOLDER` -> `1.2.3`
# `^0.0.0-PLACEHOLDER` -> `^1.2.3`
# `~0.0.0-PLACEHOLDER` -> `~1.2.3`
def replace_version:
if startswith("^") then
"^" + $version
elif startswith("~") then
"~" + $version
else
$version
end
;

# Takes a dependencies object and replaces the versions of all the dependencies
# matching `$package_filter`.
def map_deps:
with_entries(
.key as $key |
.value |= if $key | test($package_filter) then
replace_version
else
.
end
)
;

# Stamp the `version` property and all the dependencies.
if $version then
.version = $version |
if .dependencies then .dependencies |= map_deps else . end |
if .devDependencies then .devDependencies |= map_deps else . end |
if .peerDependencies then .peerDependencies |= map_deps else . end |
if .optionalDependencies then .optionalDependencies |= map_deps else . end
else
.
end
35 changes: 35 additions & 0 deletions tools/stamping/stamp_package.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

def stamp_package(
name,
package = "package.json",
default_version = None,
**kwargs
):
"""Updates `package.json` files with the stamped version.
In unstamped builds, the `package.json` file will be left alone. In stamped
builds, the `package.json` will be updated to use the stamped version.
This stamps both the `version` property and all the `rules_prerender` and
`@rules_prerender/*` dependencies. If a dependency uses `^` or `~`, that
prefix is retained while the version is replaced with the stamped version.
Params:
name: Name of this target.
package: The file to stamp. Defaults to `package.json`.
default_version: The version to use when stamping is not enabled.
If not given, the `package.json` file is left unchanged. This is
mainly intended for testing purposes.
**kwargs: Remaining arguments to pass through to the underlying rule.
"""
package_filter_args = ["--arg", "PACKAGE_FILTER", "\"^rules_prerender|^@rules_prerender/\""]
default_version_args = ["--arg", "DEFAULT_VERSION", default_version] if default_version else []
jq(
name = name,
srcs = [package],
filter_file = Label("//tools/stamping:stamp.jq"),
args = package_filter_args + default_version_args,
**kwargs
)

0 comments on commit f42c1ea

Please sign in to comment.