Skip to content

Commit 399d8ad

Browse files
SebSparrowHawkhoe-jo
authored andcommitted
sphinx_module add SphinxDocsLibrary depdencies
Added support for SphinxDocsLibrary so that existing input sources of that type can be added as dependencies.
1 parent 6ca6e5f commit 399d8ad

1 file changed

Lines changed: 39 additions & 20 deletions

File tree

bazel/rules/rules_score/private/sphinx_module.bzl

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
# SPDX-License-Identifier: Apache-2.0
1212
# *******************************************************************************
1313

14-
load("//bazel/rules/rules_score:providers.bzl", "SphinxModuleInfo", "SphinxNeedsInfo")
15-
1614
# ======================================================================================
1715
# Helpers
1816
# ======================================================================================
1917
load("@bazel_skylib//lib:paths.bzl", "paths")
18+
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
19+
load("@rules_python//sphinxdocs/private:sphinx_docs_library_info.bzl", "SphinxDocsLibraryInfo")
20+
load("//bazel/rules/rules_score:providers.bzl", "SphinxModuleInfo", "SphinxNeedsInfo")
2021

2122
def _create_config_py(ctx):
2223
"""Get or generate the conf.py configuration file.
@@ -149,16 +150,12 @@ def _score_html_impl(ctx):
149150
},
150151
)
151152

152-
for input_file in ctx.files.srcs:
153-
print("=> Input File: ", input_file.path)
154-
155153
source_prefix = ctx.label.name
156154
sphinx_source_files = []
157155

158-
# Materialize a file under the `_sources` dir
156+
# Materialize a file under the `_sources` dir
159157
def _relocate(source_file, dest_path = None):
160158
if not dest_path:
161-
print("$$ Short path of source file: ", source_file.short_path)
162159
dest_path = source_file.short_path.removeprefix(ctx.attr.strip_prefix)
163160

164161
dest_path = paths.join(source_prefix, dest_path)
@@ -174,29 +171,43 @@ def _score_html_impl(ctx):
174171
sphinx_source_files.append(dest_file)
175172
return dest_file
176173

174+
for dep in ctx.attr.deps:
175+
if SphinxModuleInfo in dep:
176+
modules.extend([dep[SphinxModuleInfo].html_dir])
177+
178+
for t in ctx.attr.docs_library_deps:
179+
info = t[SphinxDocsLibraryInfo]
180+
for entry in info.transitive.to_list():
181+
for original in entry.files:
182+
new_path = entry.prefix + original.short_path.removeprefix(entry.strip_prefix)
183+
_relocate(original, new_path)
184+
185+
needs_external_needs_json = ctx.actions.declare_file(ctx.label.name + "/needs_external_needs.json")
186+
187+
ctx.actions.write(
188+
output = needs_external_needs_json,
189+
content = json.encode_indent(needs_external_needs, indent = " "),
190+
)
191+
192+
config_file = _create_config_py(ctx)
177193

178194
# Sphinx only accepts a single directory to read its doc sources from.
179195
# Because plain files and generated files are in different directories,
180196
# we need to merge the two into a single directory.
181197
for orig_file in ctx.files.srcs:
182198
_relocate(orig_file)
183199

184-
relocated_index_file = "" #_relocate(ctx.attr.index.files.to_list()[0])
200+
relocated_index_file = ""
185201
for input_file in sphinx_source_files:
186202
if input_file.path.endswith("/index.rst"):
187203
relocated_index_file = input_file.path
188-
print("???? Relocated index file: ", relocated_index_file)
189-
print("!! Relocated File: ", input_file.path)
190-
191-
192-
print("Debug, index file path: ", ctx.attr.index.files.to_list()[0].path)
193204

194205
# Build HTML with external needs
195206
html_inputs = sphinx_source_files + ctx.files.needs + [config_file, needs_external_needs_json]
196207
sphinx_html_output = ctx.actions.declare_directory(ctx.label.name + "/_html")
197208
html_args = [
198209
"--index_file",
199-
relocated_index_file, #ctx.attr.index.files.to_list()[0].path.removeprefix("docs/sphinx/"),
210+
relocated_index_file,
200211
"--output_dir",
201212
sphinx_html_output.path,
202213
"--config",
@@ -266,12 +277,17 @@ _score_needs = rule(
266277

267278
_score_html = rule(
268279
implementation = _score_html_impl,
269-
attrs = dict(sphinx_rule_attrs,
270-
strip_prefix = attr.string(doc = "Prefix to remove from input file paths."),
271-
needs = attr.label_list(
272-
allow_files = True,
273-
doc = "Submodule symbols.needs targets for this module.",
274-
)),
280+
attrs = dict(
281+
sphinx_rule_attrs,
282+
strip_prefix = attr.string(doc = "Prefix to remove from input file paths."),
283+
docs_library_deps = attr.label_list(
284+
doc = "List of sphinx_docs_library targets to include as source files with prefix/strip_prefix handling.",
285+
),
286+
needs = attr.label_list(
287+
allow_files = True,
288+
doc = "Submodule symbols.needs targets for this module.",
289+
),
290+
),
275291
toolchains = ["//bazel/rules/rules_score:toolchain_type"],
276292
)
277293

@@ -284,6 +300,7 @@ def sphinx_module(
284300
srcs,
285301
index,
286302
deps = [],
303+
docs_library_deps = [],
287304
sphinx = Label("//bazel/rules/rules_score:score_build"),
288305
strip_prefix = "",
289306
testonly = False,
@@ -300,6 +317,7 @@ def sphinx_module(
300317
index: Label to index.rst file
301318
config: Label to conf.py configuration file (optional, will be auto-generated if not provided)
302319
deps: List of other sphinx_module targets this module depends on
320+
docs_library_deps: {type}`list[label]` of {obj}`sphinx_docs_library` targets.
303321
sphinx: Label to sphinx build binary (default: :sphinx_build)
304322
strip_prefix: {type}`str` A prefix to remove from the file paths of the
305323
source files. e.g., given `//sphinxdocs/docs:foo.md`, stripping `docs/` makes
@@ -321,6 +339,7 @@ def sphinx_module(
321339
srcs = srcs,
322340
index = index,
323341
deps = deps,
342+
docs_library_deps = docs_library_deps,
324343
needs = [d + "_needs" for d in deps],
325344
testonly = testonly,
326345
visibility = visibility,

0 commit comments

Comments
 (0)