aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYifan Hong <elsk@google.com>2023-12-08 13:21:27 -0800
committerYifan Hong <elsk@google.com>2023-12-08 14:14:54 -0800
commitaf2a7a2f0db5b4a44cd0add866e924e633cd5ed8 (patch)
tree6fb7c09527a3029ea70f70e60f50be247eaee558
parent22f99717b19b2c9f85885f55c687d67e290217de (diff)
downloadbazel_common_rules-af2a7a2f0db5b4a44cd0add866e924e633cd5ed8.tar.gz
docs: Allow srcs in a different pkg
Previously, the docs.srcs attribute must contain .bzl files from the same package. After this change, this requirement is relaxed so we can move stardoc generation to a different package. Bug: 309174617 Change-Id: I25b2698f0695a3f3e56f918b4250cc26c2285378
-rw-r--r--docs/docs.bzl23
1 files changed, 17 insertions, 6 deletions
diff --git a/docs/docs.bzl b/docs/docs.bzl
index e770fb6..50097d2 100644
--- a/docs/docs.bzl
+++ b/docs/docs.bzl
@@ -12,9 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir")
-load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
+"""Generate bare-bones docs with Stardoc"""
+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc")
+load("//build/bazel_common_rules/dist:dist.bzl", "copy_to_dist_dir")
+
+def _sanitize_label_as_filename(label):
+ """Sanitize a Bazel label so it is safe to be used as a filename."""
+ label_text = str(label)
+ return _normalize(label_text)
+
+def _normalize(s):
+ """Returns a normalized string by replacing non-letters / non-numbers as underscores."""
+ return "".join([c if c.isalnum() else "_" for c in s.elems()])
# TODO: Add aspect_template when necessary
def docs(
@@ -68,15 +79,15 @@ def docs(
all_markdown_files = []
for src in srcs:
stardoc(
- name = name + "-" + src,
- out = name + "/" + src,
+ name = name + "-" + _sanitize_label_as_filename(src),
+ out = name + "/" + _sanitize_label_as_filename(src),
input = src,
deps = [":" + name + "_deps"],
func_template = func_template,
provider_template = provider_template,
rule_template = rule_template,
)
- all_markdown_files.append((name + "/" + src, src))
+ all_markdown_files.append((name + "/" + _sanitize_label_as_filename(src), src))
native.filegroup(
name = name + "_markdown_files",
@@ -84,7 +95,7 @@ def docs(
)
default_file_cmd = """touch $@ && """
- for target, src in all_markdown_files:
+ for src in srcs:
if default == src:
default_file_cmd += """echo '<div hidden><a href="#{src}" id="default_file">{src}</a></div>' >> $@ &&""".format(
src = src,