diff --git a/.prettierignore b/.prettierignore
index d6ed2fd..5daed7c 100644
--- a/.prettierignore
+++ b/.prettierignore
@@ -2,3 +2,4 @@ docs/*.md
lib/tests/jq/*.json
lib/tests/write_source_files/a2.js
lib/tests/write_source_files/b2.js
+lib/tests/write_source_files/e_dir/e.js
\ No newline at end of file
diff --git a/docs/write_source_files.md b/docs/write_source_files.md
index 5e511ca..906af4a 100755
--- a/docs/write_source_files.md
+++ b/docs/write_source_files.md
@@ -10,7 +10,7 @@ Public API for write_source_files
write_source_files(name, files, additional_update_targets, suggested_update_target, kwargs)
-Write to one or more files in the source tree. Stamp out tests that ensure the files exists and are up to date.
+Write to one or more files or folders in the source tree. Stamp out tests that ensure the sources exist and are up to date.
Usage:
@@ -86,7 +86,7 @@ If you have many sources that you want to update as a group, we recommend wrappi
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| name | Name of the executable target that creates or updates the source file | none |
-| files | A dict where the keys are source files to write to and the values are labels pointing to the desired content. Source files must be within the same bazel package as the target. | none |
+| files | A dict where the keys are source files or folders to write to and the values are labels pointing to the desired content. Sources must be within the same bazel package as the target. | none |
| additional_update_targets | (Optional) List of other write_source_files targets to update in the same run | []
|
| suggested_update_target | (Optional) Label of the write_source_files target to suggest running when files are out of date | None
|
| kwargs | Other common named parameters such as tags
or visibility
| none |
diff --git a/lib/private/write_source_files.bzl b/lib/private/write_source_files.bzl
index 813799c..98b59e0 100644
--- a/lib/private/write_source_files.bzl
+++ b/lib/private/write_source_files.bzl
@@ -41,8 +41,15 @@ out={out_file}
mkdir -p "$(dirname "$out")"
echo "Copying $in to $out in $PWD"
-cp -f "$in" "$out"
-chmod 644 "$out"
+
+if [[ -f "$in" ]]; then
+ cp -f "$in" "$out"
+ chmod 664 "$out"
+else
+ mkdir -p "$out"
+ cp -rf "$in"/* "$out"
+ chmod 664 "$out"/*
+fi
""".format(in_file = ctx.files.in_files[i].short_path, out_file = ctx.files.out_files[i].short_path)
for i in range(len(ctx.attr.in_files))
]) + """
@@ -84,8 +91,14 @@ if not defined BUILD_WORKSPACE_DIRECTORY (
)
echo Copying %in% to %out% in %cd%
-copy %in% %out% >NUL
-""".format(in_file = ctx.files.in_files[i].short_path.replace("/", "\\"), out_file = ctx.files.out_files[i].short_path).replace("/", "\\")
+
+if exist "%in%\\*" (
+ mkdir "%out%" >NUL 2>NUL
+ robocopy "%in%" "%out%" /E >NUL
+) else (
+ copy %in% %out% >NUL
+)
+""".format(in_file = ctx.files.in_files[i].short_path.replace("/", "\\"), out_file = ctx.files.out_files[i].short_path.replace("/", "\\"))
for i in range(len(ctx.attr.in_files))
]) + """
cd %runfiles_dir%
diff --git a/lib/tests/write_source_files/BUILD.bazel b/lib/tests/write_source_files/BUILD.bazel
index 3fb83a0..e4440b0 100644
--- a/lib/tests/write_source_files/BUILD.bazel
+++ b/lib/tests/write_source_files/BUILD.bazel
@@ -1,5 +1,6 @@
load("//lib/tests/write_source_files:write_source_files_test.bzl", "write_source_files_test")
load("//lib:write_source_files.bzl", "write_source_files")
+load("//lib:copy_to_directory.bzl", "copy_to_directory")
genrule(
name = "a-desired",
@@ -13,6 +14,17 @@ genrule(
cmd = "echo 'console.log(\"b*\")' > $@",
)
+genrule(
+ name = "e",
+ outs = ["e.js"],
+ cmd = "echo 'console.log(\"e*\")' > $@",
+)
+
+copy_to_directory(
+ name = "e_dir-desired",
+ srcs = [":e"],
+)
+
write_source_files_test(
name = "write_to_source_files_test",
in_files = [
@@ -27,11 +39,12 @@ write_source_files_test(
write_source_files(
name = "macro_smoke_test",
- files = {
- "a2.js": ":a-desired",
- "b2.js": ":b-desired",
- },
additional_update_targets = [
"//lib/tests/write_source_files/subdir:macro_smoke_test",
],
+ files = {
+ "a2.js": ":a-desired",
+ "b2.js": ":b-desired",
+ "e_dir": ":e_dir-desired",
+ },
)
diff --git a/lib/tests/write_source_files/e_dir/e.js b/lib/tests/write_source_files/e_dir/e.js
new file mode 100644
index 0000000..9550183
--- /dev/null
+++ b/lib/tests/write_source_files/e_dir/e.js
@@ -0,0 +1 @@
+console.log("e*")
diff --git a/lib/write_source_files.bzl b/lib/write_source_files.bzl
index 8fb72f9..744610e 100644
--- a/lib/write_source_files.bzl
+++ b/lib/write_source_files.bzl
@@ -12,7 +12,7 @@ _write_source_files = rule(
)
def write_source_files(name, files, additional_update_targets = [], suggested_update_target = None, **kwargs):
- """Write to one or more files in the source tree. Stamp out tests that ensure the files exists and are up to date.
+ """Write to one or more files or folders in the source tree. Stamp out tests that ensure the sources exist and are up to date.
Usage:
@@ -83,8 +83,8 @@ def write_source_files(name, files, additional_update_targets = [], suggested_up
Args:
name: Name of the executable target that creates or updates the source file
- files: A dict where the keys are source files to write to and the values are labels pointing to the desired content.
- Source files must be within the same bazel package as the target.
+ files: A dict where the keys are source files or folders to write to and the values are labels pointing to the desired content.
+ Sources must be within the same bazel package as the target.
additional_update_targets: (Optional) List of other write_source_files targets to update in the same run
suggested_update_target: (Optional) Label of the write_source_files target to suggest running when files are out of date
**kwargs: Other common named parameters such as `tags` or `visibility`
@@ -191,5 +191,5 @@ def _is_file_missing(label):
"""
file_abs = "%s/%s" % (label.package, label.name)
file_rel = file_abs[len(native.package_name()) + 1:]
- file_glob = native.glob([file_rel])
+ file_glob = native.glob([file_rel], exclude_directories = 0)
return len(file_glob) == 0