diff --git a/docs/write_file_doc.md b/docs/write_file_doc.md index ecd96bd..d8f098e 100755 --- a/docs/write_file_doc.md +++ b/docs/write_file_doc.md @@ -1,7 +1,7 @@ ## write_file
-write_file(name, out, content, is_executable, kwargs) +write_file(name, out, content, is_executable, newline, kwargs)Creates a UTF-8 encoded text file. @@ -47,9 +47,20 @@ Creates a UTF-8 encoded text file.
False
- 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. + 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
"auto"
+ + one of ["auto", "unix", "windows"]: line endings to use. "auto" + for platform-determined, "unix" for LF, and "windows" for CRLF.
- further keyword arguments, e.g. `visibility`
+ further keyword arguments, e.g. visibility
visibility
"""
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:
@@ -81,5 +104,10 @@ def write_file(name, out, content = [], is_executable = False, **kwargs):
name = name,
content = content,
out = out,
+ newline = newline or "auto",
+ is_windows = select({
+ "@bazel_tools//src/conditions:host_windows": True,
+ "//conditions:default": False,
+ }),
**kwargs
)
diff --git a/tests/write_file/BUILD b/tests/write_file/BUILD
index 321bcc0..9ea3609 100644
--- a/tests/write_file/BUILD
+++ b/tests/write_file/BUILD
@@ -32,6 +32,7 @@
# field of it, so we assert that that field contains the output file of the
# rule
+load("//rules:diff_test.bzl", "diff_test")
load("//rules:write_file.bzl", "write_file")
licenses(["notice"])
@@ -117,3 +118,49 @@ write_file(
],
is_executable = True,
)
+
+write_file(
+ name = "newline_unix_actual",
+ out = "out/newline_unix_actual.txt",
+ content = [
+ "ab",
+ "cd",
+ "ef",
+ ],
+ newline = "unix",
+)
+
+write_file(
+ name = "newline_unix_exp",
+ out = "out/newline_unix_exp.txt",
+ content = ["ab\ncd\nef"],
+)
+
+diff_test(
+ name = "unix_line_ending_test",
+ file1 = ":newline_unix_actual",
+ file2 = ":newline_unix_exp",
+)
+
+write_file(
+ name = "newline_win_actual",
+ out = "out/newline_win_actual.txt",
+ content = [
+ "ab",
+ "cd",
+ "ef",
+ ],
+ newline = "windows",
+)
+
+write_file(
+ name = "newline_win_exp",
+ out = "out/newline_win_exp.txt",
+ content = ["ab\r\ncd\r\nef"],
+)
+
+diff_test(
+ name = "win_line_ending_test",
+ file1 = ":newline_win_actual",
+ file2 = ":newline_win_exp",
+)