feat: write to source folders (#41)

This commit is contained in:
Derek Cormier 2022-03-02 12:58:36 -08:00 committed by GitHub
parent ca764e53e8
commit f788d286d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 14 deletions

View File

@ -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

View File

@ -10,7 +10,7 @@ Public API for write_source_files
write_source_files(<a href="#write_source_files-name">name</a>, <a href="#write_source_files-files">files</a>, <a href="#write_source_files-additional_update_targets">additional_update_targets</a>, <a href="#write_source_files-suggested_update_target">suggested_update_target</a>, <a href="#write_source_files-kwargs">kwargs</a>)
</pre>
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 |
| :------------- | :------------- | :------------- |
| <a id="write_source_files-name"></a>name | Name of the executable target that creates or updates the source file | none |
| <a id="write_source_files-files"></a>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 |
| <a id="write_source_files-files"></a>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 |
| <a id="write_source_files-additional_update_targets"></a>additional_update_targets | (Optional) List of other write_source_files targets to update in the same run | <code>[]</code> |
| <a id="write_source_files-suggested_update_target"></a>suggested_update_target | (Optional) Label of the write_source_files target to suggest running when files are out of date | <code>None</code> |
| <a id="write_source_files-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none |

View File

@ -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%

View File

@ -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",
},
)

View File

@ -0,0 +1 @@
console.log("e*")

View File

@ -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