diff --git a/extensions/mdbook/private/mdbook.bzl b/extensions/mdbook/private/mdbook.bzl index 1fac752918..8ffaeec6b4 100644 --- a/extensions/mdbook/private/mdbook.bzl +++ b/extensions/mdbook/private/mdbook.bzl @@ -4,6 +4,7 @@ MdBookInfo = provider( doc = "Information about a `mdbook` target.", fields = { "config": "File: The `book.toml` file.", + "config_dest": "String: The path of the configuration file in the staged book.", "plugins": "Depset[File]: TODO", "srcs": "Depset[File]: TODO", }, @@ -23,6 +24,15 @@ def _map_inputs(file): def _mdbook_impl(ctx): output = ctx.actions.declare_directory(ctx.label.name) + book = ctx.file.book + config_dest = "{}/{}".format(ctx.label.package, book.basename) if book != None else "{}/{}.book.toml".format(ctx.label.package, ctx.label.name) + if book == None: + book = ctx.actions.declare_file("{}.book.toml".format(ctx.label.name)) + ctx.actions.write( + output = book, + content = "", + ) + toolchain = ctx.toolchains["@rules_rust_mdbook//:toolchain_type"] plugin_paths = depset([ @@ -33,7 +43,7 @@ def _mdbook_impl(ctx): path_sep = ";" if is_windows else ":" plugin_path = path_sep.join(plugin_paths.to_list()) - inputs = depset([ctx.file.book] + ctx.files.srcs) + inputs = depset([book] + ctx.files.srcs) inputs_map_args = ctx.actions.args() inputs_map_args.use_param_file("%s", use_always = True) @@ -45,7 +55,7 @@ def _mdbook_impl(ctx): args.add(output.path) args.add(toolchain.mdbook) args.add("build") - args.add("${{pwd}}/{}".format(ctx.file.book.dirname)) + args.add("${{pwd}}/{}".format(ctx.label.package)) ctx.actions.run( mnemonic = "MdBookBuild", @@ -64,7 +74,8 @@ def _mdbook_impl(ctx): ), MdBookInfo( srcs = depset(ctx.files.srcs), - config = ctx.file.book, + config = book, + config_dest = config_dest, plugins = depset(ctx.files.plugins), ), ] @@ -74,9 +85,11 @@ mdbook = rule( doc = "Rules to create book from markdown files using `mdBook`.", attrs = { "book": attr.label( - doc = "The `book.toml` file.", + doc = ( + "The optional `book.toml` file. An empty default configuration is " + + "used when omitted." + ), allow_single_file = ["book.toml"], - mandatory = True, ), "plugins": attr.label_list( doc = ( @@ -121,12 +134,13 @@ def _mdbook_server_impl(ctx): workspace_name = ctx.workspace_name args.add("--mdbook={}".format(_rlocationpath(toolchain.mdbook, workspace_name))) - args.add("--config={}".format(_src_dest_path(book_info.config))) + args.add("--config={}".format(book_info.config_dest)) args.add("--hostname={}".format(ctx.attr.hostname)) args.add("--port={}".format(ctx.attr.port)) def _src_map(file): - return "--src={}={}".format(_rlocationpath(file, workspace_name), _src_dest_path(file)) + dest = book_info.config_dest if file == book_info.config else _src_dest_path(file) + return "--src={}={}".format(_rlocationpath(file, workspace_name), dest) # The set of files that must be staged into the workdir for `mdbook serve` to # see a consistent source tree. `book.toml` is included so that referencing it diff --git a/extensions/mdbook/test/default_config/BUILD.bazel b/extensions/mdbook/test/default_config/BUILD.bazel new file mode 100644 index 0000000000..0b71487717 --- /dev/null +++ b/extensions/mdbook/test/default_config/BUILD.bazel @@ -0,0 +1,12 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("//:defs.bzl", "mdbook") + +mdbook( + name = "default_config", + srcs = glob(["src/**/*.md"]), +) + +build_test( + name = "default_config_test", + targets = [":default_config"], +) diff --git a/extensions/mdbook/test/default_config/src/SUMMARY.md b/extensions/mdbook/test/default_config/src/SUMMARY.md new file mode 100644 index 0000000000..7390c82896 --- /dev/null +++ b/extensions/mdbook/test/default_config/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +- [Chapter 1](./chapter_1.md) diff --git a/extensions/mdbook/test/default_config/src/chapter_1.md b/extensions/mdbook/test/default_config/src/chapter_1.md new file mode 100644 index 0000000000..07bcc558e4 --- /dev/null +++ b/extensions/mdbook/test/default_config/src/chapter_1.md @@ -0,0 +1,3 @@ +# Chapter 1 + +This book uses mdBook's default configuration.