mirror of
https://github.com/bazelbuild/bazel-skylib
synced 2024-11-27 05:43:25 +00:00
6bf6443975
The user can specify which line endings they want write_file to use. This helps avoiding line ending mismatches with diff_test. Example: diff_test verifies that a rule generates correct output by comparing it to a checked-in "golden" file. Both files are text files, and the user builds on Windows but the golden file was written on Linux and git checkout preserved original line endings. Without explicitly specifying which line endings to use, this diff_test would fail on an otherwise good output. With explicit line endings we don't need to check in the golden file to git, we can just generate it with "auto" line endings.
114 lines
3.8 KiB
Python
114 lines
3.8 KiB
Python
# 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.
|
|
|
|
"""Implementation of write_file macro and underlying rules.
|
|
|
|
These rules write a UTF-8 encoded text file, using Bazel's FileWriteAction.
|
|
'_write_xfile' marks the resulting file executable, '_write_file' does not.
|
|
"""
|
|
|
|
def _common_impl(ctx, is_windows, is_executable):
|
|
if ctx.attr.newline == "auto":
|
|
newline = "\r\n" if 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(ctx.attr.content) if ctx.attr.content else "",
|
|
is_executable = is_executable,
|
|
)
|
|
files = depset(direct = [ctx.outputs.out])
|
|
runfiles = ctx.runfiles(files = [ctx.outputs.out])
|
|
if is_executable:
|
|
return [DefaultInfo(files = files, runfiles = runfiles, executable = ctx.outputs.out)]
|
|
else:
|
|
return [DefaultInfo(files = files, runfiles = runfiles)]
|
|
|
|
def _impl(ctx):
|
|
return _common_impl(ctx, ctx.attr.is_windows, False)
|
|
|
|
def _ximpl(ctx):
|
|
return _common_impl(ctx, ctx.attr.is_windows, True)
|
|
|
|
_ATTRS = {
|
|
"out": attr.output(mandatory = True),
|
|
"content": attr.string_list(mandatory = False, allow_empty = True),
|
|
"newline": attr.string(values = ["unix", "windows", "auto"], default = "auto"),
|
|
"is_windows": attr.bool(mandatory = True),
|
|
}
|
|
|
|
_write_file = rule(
|
|
implementation = _impl,
|
|
provides = [DefaultInfo],
|
|
attrs = _ATTRS,
|
|
)
|
|
|
|
_write_xfile = rule(
|
|
implementation = _ximpl,
|
|
executable = True,
|
|
provides = [DefaultInfo],
|
|
attrs = _ATTRS,
|
|
)
|
|
|
|
def write_file(
|
|
name,
|
|
out,
|
|
content = [],
|
|
is_executable = False,
|
|
newline = "auto",
|
|
**kwargs):
|
|
"""Creates a UTF-8 encoded text file.
|
|
|
|
Args:
|
|
name: Name of the rule.
|
|
out: Path of the output file, relative to this package.
|
|
content: A list of strings. Lines of text, the contents of the file.
|
|
Newlines are added automatically after every line except the last one.
|
|
is_executable: A boolean. Whether to make the output file executable.
|
|
When True, the rule's output can be executed using `bazel run` and can
|
|
be in the srcs of binary and test rules that require executable
|
|
sources.
|
|
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>
|
|
"""
|
|
if is_executable:
|
|
_write_xfile(
|
|
name = name,
|
|
content = content,
|
|
out = out,
|
|
newline = newline or "auto",
|
|
is_windows = select({
|
|
"@bazel_tools//src/conditions:host_windows": True,
|
|
"//conditions:default": False,
|
|
}),
|
|
**kwargs
|
|
)
|
|
else:
|
|
_write_file(
|
|
name = name,
|
|
content = content,
|
|
out = out,
|
|
newline = newline or "auto",
|
|
is_windows = select({
|
|
"@bazel_tools//src/conditions:host_windows": True,
|
|
"//conditions:default": False,
|
|
}),
|
|
**kwargs
|
|
)
|