-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove templated_args_file from nodejs_binary & nodejs_test
BREAKING CHANGE: `templated_args_file` removed from nodejs_binary, nodejs_test & jasmine_node_test. This was a separation of concerns and complicated node.bzl more than necessary while also being rigid in how the params file is formatted. It is more flexible to expose this functionality as another simple rule named params_file. To match standard $(location) and $(locations) expansion, params_file args location expansions are also in the standard short_path form (this differs from the old templated_args behavior which was not Bazel idiomatic) Usage example: ``` load("@build_bazel_rules_nodejs//:index.bzl", "params_file", "nodejs_binary") params_file( name = "params_file", args = [ "--some_param", "$(location //path/to/some:file)", "--some_other_param", "$(location //path/to/some/other:file)", ], data = [ "//path/to/some:file", "//path/to/some/other:file", ], ) nodejs_binary( name = "my_binary", data = [":params_file"], entry_point = ":my_binary.js", templated_args = ["$(location :params_file)"], ) ```
- Loading branch information
1 parent
1860a6a
commit 799acb4
Showing
8 changed files
with
172 additions
and
42 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,98 @@ | ||
# Copyright 2019 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Generates a params file from a list of arguments. | ||
""" | ||
|
||
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles") | ||
|
||
_DOC = """Generates a params file from a list of arguments.""" | ||
|
||
_ATTRS = { | ||
"out": attr.output( | ||
doc = """Path of the output file, relative to this package.""", | ||
mandatory = True, | ||
), | ||
"args": attr.string_list( | ||
doc = """Arguments to concatenate into a params file. | ||
Subject to $(location) substitutions""", | ||
), | ||
"data": attr.label_list( | ||
doc = """Data for $(location) expansions in args.""", | ||
allow_files = True, | ||
), | ||
"is_windows": attr.bool(mandatory = True), | ||
"newline": attr.string( | ||
doc = """one of ["auto", "unix", "windows"]: line endings to use. "auto" | ||
for platform-determined, "unix" for LF, and "windows" for CRLF.""", | ||
values = ["unix", "windows", "auto"], | ||
default = "auto", | ||
), | ||
} | ||
|
||
def _impl(ctx): | ||
if ctx.attr.newline == "auto": | ||
newline = "\r\n" if ctx.attr.is_windows else "\n" | ||
elif ctx.attr.newline == "windows": | ||
newline = "\r\n" | ||
else: | ||
newline = "\n" | ||
|
||
# ctx.actions.write creates a FileWriteAction which uses UTF-8 encoding. | ||
ctx.actions.write( | ||
output = ctx.outputs.out, | ||
content = newline.join([expand_location_into_runfiles(ctx, a, ctx.attr.data) for a in ctx.attr.args]), | ||
is_executable = False, | ||
) | ||
files = depset(direct = [ctx.outputs.out]) | ||
runfiles = ctx.runfiles(files = [ctx.outputs.out]) | ||
return [DefaultInfo(files = files, runfiles = runfiles)] | ||
|
||
_params_file = rule( | ||
implementation = _impl, | ||
provides = [DefaultInfo], | ||
attrs = _ATTRS, | ||
doc = _DOC, | ||
) | ||
|
||
def params_file( | ||
name, | ||
out, | ||
args = [], | ||
newline = "auto", | ||
**kwargs): | ||
"""Generates a UTF-8 encoded params file from a list of arguments. | ||
Handles $(location) expansions for arguments. | ||
Args: | ||
name: Name of the rule. | ||
out: Path of the output file, relative to this package. | ||
args: Arguments to concatenate into a params file. | ||
Subject to $(location) substitutions | ||
newline: one of ["auto", "unix", "windows"]: line endings to use. "auto" | ||
for platform-determined, "unix" for LF, and "windows" for CRLF. | ||
**kwargs: further keyword arguments, e.g. <code>visibility</code> | ||
""" | ||
_params_file( | ||
name = name, | ||
out = out, | ||
args = args, | ||
newline = newline or "auto", | ||
is_windows = select({ | ||
"@bazel_tools//src/conditions:host_windows": True, | ||
"//conditions:default": False, | ||
}), | ||
**kwargs | ||
) |
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,36 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const args = process.argv.slice(2); | ||
|
||
// The arguments are passed via a params file | ||
const [a1, a2] = require('fs').readFileSync(require.resolve(args[0]), 'utf-8').split(/\r?\n/); | ||
|
||
const a1_exp = 'build_bazel_rules_nodejs/package.json'; | ||
const a2_exp = | ||
'build_bazel_rules_nodejs/package.json build_bazel_rules_nodejs/internal/common/test/check_params_file.js'; | ||
|
||
if (a1 !== a1_exp) { | ||
console.error(`expected first argument in params file to be '${a1_exp}'`) | ||
process.exit(1); | ||
} | ||
|
||
if (a2 !== a2_exp) { | ||
console.error(`expected second argument in params file to be '${a2_exp}'`) | ||
process.exit(1); | ||
} |
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