Skip to content

Commit 5fb10d0

Browse files
committed
feat: generate doc_extract targets
Allows bzl_library targets to produce documentation without needing to load stardoc or add additional targets. Note, it would be better to do this with an aspect that visits the bzl_library graph, but the starlark_doc_extract rule is in Bazel java core, and so we cannot spawn actions using its Java code. It needs a main() entry point. Use a config_setting to make this opt-in, so we don't generate so many targets all the time.
1 parent 454b259 commit 5fb10d0

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@rules_license//rules:license.bzl", "license")
2+
load("//rules:common_settings.bzl", "bool_flag")
23
load("//:bzl_library.bzl", "bzl_library")
34

45
package(
@@ -95,3 +96,17 @@ filegroup(
9596
"//toolchains/unittest:distribution",
9697
] + glob(["*.bzl"]),
9798
)
99+
100+
101+
bool_flag(
102+
name = "extract_docs",
103+
build_setting_default = False,
104+
visibility = ["//visibility:public"],
105+
)
106+
107+
config_setting(
108+
name = "extract_docs_flag",
109+
flag_values = {
110+
":extract_docs": "true",
111+
},
112+
)

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ register_toolchains(
1010
"//toolchains/unittest:bash_toolchain",
1111
)
1212

13+
bazel_dep(name = "bazel_features", version = "1.29.0")
1314
bazel_dep(name = "platforms", version = "0.0.10")
1415
bazel_dep(name = "rules_license", version = "1.0.0")
1516

bzl_library.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ load(
2323

2424
StarlarkLibraryInfo = _StarlarkLibraryInfo
2525

26-
def bzl_library(name, **kwargs):
26+
def bzl_library(name, srcs = [], deps = [], **kwargs):
2727
"""Wrapper for bzl_library.
2828
2929
Args:
@@ -36,11 +36,25 @@ def bzl_library(name, **kwargs):
3636
_ = kwargs.pop("exec_compatible_with", None)
3737
_ = kwargs.pop("features", None)
3838
_ = kwargs.pop("target_compatible_with", None)
39+
3940
_bzl_library(
4041
name = name,
42+
srcs = srcs,
43+
deps = deps,
4144
compatible_with = [],
4245
exec_compatible_with = [],
4346
features = [],
4447
target_compatible_with = [],
4548
**kwargs
4649
)
50+
enable_doc_extract = select({
51+
Label("//:extract_docs_flag"): hasattr(native, "starlark_doc_extract"),
52+
"//conditions:default": False,
53+
})
54+
if enable_doc_extract:
55+
for i, src in enumerate(srcs):
56+
native.starlark_doc_extract(
57+
name = "{}.doc_extract{}".format(name, i if i > 0 else ""),
58+
src = src,
59+
deps = deps,
60+
)

0 commit comments

Comments
 (0)