aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-14 00:01:13 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-12-14 00:01:13 +0000
commitae30fb761c676dd815dc6f1085fcf8072ae90059 (patch)
treedec953318ec716d8257efbffabe1bc168d5ab5b9
parent260add9dbb9d71f62357c41494ea589d37c1db21 (diff)
parent9f9e668d919f2cf47e5e031fa07bcc55bcaf35ee (diff)
downloadbazel_common_rules-android14-qpr2-s1-release.tar.gz
Change-Id: Ib8c46172f67019f262e021f330297924de1264c1
-rw-r--r--docs/docs.bzl66
-rwxr-xr-xdocs/insert_resource.py7
2 files changed, 49 insertions, 24 deletions
diff --git a/docs/docs.bzl b/docs/docs.bzl
index e770fb6..ed25a45 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(
@@ -24,7 +35,8 @@ def docs(
deps = None,
func_template = None,
provider_template = None,
- rule_template = None):
+ rule_template = None,
+ **kwargs):
"""Build docs.
The following rules are also generated:
@@ -46,6 +58,7 @@ def docs(
func_template: Template for generating docs for functions.
provider_template: Template for generating docs for providers.
rule_template: Template for generating docs for rules.
+ **kwargs: kwargs to internal rules
"""
all_deps = []
@@ -60,31 +73,35 @@ def docs(
if rule_template == None:
rule_template = "//build/bazel_common_rules/docs:templates/rule.vm"
+ private_kwargs = kwargs | {
+ "visibility": ["//visibility:private"],
+ }
+
bzl_library(
name = name + "_deps",
srcs = all_deps,
+ **private_kwargs
)
- all_markdown_files = []
+ # Key: label to bzl. Value: label to markdown.
+ bzl_md = {}
+
for src in srcs:
+ stardoc_target_name = name + "-" + _sanitize_label_as_filename(src)
stardoc(
- name = name + "-" + src,
- out = name + "/" + src,
+ name = stardoc_target_name,
+ out = name + "/" + _sanitize_label_as_filename(src) + ".md",
input = src,
deps = [":" + name + "_deps"],
func_template = func_template,
provider_template = provider_template,
rule_template = rule_template,
+ **private_kwargs
)
- all_markdown_files.append((name + "/" + src, src))
-
- native.filegroup(
- name = name + "_markdown_files",
- srcs = [target for target, _ in all_markdown_files],
- )
+ bzl_md[src] = stardoc_target_name
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,
@@ -100,6 +117,7 @@ def docs(
name + "/docs_resources/default_file.html.frag",
],
cmd = default_file_cmd,
+ **private_kwargs
)
native.genrule(
@@ -107,21 +125,24 @@ def docs(
srcs = [
"//build/bazel_common_rules/docs:index.html",
":{name}_default_file.html.frag".format(name = name),
- ":{name}_markdown_files".format(name = name),
- ],
+ ] + bzl_md.keys() + bzl_md.values(),
outs = [
name + "/root/index.html",
],
cmd = """
- $(location //build/bazel_common_rules/docs:insert_resource.py) \
- --infile $(location //build/bazel_common_rules/docs:index.html) \
- --outfile $(location {name}/root/index.html) \
- $(location :{name}_default_file.html.frag) \
- $(locations :{name}_markdown_files)
- """.format(name = name),
+ $(location //build/bazel_common_rules/docs:insert_resource.py) \\
+ --infile $(location //build/bazel_common_rules/docs:index.html) \\
+ --outfile $(location {name}/root/index.html) \\
+ default_file.html.frag:$(location :{name}_default_file.html.frag) \\
+ {bzl_md}
+ """.format(
+ name = name,
+ bzl_md = " ".join(["$(location {}):$(location {})".format(bzl, md) for bzl, md in bzl_md.items()]),
+ ),
tools = [
"//build/bazel_common_rules/docs:insert_resource.py",
],
+ **kwargs
)
native.genrule(
@@ -137,6 +158,7 @@ python3 -m http.server 8080
'
chmod +x $(location {name}/run_server.sh)
""".format(name = name),
+ **private_kwargs
)
native.sh_binary(
@@ -145,9 +167,11 @@ python3 -m http.server 8080
":{name}_run_server.sh".format(name = name),
],
data = [":" + name],
+ **kwargs
)
copy_to_dist_dir(
name = name + "_dist",
data = [":" + name],
+ **kwargs
)
diff --git a/docs/insert_resource.py b/docs/insert_resource.py
index 02260c2..09c9ef1 100755
--- a/docs/insert_resource.py
+++ b/docs/insert_resource.py
@@ -14,9 +14,10 @@ def main(infile, outfile, resources):
magic = inlines.index(MAGIC)
outlines = inlines[:magic]
- for resource_name in resources:
+ for resource in resources:
+ resource_name, path = resource.rsplit(":", 1)
outlines.append('<div hidden class="embedded_resource" id="{}-res">\n'.format(os.path.basename(resource_name)))
- with open(resource_name, 'rb') as resource:
+ with open(path, 'rb') as resource:
# Resources need to be base64 encoded. For example, the resource file may be in
# markdown format:
# `<name>`
@@ -35,6 +36,6 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description=main.__doc__)
parser.add_argument("--infile", required=True, type=argparse.FileType('r'), help="input file")
parser.add_argument("--outfile", required=True, type=argparse.FileType('w'), help="output file")
- parser.add_argument("resources", nargs='+', help="resource files")
+ parser.add_argument("resources", metavar="NAME:PATH", nargs='+', help="resource files")
args = parser.parse_args()
main(**vars(args))