| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 1 | def _mdbook_html_impl(ctx): |
| 2 | # We will be generating our own book.toml based on this rule's configuration. |
| 3 | # |
| 4 | # We do this because: |
| 5 | # - The book.toml must contain a reference to the source files of the |
| 6 | # generated book. This only works as long as the source files are not |
| 7 | # generated by Bazel. |
| 8 | # - The book.toml file effectively describes a build, so it makes sense to |
| 9 | # port that over to be fully managed by the Bazel rule for mdbook. This |
| 10 | # makes things more consistent with the rest of our Bazel usage, at the |
| 11 | # expense of slightly deviating from how upstream does things. |
| 12 | # |
| 13 | # We emit the toml into `book.toml` because that's what mdbook needs. |
| 14 | # However, instead of emitting it in a subdirectory named after this |
| 15 | # target, we do that in `${target}_`. This is so that we can use the target |
| 16 | # name as a directory containing the actual generated HTML files, making |
| 17 | # the life of developers using this rule a bit easier. |
| 18 | out_book_toml = ctx.actions.declare_file(ctx.attr.name + "_/book.toml") |
| 19 | |
| 20 | # Find root of given handbook from srcs - there must be exactly one |
| 21 | # SUMMARY.md and the parent directory of that is the root. |
| 22 | summary = None |
| 23 | for f in ctx.files.srcs: |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 24 | if not f.path.endswith("/SUMMARY.md"): |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 25 | continue |
| 26 | if summary != None: |
| 27 | fail("More then one SUMMARY.md provided.") |
| 28 | summary = f |
| 29 | if summary == None: |
| 30 | fail("No SUMMARY.md provided in srcs.") |
| 31 | |
| 32 | # We now have the SUMMARY.md file from which we can figure out the source |
| 33 | # directory of the book. However, mdbook takes a source root path relative |
| 34 | # to the book.toml file, not one relative to the current working |
| 35 | # directory... Thus, we need to prepend a list of '../' elements that bring |
| 36 | # mdbook down from the book.toml location back into the workspace execution |
| 37 | # root, which is where our SUMMARY.md path is itself rooted. |
| 38 | # |
| 39 | # For example, if book.toml lives in: |
| Tim Windelschmidt | afeb4c4 | 2024-07-17 21:37:26 +0200 | [diff] [blame] | 40 | # execroot/_main/bazel-out/k8-fastbuild/bin/metropolis/handbook/handbook_/book.toml |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 41 | # Then we will need to prepend: |
| 42 | # ../../../../../../../ |
| 43 | # To get back to execroot/. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 44 | prepend = len(out_book_toml.path.split("/")) - 1 |
| 45 | src_dir_path = ("../" * prepend) + summary.dirname |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 46 | |
| 47 | # Generate book.toml. |
| 48 | # Bazel does not have a toml library. We abuse JSON encoding to get |
| 49 | # serialized list/string data as an acceptable substitute to building a |
| 50 | # fully self-standing toml serializer for Bazel. |
| 51 | book_toml_contents = [ |
| 52 | "[book]", |
| 53 | "title = {}".format(json.encode(ctx.attr.title)), |
| 54 | "authors = {}".format(json.encode(ctx.attr.authors)), |
| 55 | "language = {}".format(json.encode(ctx.attr.language)), |
| 56 | "multilingual = false", |
| 57 | "src = {}".format(json.encode(src_dir_path)), |
| 58 | ] |
| 59 | ctx.actions.write( |
| 60 | output = out_book_toml, |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 61 | content = "\n".join(book_toml_contents), |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 62 | ) |
| 63 | |
| 64 | out_dir = ctx.actions.declare_directory(ctx.attr.name) |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 65 | |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 66 | # We also have to prepend the out dir path, for the same reasons for which |
| 67 | # we prepend src_dir_path above. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 68 | out_dir_path = ("../" * prepend) + out_dir.path |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 69 | ctx.actions.run( |
| 70 | executable = ctx.executable._mdbook, |
| 71 | arguments = [ |
| 72 | "build", |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 73 | "-d", |
| 74 | out_dir_path, |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 75 | out_book_toml.dirname, |
| 76 | ], |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 77 | inputs = ctx.files.srcs + [out_book_toml], |
| 78 | outputs = [out_dir], |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 79 | ) |
| 80 | return [ |
| 81 | DefaultInfo( |
| 82 | files = depset([out_dir]), |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 83 | ), |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 84 | ] |
| 85 | |
| 86 | mdbook_html = rule( |
| 87 | doc = "Build an mdbook source root into HTML files.", |
| 88 | implementation = _mdbook_html_impl, |
| 89 | attrs = { |
| 90 | "title": attr.string( |
| 91 | doc = "The title of the generated book.", |
| 92 | ), |
| 93 | "authors": attr.string_list( |
| 94 | default = ["Monogon Project Authors"], |
| 95 | doc = "The authors of the generated book.", |
| 96 | ), |
| 97 | "language": attr.string( |
| 98 | default = "en", |
| 99 | doc = "The language of the generated book.", |
| 100 | ), |
| 101 | "srcs": attr.label_list( |
| 102 | allow_files = True, |
| 103 | doc = "The sources of the generated book. Exaclty one file must be named SUMMARY.md, and that file's location will be used to determine the root of the book sources.", |
| 104 | ), |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 105 | "_mdbook": attr.label( |
| 106 | doc = "The mdbook tool.", |
| 107 | executable = True, |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame^] | 108 | cfg = "exec", |
| Tim Windelschmidt | 223609c | 2024-01-12 22:59:20 +0100 | [diff] [blame] | 109 | default = "@crate_index//:mdbook__mdbook", |
| Serge Bazanski | aad7948 | 2021-07-02 17:40:36 +0200 | [diff] [blame] | 110 | ), |
| 111 | }, |
| 112 | ) |