Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow additional tools for sphinx_docs #1831

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion sphinxdocs/private/sphinx.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def sphinx_docs(
formats,
strip_prefix = "",
extra_opts = [],
tools = [],
**kwargs):
"""Generate docs using Sphinx.

Expand Down Expand Up @@ -88,6 +89,11 @@ def sphinx_docs(
source files. e.g., given `//docs:foo.md`, stripping `docs/`
makes Sphinx see `foo.md` in its generated source directory.
extra_opts: (list[str]) Additional options to pass onto Sphinx building.
On each provided option, a location expansion is performed.
See `ctx.expand_location()`.
tools: (list[label]) Additional tools that are used by Sphinx and its plugins.
rickeylev marked this conversation as resolved.
Show resolved Hide resolved
This just makes the tools available during Sphinx execution. To locate
them, use `extra_opts` and `$(location)`.
**kwargs: (dict) Common attributes to pass onto rules.
"""
add_tag(kwargs, "@rules_python//sphinxdocs:sphinx_docs")
Expand All @@ -102,6 +108,7 @@ def sphinx_docs(
formats = formats,
strip_prefix = strip_prefix,
extra_opts = extra_opts,
tools = tools,
**kwargs
)

Expand Down Expand Up @@ -174,6 +181,10 @@ _sphinx_docs = rule(
doc = "Doc source files for Sphinx.",
),
"strip_prefix": attr.string(doc = "Prefix to remove from input file paths."),
"tools": attr.label_list(
cfg = "exec",
doc = "Additional tools that are used by Sphinx and its plugins.",
),
"_extra_defines_flag": attr.label(default = "//sphinxdocs:extra_defines"),
"_extra_env_flag": attr.label(default = "//sphinxdocs:extra_env"),
},
Expand Down Expand Up @@ -238,7 +249,8 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix):
args.add("-j", "auto") # Build in parallel, if possible
args.add("-E") # Don't try to use cache files. Bazel can't make use of them.
args.add("-a") # Write all files; don't try to detect "changed" files
args.add_all(ctx.attr.extra_opts)
for opt in ctx.attr.extra_opts:
args.add(ctx.expand_location(opt))
rickeylev marked this conversation as resolved.
Show resolved Hide resolved
args.add_all(ctx.attr._extra_defines_flag[_FlagInfo].value, before_each = "-D")
args.add(source_path)
args.add(output_dir.path)
Expand All @@ -248,11 +260,16 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix):
for v in ctx.attr._extra_env_flag[_FlagInfo].value
])

tools = []
for tool in ctx.attr.tools:
tools.append(tool[DefaultInfo].files_to_run)

ctx.actions.run(
executable = ctx.executable.sphinx,
arguments = [args],
inputs = inputs,
outputs = [output_dir],
tools = tools,
mnemonic = "SphinxBuildDocs",
progress_message = "Sphinx building {} for %{{label}}".format(format),
env = env,
Expand Down