Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions extensions/mdbook/private/mdbook.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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([
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -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),
),
]
Expand All @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions extensions/mdbook/test/default_config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
3 changes: 3 additions & 0 deletions extensions/mdbook/test/default_config/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Summary

- [Chapter 1](./chapter_1.md)
3 changes: 3 additions & 0 deletions extensions/mdbook/test/default_config/src/chapter_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Chapter 1

This book uses mdBook's default configuration.