2022-01-28 02:15:28 +00:00
|
|
|
"Public API for write_source_files"
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
load(
|
|
|
|
"//lib/private:write_source_file.bzl",
|
2022-03-28 21:53:58 +00:00
|
|
|
_write_source_file = "write_source_file",
|
2022-01-28 02:15:28 +00:00
|
|
|
)
|
|
|
|
|
2022-03-28 21:53:58 +00:00
|
|
|
def write_source_files(
|
|
|
|
name,
|
|
|
|
files = {},
|
|
|
|
additional_update_targets = [],
|
|
|
|
suggested_update_target = None,
|
|
|
|
diff_test = True,
|
|
|
|
**kwargs):
|
2022-03-02 20:58:36 +00:00
|
|
|
"""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.
|
2022-01-28 02:15:28 +00:00
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
```starlark
|
|
|
|
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:
|
|
|
|
```bash
|
|
|
|
bazel run //:write_foobar
|
|
|
|
```
|
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
A test will fail if the source file doesn't exist or if it's out of date with instructions on how to create/update it.
|
|
|
|
|
|
|
|
You can declare a tree of generated source file targets:
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
```starlark
|
|
|
|
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
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",
|
|
|
|
]
|
|
|
|
)
|
2022-01-28 02:15:28 +00:00
|
|
|
```
|
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
And update them with a single run:
|
|
|
|
|
2022-01-28 02:15:28 +00:00
|
|
|
```bash
|
2022-02-28 23:42:20 +00:00
|
|
|
bazel run //:write_all
|
|
|
|
```
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
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`. E.g.,
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
```starlark
|
|
|
|
write_source_files(
|
|
|
|
name = "write_foo",
|
|
|
|
files = {
|
|
|
|
"foo.json": ":generated-foo",
|
|
|
|
},
|
|
|
|
suggested_update_target = "//:write_all"
|
|
|
|
)
|
2022-01-28 02:15:28 +00:00
|
|
|
```
|
|
|
|
|
2022-02-28 23:42:20 +00:00
|
|
|
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 sources 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.
|
2022-06-24 19:30:08 +00:00
|
|
|
|
|
|
|
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.
|
2022-02-28 23:42:20 +00:00
|
|
|
|
2022-01-28 02:15:28 +00:00
|
|
|
Args:
|
|
|
|
name: Name of the executable target that creates or updates the source file
|
2022-03-02 20:58:36 +00:00
|
|
|
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.
|
2022-03-15 00:33:52 +00:00
|
|
|
additional_update_targets: (Optional) List of other write_source_file or other executable updater targets to call in the same run
|
|
|
|
suggested_update_target: (Optional) Label of the write_source_file target to suggest running when files are out of date
|
2022-03-28 21:53:58 +00:00
|
|
|
diff_test: (Optional) Generate a test target to check that the source file(s) exist and are up to date with the generated files(s).
|
2022-01-28 02:15:28 +00:00
|
|
|
**kwargs: Other common named parameters such as `tags` or `visibility`
|
|
|
|
"""
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
single_update_target = len(files.keys()) == 1
|
|
|
|
update_targets = []
|
2022-10-05 16:01:50 +00:00
|
|
|
test_targets = []
|
2022-03-15 00:33:52 +00:00
|
|
|
for i, pair in enumerate(files.items()):
|
|
|
|
out_file, in_file = pair
|
|
|
|
|
2022-05-26 01:47:32 +00:00
|
|
|
this_suggested_update_target = suggested_update_target
|
2022-03-15 00:33:52 +00:00
|
|
|
if single_update_target:
|
|
|
|
update_target_name = name
|
|
|
|
else:
|
|
|
|
update_target_name = "%s_%d" % (name, i)
|
|
|
|
update_targets.append(update_target_name)
|
2022-05-26 01:47:32 +00:00
|
|
|
if not this_suggested_update_target:
|
|
|
|
this_suggested_update_target = name
|
2022-03-15 00:33:52 +00:00
|
|
|
|
|
|
|
# Runnable target that writes to the out file to the source tree
|
2022-10-05 16:01:50 +00:00
|
|
|
test_target = _write_source_file(
|
2022-03-15 00:33:52 +00:00
|
|
|
name = update_target_name,
|
|
|
|
in_file = in_file,
|
|
|
|
out_file = out_file,
|
|
|
|
additional_update_targets = additional_update_targets if single_update_target else [],
|
2022-05-26 01:47:32 +00:00
|
|
|
suggested_update_target = this_suggested_update_target,
|
2022-03-28 21:53:58 +00:00
|
|
|
diff_test = diff_test,
|
|
|
|
**kwargs
|
2022-03-15 00:33:52 +00:00
|
|
|
)
|
2022-01-28 02:15:28 +00:00
|
|
|
|
2022-10-05 16:01:50 +00:00
|
|
|
if test_target:
|
|
|
|
test_targets.append(test_target)
|
|
|
|
|
|
|
|
if len(test_targets) > 0:
|
|
|
|
native.test_suite(
|
|
|
|
name = "%s_tests" % name,
|
|
|
|
tests = test_targets,
|
|
|
|
visibility = kwargs.get("visibility"),
|
|
|
|
tags = kwargs.get("tags"),
|
|
|
|
)
|
|
|
|
|
2022-03-15 00:33:52 +00:00
|
|
|
if not single_update_target:
|
|
|
|
_write_source_file(
|
|
|
|
name = name,
|
|
|
|
additional_update_targets = update_targets + additional_update_targets,
|
2022-03-28 21:53:58 +00:00
|
|
|
suggested_update_target = suggested_update_target,
|
|
|
|
diff_test = False,
|
|
|
|
**kwargs
|
2022-03-15 00:33:52 +00:00
|
|
|
)
|