7.3 KiB
Public API for write_source_files
write_source_file
write_source_file(name, in_file, out_file, executable, additional_update_targets, suggested_update_target, diff_test, kwargs)
Write a file or directory to the source tree.
By default, a diff_test
target ("{name}_test") is generated that ensure the source tree file or directory to be written to
is up to date and the rule also checks that the source tree file or directory to be written to exists.
To disable the exists check and up-to-date test set diff_test
to False
.
PARAMETERS
RETURNS
Name of the generated test target if requested, otherwise None.
write_source_files
write_source_files(name, files, executable, additional_update_targets, suggested_update_target, diff_test, kwargs)
Write one or more files and/or directories to the source tree.
By default, diff_test
targets are generated that ensure the source tree files and/or directories to be written to
are up to date and the rule also checks that all source tree files and/or directories to be written to exist.
To disable the exists check and up-to-date tests set diff_test
to False
.
Usage:
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
write_source_files(
name = "write_foobar",
files = {
"foobar.json": "//some/generated:file",
},
)
To update the source file, run:
bazel run //:write_foobar
The generated diff_test
will fail if the file is out of date and print out instructions on
how to update it.
If the file does not exist, Bazel will fail at analysis time and print out instructions on how to create it.
You can declare a tree of generated source file targets:
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
write_source_files(
name = "write_all",
additional_update_targets = [
# Other write_source_files targets to run when this target is run
"//a/b/c:write_foo",
"//a/b:write_bar",
]
)
And update them with a single run:
bazel run //:write_all
When a file is out of date, you can leave a suggestion to run a target further up in the tree by specifying suggested_update_target
.
For example,
write_source_files(
name = "write_foo",
files = {
"foo.json": ":generated-foo",
},
suggested_update_target = "//:write_all"
)
A test failure from foo.json
being out of date will yield the following message:
//a/b:c:foo.json is out of date. To update this and other generated files, run:
bazel run //:write_all
To update *only* this file, run:
bazel run //a/b/c:write_foo
If you have many write_source_files
targets that you want to update as a group, we recommend wrapping
write_source_files
in a macro that defaults suggested_update_target
to the umbrella update target.
NOTE: If you run formatters or linters on your codebase, it is advised that you exclude/ignore the outputs of this rule from those formatters/linters so as to avoid causing collisions and failing tests.
PARAMETERS