-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stamps
@aspect_rules_js
NPM packages.
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
Showing
13 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "test", | ||
"version": "0.0.0-PLACEHOLDER" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.2.3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |