Skip to content

Commit

Permalink
Add support for Bzlmod
Browse files Browse the repository at this point in the history
Input .bzl files are turned into runfiles of a custom Stardoc binary,
which allows reusing the Java runfiles library to load the files while
taking repository mappings into account.
  • Loading branch information
fmeum committed Nov 17, 2022
1 parent 6f274e9 commit 8d01c6a
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 231 deletions.
7 changes: 7 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module(
name = "stardoc",
version = "0.5.3",
compatibility_level = 1,
)
bazel_dep(name = "bazel_skylib", version = "1.2.1")
bazel_dep(name = "rules_java", version = "5.3.5")
44 changes: 22 additions & 22 deletions docs/stardoc_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ Starlark rule for stardoc: a documentation generator tool written in Java.
## stardoc

<pre>
stardoc(<a href="#stardoc-name">name</a>, <a href="#stardoc-aspect_template">aspect_template</a>, <a href="#stardoc-deps">deps</a>, <a href="#stardoc-format">format</a>, <a href="#stardoc-func_template">func_template</a>, <a href="#stardoc-header_template">header_template</a>, <a href="#stardoc-input">input</a>, <a href="#stardoc-out">out</a>,
<a href="#stardoc-provider_template">provider_template</a>, <a href="#stardoc-renderer">renderer</a>, <a href="#stardoc-rule_template">rule_template</a>, <a href="#stardoc-semantic_flags">semantic_flags</a>, <a href="#stardoc-stardoc">stardoc</a>, <a href="#stardoc-symbol_names">symbol_names</a>)
stardoc(<a href="#stardoc-name">name</a>, <a href="#stardoc-input">input</a>, <a href="#stardoc-out">out</a>, <a href="#stardoc-deps">deps</a>, <a href="#stardoc-format">format</a>, <a href="#stardoc-symbol_names">symbol_names</a>, <a href="#stardoc-semantic_flags">semantic_flags</a>, <a href="#stardoc-stardoc">stardoc</a>, <a href="#stardoc-renderer">renderer</a>,
<a href="#stardoc-aspect_template">aspect_template</a>, <a href="#stardoc-func_template">func_template</a>, <a href="#stardoc-header_template">header_template</a>, <a href="#stardoc-provider_template">provider_template</a>, <a href="#stardoc-rule_template">rule_template</a>, <a href="#stardoc-kwargs">kwargs</a>)
</pre>


Generates documentation for exported skylark rule definitions in a target starlark file.

This rule is an experimental replacement for the existing skylark_doc rule.


**ATTRIBUTES**


| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="stardoc-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="stardoc-aspect_template"></a>aspect_template | The input file template for generating documentation of aspects. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:templates/markdown_tables/aspect.vm</code> |
| <a id="stardoc-deps"></a>deps | A list of bzl_library dependencies which the input depends on. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
| <a id="stardoc-format"></a>format | The format of the output file. Valid values: 'markdown' or 'proto'. | String | optional | <code>"markdown"</code> |
| <a id="stardoc-func_template"></a>func_template | The input file template for generating documentation of functions. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:templates/markdown_tables/func.vm</code> |
| <a id="stardoc-header_template"></a>header_template | The input file template for the header of the output documentation. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:templates/markdown_tables/header.vm</code> |
| <a id="stardoc-input"></a>input | The starlark file to generate documentation for. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="stardoc-out"></a>out | The (markdown) file to which documentation will be output. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="stardoc-provider_template"></a>provider_template | The input file template for generating documentation of providers. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:templates/markdown_tables/provider.vm</code> |
| <a id="stardoc-renderer"></a>renderer | The location of the renderer tool. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:renderer</code> |
| <a id="stardoc-rule_template"></a>rule_template | The input file template for generating documentation of rules. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:templates/markdown_tables/rule.vm</code> |
| <a id="stardoc-semantic_flags"></a>semantic_flags | A list of canonical flags to affect Starlark semantics for the Starlark interpretter during documentation generation. This should only be used to maintain compatibility with non-default semantic flags required to use the given Starlark symbols.<br><br>For example, if <code>//foo:bar.bzl</code> does not build except when a user would specify <code>--incompatible_foo_semantic=false</code>, then this attribute should contain "--incompatible_foo_semantic=false". | List of strings | optional | <code>[]</code> |
| <a id="stardoc-stardoc"></a>stardoc | The location of the stardoc tool. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>//stardoc:stardoc</code> |
| <a id="stardoc-symbol_names"></a>symbol_names | A list of symbol names to generate documentation for. These should correspond to the names of rule definitions in the input file. If this list is empty, then documentation for all exported rule definitions will be generated. | List of strings | optional | <code>[]</code> |
**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="stardoc-name"></a>name | <p align="center"> - </p> | none |
| <a id="stardoc-input"></a>input | The starlark file to generate documentation for (mandatory). | none |
| <a id="stardoc-out"></a>out | The file to which documentation will be output (mandatory). | none |
| <a id="stardoc-deps"></a>deps | A list of bzl_library dependencies which the input depends on. | <code>[]</code> |
| <a id="stardoc-format"></a>format | The format of the output file. Valid values: 'markdown' or 'proto'. | <code>"markdown"</code> |
| <a id="stardoc-symbol_names"></a>symbol_names | A list of symbol names to generate documentation for. These should correspond to the names of rule definitions in the input file. If this list is empty, then documentation for all exported rule definitions will be generated. | <code>[]</code> |
| <a id="stardoc-semantic_flags"></a>semantic_flags | A list of canonical flags to affect Starlark semantics for the Starlark interpreter during documentation generation. This should only be used to maintain compatibility with non-default semantic flags required to use the given Starlark symbols.<br><br>For example, if <code>//foo:bar.bzl</code> does not build except when a user would specify <code>--incompatible_foo_semantic=false</code>, then this attribute should contain "--incompatible_foo_semantic=false". | <code>[]</code> |
| <a id="stardoc-stardoc"></a>stardoc | The location of the stardoc tool. | <code>Label("//stardoc:prebuilt_stardoc_binary")</code> |
| <a id="stardoc-renderer"></a>renderer | The location of the renderer tool. | <code>Label("//stardoc:renderer")</code> |
| <a id="stardoc-aspect_template"></a>aspect_template | The input file template for generating documentation of aspects | <code>Label("//stardoc:templates/markdown_tables/aspect.vm")</code> |
| <a id="stardoc-func_template"></a>func_template | The input file template for generating documentation of functions. | <code>Label("//stardoc:templates/markdown_tables/func.vm")</code> |
| <a id="stardoc-header_template"></a>header_template | The input file template for the header of the output documentation. | <code>Label("//stardoc:templates/markdown_tables/header.vm")</code> |
| <a id="stardoc-provider_template"></a>provider_template | The input file template for generating documentation of providers. | <code>Label("//stardoc:templates/markdown_tables/provider.vm")</code> |
| <a id="stardoc-rule_template"></a>rule_template | The input file template for generating documentation of rules. | <code>Label("//stardoc:templates/markdown_tables/rule.vm")</code> |
| <a id="stardoc-kwargs"></a>kwargs | <p align="center"> - </p> | none |


6 changes: 3 additions & 3 deletions setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def stardoc_repositories():
http_archive,
name = "rules_java",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/releases/download/4.0.0/rules_java-4.0.0.tar.gz",
"https://github.com/bazelbuild/rules_java/releases/download/4.0.0/rules_java-4.0.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_java/releases/download/5.3.5/rules_java-5.3.5.tar.gz",
"https://github.com/bazelbuild/rules_java/releases/download/5.3.5/rules_java-5.3.5.tar.gz",
],
sha256 = "34b41ec683e67253043ab1a3d1e8b7c61e4e8edefbcad485381328c934d072fe",
sha256 = "c73336802d0b4882e40770666ad055212df4ea62cfa6edf9cb0f9d29828a0934",
)
28 changes: 3 additions & 25 deletions stardoc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ bzl_library(
srcs = ["stardoc.bzl"],
visibility = ["//visibility:public"],
deps = [
"//stardoc/private:stardoc_lib",
"@bazel_skylib//:bzl_library",
"@rules_java//java:rules",
],
)

Expand All @@ -45,38 +47,14 @@ stardoc(
deps = [":stardoc_lib"],
)

java_binary(
name = "stardoc",
jvm_flags = [
# quiet warnings from com.google.protobuf.UnsafeUtil,
# see: https://github.com/google/protobuf/issues/3781
# TODO(cparsons): Remove once Stardoc has the fix.
"-XX:+IgnoreUnrecognizedVMOptions",
"--add-opens=java.base/java.nio=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
],
main_class = "com.google.devtools.build.skydoc.SkydocMain",
runtime_deps = [
":prebuilt_stardoc_binary",
],
)

java_import(
name = "prebuilt_stardoc_binary",
jars = ["stardoc_binary.jar"],
visibility = ["//visibility:private"],
visibility = ["//visibility:public"],
)

java_binary(
name = "renderer",
jvm_flags = [
# quiet warnings from com.google.protobuf.UnsafeUtil,
# see: https://github.com/google/protobuf/issues/3781
# TODO(cparsons): Remove once Stardoc has the fix.
"-XX:+IgnoreUnrecognizedVMOptions",
"--add-opens=java.base/java.nio=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
],
main_class = "com.google.devtools.build.skydoc.renderer.RendererMain",
runtime_deps = [
":prebuilt_renderer_binary",
Expand Down
10 changes: 10 additions & 0 deletions stardoc/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "stardoc_lib",
srcs = ["stardoc.bzl"],
visibility = ["//stardoc:__pkg__"],
deps = [
"@bazel_skylib//:bzl_library",
],
)
188 changes: 188 additions & 0 deletions stardoc/private/stardoc.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Starlark rule for stardoc: a documentation generator tool written in Java."""

load("@bazel_skylib//:bzl_library.bzl", "StarlarkLibraryInfo")

def _stardoc_runfiles_impl(ctx):
runfiles = ctx.runfiles(
files = [ctx.file.input],
transitive_files = depset(transitive = [
dep[StarlarkLibraryInfo].transitive_srcs
for dep in ctx.attr.deps
]),
)
return [
DefaultInfo(runfiles = runfiles),
]

stardoc_runfiles = rule(
implementation = _stardoc_runfiles_impl,
attrs = {
"input": attr.label(
allow_single_file = [".bzl"],
),
"deps": attr.label_list(
providers = [StarlarkLibraryInfo],
),
},
)

def _stardoc_impl(ctx):
"""Implementation of the stardoc rule."""
for semantic_flag in ctx.attr.semantic_flags:
if not semantic_flag.startswith("--"):
fail("semantic_flags entry '%s' must start with '--'" % semantic_flag)
out_file = ctx.outputs.out
stardoc_args = ctx.actions.args()
stardoc_args.add("--input=" + str(ctx.file.input.owner))
stardoc_args.add("--workspace_name=" + ctx.workspace_name)
stardoc_args.add_all(
ctx.attr.symbol_names,
format_each = "--symbols=%s",
omit_if_empty = True,
)
stardoc_args.add_all(ctx.attr.semantic_flags)
stardoc = ctx.executable.stardoc

if ctx.attr.format == "proto":
stardoc_args.add("--output=" + out_file.path)
ctx.actions.run(
outputs = [out_file],
executable = stardoc,
arguments = [stardoc_args],
mnemonic = "Stardoc",
progress_message = ("Generating Starlark doc for %s" %
(ctx.label.name)),
)
elif ctx.attr.format == "markdown":
proto_file = ctx.actions.declare_file(ctx.label.name + ".raw", sibling = out_file)
stardoc_args.add("--output=" + proto_file.path)
ctx.actions.run(
outputs = [proto_file],
executable = stardoc,
arguments = [stardoc_args],
mnemonic = "Stardoc",
progress_message = ("Generating proto for Starlark doc for %s" %
(ctx.label.name)),
)
renderer_args = ctx.actions.args()
renderer_args.add("--input=" + str(proto_file.path))
renderer_args.add("--output=" + str(ctx.outputs.out.path))
renderer_args.add("--aspect_template=" + str(ctx.file.aspect_template.path))
renderer_args.add("--header_template=" + str(ctx.file.header_template.path))
renderer_args.add("--func_template=" + str(ctx.file.func_template.path))
renderer_args.add("--provider_template=" + str(ctx.file.provider_template.path))
renderer_args.add("--rule_template=" + str(ctx.file.rule_template.path))
renderer = ctx.executable.renderer
ctx.actions.run(
outputs = [out_file],
inputs = [proto_file, ctx.file.aspect_template, ctx.file.header_template, ctx.file.func_template, ctx.file.provider_template, ctx.file.rule_template],
executable = renderer,
arguments = [renderer_args],
mnemonic = "Renderer",
progress_message = ("Converting proto format of %s to markdown format" %
(ctx.label.name)),
)

# Work around default outputs not getting captured by sh_binary:
# https://github.com/bazelbuild/bazel/issues/15043.
# See discussion in https://github.com/bazelbuild/stardoc/pull/139.
outputs = [out_file]
return [DefaultInfo(files = depset(outputs), runfiles = ctx.runfiles(files = outputs))]

stardoc = rule(
_stardoc_impl,
doc = """
Generates documentation for exported skylark rule definitions in a target starlark file.
This rule is an experimental replacement for the existing skylark_doc rule.
""",
attrs = {
"input": attr.label(
doc = "The starlark file to generate documentation for.",
allow_single_file = [".bzl"],
mandatory = True,
),
"out": attr.output(
doc = "The (markdown) file to which documentation will be output.",
mandatory = True,
),
"format": attr.string(
doc = "The format of the output file. Valid values: 'markdown' or 'proto'.",
values = ["markdown", "proto"],
mandatory = True,
),
"symbol_names": attr.string_list(
doc = """
A list of symbol names to generate documentation for. These should correspond to
the names of rule definitions in the input file. If this list is empty, then
documentation for all exported rule definitions will be generated.
""",
mandatory = True,
),
"semantic_flags": attr.string_list(
doc = """
A list of canonical flags to affect Starlark semantics for the Starlark interpretter
during documentation generation. This should only be used to maintain compatibility with
non-default semantic flags required to use the given Starlark symbols.
For example, if `//foo:bar.bzl` does not build except when a user would specify
`--incompatible_foo_semantic=false`, then this attribute should contain
"--incompatible_foo_semantic=false".
""",
mandatory = True,
),
"stardoc": attr.label(
doc = "The location of the stardoc tool.",
allow_files = True,
cfg = "exec",
executable = True,
mandatory = True,
),
"renderer": attr.label(
doc = "The location of the renderer tool.",
allow_files = True,
cfg = "exec",
executable = True,
mandatory = True,
),
"aspect_template": attr.label(
doc = "The input file template for generating documentation of aspects.",
allow_single_file = [".vm"],
mandatory = True,
),
"header_template": attr.label(
doc = "The input file template for the header of the output documentation.",
allow_single_file = [".vm"],
mandatory = True,
),
"func_template": attr.label(
doc = "The input file template for generating documentation of functions.",
allow_single_file = [".vm"],
mandatory = True,
),
"provider_template": attr.label(
doc = "The input file template for generating documentation of providers.",
allow_single_file = [".vm"],
mandatory = True,
),
"rule_template": attr.label(
doc = "The input file template for generating documentation of rules.",
allow_single_file = [".vm"],
mandatory = True,
),
},
)
Binary file modified stardoc/renderer_binary.jar
Binary file not shown.
Loading

0 comments on commit 8d01c6a

Please sign in to comment.