2
0
Fork 0
mirror of https://github.com/bazel-contrib/bazel-lib synced 2024-12-03 11:52:43 +00:00
bazel-lib/docs/write_source_files.md

95 lines
3.2 KiB
Markdown
Raw Normal View History

2022-01-28 02:15:28 +00:00
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
Public API for write_source_files
<a id="#write_source_files"></a>
## write_source_files
<pre>
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>)
2022-01-28 02:15:28 +00:00
</pre>
Write to one or more files in the source tree. Stamp out tests that ensure the files exists and are up to date.
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
```
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
```starlark
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
2022-01-28 02:15:28 +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
```
And update them with a single run:
2022-01-28 02:15:28 +00:00
```bash
bazel run //:write_all
```
2022-01-28 02:15:28 +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.,
```starlark
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:
2022-01-28 02:15:28 +00:00
```
//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-01-28 02:15:28 +00:00
**PARAMETERS**
| 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-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> |
2022-01-28 02:15:28 +00:00
| <a id="write_source_files-kwargs"></a>kwargs | Other common named parameters such as <code>tags</code> or <code>visibility</code> | none |