Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/mdbook-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ impl TextDirection {
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
#[non_exhaustive]
pub struct BuildConfig {
/// Where to put built artefacts relative to the book's root directory.
/// Where to put built artifacts relative to the book's root directory.
pub build_dir: PathBuf,
/// Should non-existent markdown files specified in `SUMMARY.md` be created
/// if they don't exist?
Expand Down
2 changes: 1 addition & 1 deletion guide/src/cli/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ your default web browser after building it.
#### `--dest-dir`

The `--dest-dir` (`-d`) option allows you to change the output directory for the
book. Relative paths are interpreted relative to the book's root directory. If
book. Relative paths are interpreted relative to the current directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml`, or to `./book`.

Expand Down
2 changes: 1 addition & 1 deletion guide/src/cli/clean.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mdbook clean path/to/book

The `--dest-dir` (`-d`) option allows you to override the book's output
directory, which will be deleted by this command. Relative paths are interpreted
relative to the book's root directory. If not specified it will default to the
relative to the current directory. If not specified it will default to the
value of the `build.build-dir` key in `book.toml`, or to `./book`.

```bash
Expand Down
2 changes: 1 addition & 1 deletion guide/src/cli/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ default web browser after starting the server.
#### `--dest-dir`

The `--dest-dir` (`-d`) option allows you to change the output directory for the
book. Relative paths are interpreted relative to the book's root directory. If
book. Relative paths are interpreted relative to the current directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml`, or to `./book`.

Expand Down
2 changes: 1 addition & 1 deletion guide/src/cli/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ your default web browser.
#### `--dest-dir`

The `--dest-dir` (`-d`) option allows you to change the output directory for the
book. Relative paths are interpreted relative to the book's root directory. If
book. Relative paths are interpreted relative to the current directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml`, or to `./book`.

Expand Down
5 changes: 1 addition & 4 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::command_prelude::*;
use crate::{get_book_dir, open};
use anyhow::Result;
use mdbook_driver::MDBook;
use std::path::PathBuf;

// Create clap subcommand arguments
pub fn make_subcommand() -> Command {
Expand All @@ -18,9 +17,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let mut book = MDBook::load(book_dir)?;

if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
book.config.build.build_dir = dest_dir.into();
}
set_dest_dir(args, &mut book);

book.build()?;

Expand Down
4 changes: 3 additions & 1 deletion src/cmd/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let book = MDBook::load(book_dir)?;

let dir_to_remove = match args.get_one::<PathBuf>("dest-dir") {
Some(dest_dir) => dest_dir.into(),
Some(dest_dir) => std::env::current_dir()
.expect("current dir should be valid")
.join(dest_dir),
None => book.root.join(&book.config.build.build_dir),
};

Expand Down
12 changes: 11 additions & 1 deletion src/cmd/command_prelude.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Helpers for building the command-line arguments for commands.

pub use clap::{Arg, ArgMatches, Command, arg};
use mdbook_driver::MDBook;
use std::path::PathBuf;

pub trait CommandExt: Sized {
Expand All @@ -15,7 +16,7 @@ pub trait CommandExt: Sized {
.value_parser(clap::value_parser!(PathBuf))
.help(
"Output directory for the book\n\
Relative paths are interpreted relative to the book's root directory.\n\
Relative paths are interpreted relative to the current directory.\n\
If omitted, mdBook uses build.build-dir from book.toml \
or defaults to `./book`.",
),
Expand Down Expand Up @@ -57,3 +58,12 @@ impl CommandExt for Command {
self.arg(arg)
}
}

pub fn set_dest_dir(args: &ArgMatches, book: &mut MDBook) {
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
let build_dir = std::env::current_dir()
.expect("current dir should be valid")
.join(dest_dir);
book.config.build.build_dir = build_dir;
}
}
4 changes: 1 addition & 3 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
book.config
.set("output.html.live-reload-endpoint", LIVE_RELOAD_ENDPOINT)
.expect("live-reload-endpoint update failed");
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
book.config.build.build_dir = dest_dir.into();
}
set_dest_dir(args, book);
// Override site-url for local serving of the 404 file
book.config.set("output.html.site-url", "/").unwrap();
};
Expand Down
4 changes: 1 addition & 3 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
let mut book = MDBook::load(&book_dir)?;

let update_config = |book: &mut MDBook| {
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
book.config.build.build_dir = dest_dir.into();
}
set_dest_dir(args, book);
};
update_config(&mut book);

Expand Down
19 changes: 19 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,22 @@ fn book_toml_isnt_required() {
str![[r##"<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>"##]],
);
}

// Dest dir relative path behavior.
#[test]
fn dest_dir_relative_path() {
let mut test = BookTest::from_dir("build/basic_build");
let current_dir = test.dir.join("work");
std::fs::create_dir_all(&current_dir).unwrap();
test.run("build", |cmd| {
cmd.args(&["--dest-dir", "foo", ".."])
.current_dir(&current_dir)
.expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Book building has started
[TIMESTAMP] [INFO] (mdbook_driver::mdbook): Running the html backend
[TIMESTAMP] [INFO] (mdbook_html::html_handlebars::hbs_renderer): HTML book written to `[ROOT]/work/foo`

"#]]);
});
assert!(current_dir.join("foo/index.html").exists());
}
Loading