Skip to content

Commit 9a1edce

Browse files
authored
fix: set correct suggested_update_target for write_source_files macros with multiple files (#120)
1 parent 4b1a0df commit 9a1edce

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

docs/docs.md

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/private/docs.bzl

+9-21
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11
"Helpers for generating stardoc documentation"
22

3-
load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
3+
load("@io_bazel_stardoc//stardoc:stardoc.bzl", _stardoc = "stardoc")
44
load("//lib:write_source_files.bzl", "write_source_files")
55

66
def stardoc_with_diff_test(
77
name,
88
bzl_library_target,
9-
suggested_update_target = "//docs:update",
109
**kwargs):
11-
"""Creates a stardoc target, diff test, and an executable to rule to write the generated doc to the source tree and test that it's up to date.
10+
"""Creates a stardoc target that can be auto-detected by update_docs to write the generated doc to the source tree and test that it's up to date.
1211
1312
This is helpful for minimizing boilerplate in repos wih lots of stardoc targets.
1413
1514
Args:
1615
name: the name of the stardoc file to be written to the current source directory (.md will be appended to the name). Call bazel run on this target to update the file.
1716
bzl_library_target: the label of the `bzl_library` target to generate documentation for
18-
suggested_update_target: the target suggested to be run when a doc is out of date (should be the label for [update_docs](#update_docs))
1917
**kwargs: additional attributes passed to the stardoc() rule, such as for overriding the templates
2018
"""
2119

22-
stardoc_label = name + "-docgen"
23-
out_file = name + ".md"
24-
2520
# Generate MD from .bzl
26-
stardoc(
27-
name = stardoc_label,
21+
_stardoc(
22+
name = name,
2823
out = name + "-docgen.md",
2924
input = bzl_library_target + ".bzl",
3025
deps = [bzl_library_target],
3126
tags = ["package:" + native.package_name()], # Tag the package name which will help us reconstruct the write_source_files label in update_docs
3227
**kwargs
3328
)
3429

35-
write_source_files(
36-
name = name,
37-
suggested_update_target = suggested_update_target,
38-
files = {
39-
out_file: ":" + stardoc_label,
40-
},
41-
)
42-
4330
def update_docs(name = "update"):
4431
"""Stamps an executable run for writing all stardocs declared with stardoc_with_diff_test to the source tree.
4532
@@ -60,16 +47,17 @@ def update_docs(name = "update"):
6047
name: the name of executable target
6148
"""
6249

63-
update_labels = []
50+
update_files = {}
6451
for r in native.existing_rules().values():
6552
if r["kind"] == "stardoc":
6653
for tag in r["tags"]:
6754
if tag.startswith("package:"):
6855
stardoc_name = r["name"]
69-
write_source_files_name = stardoc_name[:-len("-docgen")]
70-
update_labels.append("//%s:%s" % (tag[len("package:"):], write_source_files_name))
56+
source_file_name = stardoc_name + ".md"
57+
generated_file_name = stardoc_name + "-docgen.md"
58+
update_files[source_file_name] = generated_file_name
7159

7260
write_source_files(
7361
name = name,
74-
additional_update_targets = update_labels,
62+
files = update_files,
7563
)

lib/write_source_files.bzl

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,22 @@ def write_source_files(
9696
for i, pair in enumerate(files.items()):
9797
out_file, in_file = pair
9898

99+
this_suggested_update_target = suggested_update_target
99100
if single_update_target:
100101
update_target_name = name
101102
else:
102103
update_target_name = "%s_%d" % (name, i)
103104
update_targets.append(update_target_name)
105+
if not this_suggested_update_target:
106+
this_suggested_update_target = name
104107

105108
# Runnable target that writes to the out file to the source tree
106109
_write_source_file(
107110
name = update_target_name,
108111
in_file = in_file,
109112
out_file = out_file,
110113
additional_update_targets = additional_update_targets if single_update_target else [],
111-
suggested_update_target = suggested_update_target,
114+
suggested_update_target = this_suggested_update_target,
112115
diff_test = diff_test,
113116
**kwargs
114117
)

0 commit comments

Comments
 (0)