blob: deacbf9af90c6d1dedfde42fdafa16e24ba233ba [file] [log] [blame]
James Kuszmaulac0912d2024-05-21 15:56:59 -07001load("//tools/build_rules:clean_dep.bzl", "clean_dep")
2
James Kuszmaul31a7f602022-10-04 16:56:24 -07003def _mkdocs_impl(ctx):
4 output_file = ctx.actions.declare_file(ctx.attr.name + ".tar")
5 build_args = ctx.actions.args()
6 build_args.add("build")
7 site_subdir = "site"
8 build_args.add("-f")
9 build_args.add_all(ctx.files.config)
10 build_args.add("--site-dir")
11 build_args.add(site_subdir)
12 site_dir = ctx.files.config[0].dirname + "/" + site_subdir
13 ctx.actions.run(
14 mnemonic = "MkDocsBuild",
15 executable = ctx.executable._mkdocs,
16 arguments = [build_args],
17 inputs = ctx.files.srcs + ctx.files.config,
18 env = {"OUTPUT": output_file.path, "SITE_DIR": site_dir},
19 outputs = [output_file],
20 )
21 return [DefaultInfo(files = depset([output_file]))]
22
23_mkdocs = rule(
24 implementation = _mkdocs_impl,
25 attrs = {
26 "srcs": attr.label_list(
27 allow_files = [".md"],
28 doc = "A list of markdown files to generate docs for.",
29 ),
30 "config": attr.label(
31 doc = "mkdocs.yaml configuration file to use for the documentation.",
32 allow_files = [".yaml"],
33 mandatory = True,
34 ),
James Kuszmaulac0912d2024-05-21 15:56:59 -070035 "_mkdocs": attr.label(default = Label("//documentation:mkdocs_bin"), executable = True, cfg = "exec"),
James Kuszmaul31a7f602022-10-04 16:56:24 -070036 },
37)
38
39def mkdocs(name, srcs, config, **kwargs):
40 """Bazel rule to build and serve mkdocs documentation.
41
42 Build a tarball of the HTML files to be served by building "name", or get
43 the functionality of `mkdocs serve` by doing a bazel run on "name.serve".
44
45 Args:
46 name: name of the rule.
47 srcs: A list of markdown files to generate documentation from.
48 config: A .yaml specifying the mkdocs configuration to use.
49 See https://www.mkdocs.org/user-guide/configuration/
50 Note that mkdocs does not allow the mkdocs.yaml configuration to be
51 in the same folder as the markdown files (canonically, if the
52 mkdocs.yaml is at foo/mkdocs.yaml, the markdown files will be
53 at foo/docs/*.md).
54 """
55 _mkdocs(name = name, srcs = srcs, config = config, **kwargs)
56 native.py_binary(
57 name = name + ".serve",
James Kuszmaulac0912d2024-05-21 15:56:59 -070058 srcs = [clean_dep("//documentation:mkdocs_bin.py")],
James Kuszmaul31a7f602022-10-04 16:56:24 -070059 main = "mkdocs_bin.py",
60 args = ["serve", "-f", "$(location %s)" % (config,)],
61 deps = ["@pip//mkdocs"],
62 data = srcs + [config],
63 **kwargs
64 )