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