-
Notifications
You must be signed in to change notification settings - Fork 6.8k
build: run unit tests with partial compilation and Angular linker #22979
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
Merged
mmalerba
merged 1 commit into
angular:master
from
devversion:build/run-unit-tests-with-linker
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,30 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_library( | ||
name = "sources", | ||
srcs = glob(["**/*.ts"]), | ||
deps = [ | ||
"@npm//@angular/compiler-cli", | ||
"@npm//@babel/core", | ||
"@npm//@babel/traverse", | ||
"@npm//@types/node", | ||
], | ||
) | ||
|
||
# Exposes the `linker-process` tool as executable so that it can be used as | ||
# build tool (for `ctx.actions.run`) within the `linker_process` custom Bazel rule. | ||
nodejs_binary( | ||
name = "linker-process", | ||
data = [ | ||
":sources", | ||
], | ||
entry_point = ":linker-process.ts", | ||
templated_args = [ | ||
# TODO(josephperrott): update dependency usages to no longer need bazel patch module resolver | ||
# See: https://github.com/bazelbuild/rules_nodejs/wiki#--bazel_patch_module_resolver-now-defaults-to-false-2324 | ||
"--bazel_patch_module_resolver", | ||
], | ||
) |
This file contains hidden or 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,100 @@ | ||
""" | ||
Exposes a custom Bazel rule for processing sources, which are extracted from Bazel | ||
targets, with the Angular linker plugin. | ||
""" | ||
|
||
load("@build_bazel_rules_nodejs//:providers.bzl", "JSModuleInfo", "JSNamedModuleInfo") | ||
devversion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
Gets the Bazel manifest path for a given file. Manifest paths are used within Bazel runfile | ||
manifests and are formatted as followed: `<workspace_name>/<workspace_relative_file_path>` | ||
""" | ||
|
||
def _to_manifest_path(ctx, file): | ||
# If a file resides outside of the current workspace, we omit the leading `../` | ||
# segment as the rest will contain the workspace name. e.g. `../npm/node_modules/<..>`. | ||
if file.short_path.startswith("../"): | ||
return file.short_path[3:] | ||
else: | ||
return ctx.workspace_name + "/" + file.short_path | ||
|
||
"""Extracts all source files from the specified list of dependencies.""" | ||
|
||
def _extract_source_files(deps): | ||
depsets = [] | ||
for dep in deps: | ||
if JSNamedModuleInfo in dep: | ||
depsets.append(dep[JSNamedModuleInfo].sources) | ||
elif JSModuleInfo in dep: | ||
depsets.append(dep[JSModuleInfo].sources) | ||
elif hasattr(dep, "files"): | ||
depsets.append(dep.files) | ||
return depset(transitive = depsets) | ||
|
||
def _linker_process(ctx): | ||
args = ctx.actions.args() | ||
sources = _extract_source_files(ctx.attr.srcs) | ||
tmp_dir_name = ctx.label.name | ||
|
||
# The output directory manifest path. e.g `angular_material/src/cdk/a11y/linker_processed`. | ||
output_dir_manifest_path = "%s/%s/%s" % (ctx.workspace_name, ctx.label.package, tmp_dir_name) | ||
|
||
# The output directory exec path. e.g `bazel_out/<..>/src/cdk/a11y/linker_processed`. | ||
output_dir_exec_path = "%s/%s/%s" % (ctx.bin_dir.path, ctx.label.package, tmp_dir_name) | ||
|
||
# Given the sources being transformed and written to a new location, the AMD module names | ||
# need to be rewritten. This file maps AMD modules as per the new location to the AMD modules | ||
# as they appear in the sources. i.e. we generate AMD module aliases. | ||
amd_module_mapping_file = ctx.actions.declare_file("%s/_module_mappings.js" % tmp_dir_name) | ||
|
||
args.add(output_dir_exec_path) | ||
args.add(output_dir_manifest_path) | ||
args.add(amd_module_mapping_file.path) | ||
|
||
outputs = [amd_module_mapping_file] | ||
|
||
# Iterate through the determined sources and pass them to the tool as argument. | ||
for input_file in sources.to_list(): | ||
output_pkg_path = _to_manifest_path(ctx, input_file) | ||
args.add("%s:%s" % (input_file.path, output_pkg_path)) | ||
outputs.append(ctx.actions.declare_file("%s/%s" % (tmp_dir_name, output_pkg_path))) | ||
|
||
# Support passing arguments through a parameter file. This is necessary because on Windows | ||
# there is an argument limit and we need to handle a large amount of input files. Bazel | ||
# switches between parameter file and normal argument passing based on the operating system. | ||
# Read more here: https://docs.bazel.build/versions/master/skylark/lib/Args.html#use_param_file | ||
args.use_param_file(param_file_arg = "--param-file=%s", use_always = True) | ||
|
||
ctx.actions.run( | ||
inputs = sources, | ||
outputs = outputs, | ||
executable = ctx.executable._linker_process_tool, | ||
arguments = [args], | ||
progress_message = "NgLinkerProcess", | ||
) | ||
|
||
outputs_depset = depset(outputs) | ||
|
||
return [ | ||
DefaultInfo(files = outputs_depset), | ||
] | ||
|
||
""" | ||
Rule definition for the "linker_process" rule that can process a list of targets | ||
with the Angular linker. The processed files can be retrieved through the default | ||
files provider, or through the `JSNamedModuleInfo` provider. | ||
""" | ||
linker_process = rule( | ||
implementation = _linker_process, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
doc = """List of sources that should be processed with the Angular linker.""", | ||
), | ||
"_linker_process_tool": attr.label( | ||
default = Label("//tools/linker-process"), | ||
executable = True, | ||
cfg = "host", | ||
), | ||
}, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.