Skip to content

Commit

Permalink
feat(): macros nodejs_binary_toolchains nodejs_test_toolchains input …
Browse files Browse the repository at this point in the history
…multiple toolchains nodejs_binary or nodejs_test
  • Loading branch information
dymart committed Dec 10, 2021
1 parent 12a521d commit d1352a2
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
16 changes: 14 additions & 2 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Defaults to `"1.22.11"`

<pre>
nodejs_binary(<a href="#nodejs_binary-name">name</a>, <a href="#nodejs_binary-chdir">chdir</a>, <a href="#nodejs_binary-configuration_env_vars">configuration_env_vars</a>, <a href="#nodejs_binary-data">data</a>, <a href="#nodejs_binary-default_env_vars">default_env_vars</a>, <a href="#nodejs_binary-entry_point">entry_point</a>, <a href="#nodejs_binary-env">env</a>,
<a href="#nodejs_binary-link_workspace_root">link_workspace_root</a>, <a href="#nodejs_binary-node">node</a>, <a href="#nodejs_binary-templated_args">templated_args</a>)
<a href="#nodejs_binary-link_workspace_root">link_workspace_root</a>, <a href="#nodejs_binary-node">node</a>, <a href="#nodejs_binary-templated_args">templated_args</a>, <a href="#nodejs_binary-toolchain">toolchain</a>)
</pre>

Runs some JavaScript code in NodeJS.
Expand Down Expand Up @@ -484,14 +484,20 @@ Predefined genrule variables are not supported in this context.

Defaults to `[]`

<h4 id="nodejs_binary-toolchain">toolchain</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*)

Defaults to `None`


## nodejs_test

**USAGE**

<pre>
nodejs_test(<a href="#nodejs_test-name">name</a>, <a href="#nodejs_test-chdir">chdir</a>, <a href="#nodejs_test-configuration_env_vars">configuration_env_vars</a>, <a href="#nodejs_test-data">data</a>, <a href="#nodejs_test-default_env_vars">default_env_vars</a>, <a href="#nodejs_test-entry_point">entry_point</a>, <a href="#nodejs_test-env">env</a>,
<a href="#nodejs_test-expected_exit_code">expected_exit_code</a>, <a href="#nodejs_test-link_workspace_root">link_workspace_root</a>, <a href="#nodejs_test-node">node</a>, <a href="#nodejs_test-templated_args">templated_args</a>)
<a href="#nodejs_test-expected_exit_code">expected_exit_code</a>, <a href="#nodejs_test-link_workspace_root">link_workspace_root</a>, <a href="#nodejs_test-node">node</a>, <a href="#nodejs_test-templated_args">templated_args</a>, <a href="#nodejs_test-toolchain">toolchain</a>)
</pre>


Expand Down Expand Up @@ -744,6 +750,12 @@ Predefined genrule variables are not supported in this context.

Defaults to `[]`

<h4 id="nodejs_test-toolchain">toolchain</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*)

Defaults to `None`


## npm_install

Expand Down
8 changes: 7 additions & 1 deletion docs/Cypress.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Defaults to `None`
<pre>
cypress_web_test(<a href="#cypress_web_test-name">name</a>, <a href="#cypress_web_test-chdir">chdir</a>, <a href="#cypress_web_test-config_file">config_file</a>, <a href="#cypress_web_test-configuration_env_vars">configuration_env_vars</a>, <a href="#cypress_web_test-cypress_npm_package">cypress_npm_package</a>, <a href="#cypress_web_test-data">data</a>,
<a href="#cypress_web_test-default_env_vars">default_env_vars</a>, <a href="#cypress_web_test-entry_point">entry_point</a>, <a href="#cypress_web_test-env">env</a>, <a href="#cypress_web_test-expected_exit_code">expected_exit_code</a>, <a href="#cypress_web_test-link_workspace_root">link_workspace_root</a>, <a href="#cypress_web_test-node">node</a>,
<a href="#cypress_web_test-plugin_file">plugin_file</a>, <a href="#cypress_web_test-srcs">srcs</a>, <a href="#cypress_web_test-templated_args">templated_args</a>)
<a href="#cypress_web_test-plugin_file">plugin_file</a>, <a href="#cypress_web_test-srcs">srcs</a>, <a href="#cypress_web_test-templated_args">templated_args</a>, <a href="#cypress_web_test-toolchain">toolchain</a>)
</pre>


Expand Down Expand Up @@ -349,6 +349,12 @@ Predefined genrule variables are not supported in this context.

Defaults to `[]`

<h4 id="cypress_web_test-toolchain">toolchain</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*)

Defaults to `None`


## cypress_repositories

Expand Down
4 changes: 4 additions & 0 deletions index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ load("//internal/js_library:js_library.bzl", _js_library = "js_library")
load(
"//internal/node:node.bzl",
_nodejs_binary = "nodejs_binary_macro",
_nodejs_binary_toolchains = "nodejs_binary_toolchain_macro",
_nodejs_test = "nodejs_test_macro",
_nodejs_test_toolchains = "nodejs_test_toolchain_macro",
)
load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories")
load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin")
Expand All @@ -41,6 +43,8 @@ load(

check_bazel_version = _check_bazel_version
nodejs_binary = _nodejs_binary
nodejs_binary_toolchains = _nodejs_binary_toolchains
nodejs_test_toolchains = _nodejs_test_toolchains
nodejs_test = _nodejs_test
node_repositories = _node_repositories
pkg_npm = _pkg_npm
Expand Down
56 changes: 50 additions & 6 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ fi
# when building the image as that will reflect the selected --platform.
node_tool_files = ctx.files.node[:]

# this should be resolved the same as above
node_tool_files.extend(ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files)
if ctx.attr.toolchain:
node_tool_files.extend(ctx.attr.toolchain[platform_common.ToolchainInfo].nodeinfo.tool_files)
else:
node_tool_files.extend(ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files)

node_tool_files.append(ctx.file._link_modules_script)
node_tool_files.append(ctx.file._runfile_helpers_bundle)
Expand Down Expand Up @@ -597,14 +599,15 @@ Predefined genrule variables are not supported in this context.
default = Label("@nodejs//:node_bin"),
allow_single_file = True,
),
"_node_patches_script": attr.label(
default = Label("//internal/node:node_patches.js"),
allow_single_file = True,
),
"toolchain": attr.label(),
"_repository_args": attr.label(
default = Label("@nodejs//:bin/node_repo_args.sh"),
allow_single_file = True,
),
"_node_patches_script": attr.label(
default = Label("//internal/node:node_patches.js"),
allow_single_file = True,
),
"_require_patch_template": attr.label(
default = Label("//internal/node:require_patch.js"),
allow_single_file = True,
Expand Down Expand Up @@ -658,6 +661,27 @@ def nodejs_binary_macro(name, **kwargs):
**kwargs
)

def nodejs_binary_toolchain_macro(name, toolchains = [], **kwargs):
"""This extens nodejs binary and allows for multiple toolchains to be passed as a list and
a target to be created for each one automatically
"""
entry = maybe_directory_file_path(name, kwargs.pop("entry_point", None))
rule = 0

# Is there a good way to resolve the naming for the version chosen for each select statement?
# ideally able to produce the target name name_@toolchain ex: main_multiple_@node16_linux_amd64
# Is there a different outcome that would be better?
print(toolchains)
for toolchain in toolchains:
nodejs_binary(
name = "{name}_{toolchain}".format(name = name, toolchain = rule),
entry_point = entry,
node = toolchain,
toolchain = toolchain,
**kwargs
)
rule += 1

nodejs_test_kwargs = dict(
nodejs_binary_kwargs,
attrs = dict(nodejs_binary_kwargs["attrs"], **{
Expand Down Expand Up @@ -710,3 +734,23 @@ def nodejs_test_macro(name, **kwargs):
entry_point = maybe_directory_file_path(name, kwargs.pop("entry_point", None)),
**kwargs
)

def nodejs_test_toolchain_macro(name, toolchains = [], **kwargs):
"""This extens nodejs test and allows for multiple toolchains to be passed as a list and
a target to be created for each one automatically
"""
entry = maybe_directory_file_path(name, kwargs.pop("entry_point", None))
rule = 0

# Is there a good way to resolve the naming for the version chosen for each select statement?
# ideally able to produce the target name name_@toolchain ex: main_multiple_test_@node16_linux_amd64
# Is there a different outcome that would be better?
for toolchain in toolchains:
nodejs_test(
name = "{name}_{toolchain}".format(name = name, toolchain = rule),
entry_point = entry,
node = toolchain,
toolchain = toolchain,
**kwargs
)
rule += 1
74 changes: 73 additions & 1 deletion internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "nodejs_test", "npm_package_bin")
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "nodejs_binary_toolchains", "nodejs_test", "nodejs_test_toolchains", "npm_package_bin")
load("@npm//typescript:index.bzl", "tsc")
load("//:index.bzl", "js_library")
load("//nodejs:repositories.bzl", "BUILT_IN_NODE_PLATFORMS")
Expand Down Expand Up @@ -604,3 +604,75 @@ nodejs_test(
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain",
}),
)

# these rules create different targets for each toolchain that is supplied in the list for toolchains
# this makes it easy to build or test against multiple versions of a toolchain at the same time with less duplicate work

# this will create 2 nodejs_binary targets
# bazel build //internal/node/test:main_multiple_0
# bazel build //internal/node/test:main_multiple_1
# the numbers are based on the order that the toolchains are passed to the rule
nodejs_binary_toolchains(
name = "main_multiple",
entry_point = "binary_version.js",
toolchains = [
# using the select statement will download toolchains for all platforms
# you can also just provide an individual toolchain if you don't want to download them all
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:node_toolchain",
}),
],
)

# this will create 2 tests:
# bazel test //internal/node/test:main_multiple_test_15_0
# bazel test //internal/node/test:main_multiple_test_15_1
# the numbers are based on the order that the toolchains are passed to the rule
nodejs_test_toolchains(
name = "main_multiple_test_15",
entry_point = "toolchain_test_v15.14.0.js",
toolchains = [
# using the select statement will download toolchains for all platforms
# you can also just provide an individual toolchain if you don't want to download them all
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:node_toolchain",
}),
],
)

# this will create 2 test:
# bazel test //internal/node/test:main_multiple_test_16_0
# bazel test //internal/node/test:main_multiple_test_16_1
# the numbers are based on the order that the toolchains are passed to the rule
nodejs_test_toolchains(
name = "main_multiple_test_16",
entry_point = "toolchain_test_v16.js",
toolchains = [
# using the select statement will download toolchains for all platforms
# you can also just provide an individual toolchain if you don't want to download them all
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain",
}),
select({
"@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain",
"@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain",
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain",
}),
],
)

0 comments on commit d1352a2

Please sign in to comment.