Skip to content

Commit 54a5d74

Browse files
authored
Merge pull request #2806 from ehuss/dest-dir-relative
Change CLI dest-dir to be relative to the current directory
2 parents dbb51d3 + c177081 commit 54a5d74

File tree

11 files changed

+41
-17
lines changed

11 files changed

+41
-17
lines changed

crates/mdbook-core/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl TextDirection {
366366
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
367367
#[non_exhaustive]
368368
pub struct BuildConfig {
369-
/// Where to put built artefacts relative to the book's root directory.
369+
/// Where to put built artifacts relative to the book's root directory.
370370
pub build_dir: PathBuf,
371371
/// Should non-existent markdown files specified in `SUMMARY.md` be created
372372
/// if they don't exist?

guide/src/cli/build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ your default web browser after building it.
3030
#### `--dest-dir`
3131

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

guide/src/cli/clean.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mdbook clean path/to/book
2020

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

2626
```bash

guide/src/cli/serve.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ default web browser after starting the server.
4040
#### `--dest-dir`
4141

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

guide/src/cli/watch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ your default web browser.
2323
#### `--dest-dir`
2424

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

src/cmd/build.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::command_prelude::*;
22
use crate::{get_book_dir, open};
33
use anyhow::Result;
44
use mdbook_driver::MDBook;
5-
use std::path::PathBuf;
65

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

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

2522
book.build()?;
2623

src/cmd/clean.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
2121
let book = MDBook::load(book_dir)?;
2222

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

src/cmd/command_prelude.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Helpers for building the command-line arguments for commands.
22
33
pub use clap::{Arg, ArgMatches, Command, arg};
4+
use mdbook_driver::MDBook;
45
use std::path::PathBuf;
56

67
pub trait CommandExt: Sized {
@@ -15,7 +16,7 @@ pub trait CommandExt: Sized {
1516
.value_parser(clap::value_parser!(PathBuf))
1617
.help(
1718
"Output directory for the book\n\
18-
Relative paths are interpreted relative to the book's root directory.\n\
19+
Relative paths are interpreted relative to the current directory.\n\
1920
If omitted, mdBook uses build.build-dir from book.toml \
2021
or defaults to `./book`.",
2122
),
@@ -57,3 +58,12 @@ impl CommandExt for Command {
5758
self.arg(arg)
5859
}
5960
}
61+
62+
pub fn set_dest_dir(args: &ArgMatches, book: &mut MDBook) {
63+
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
64+
let build_dir = std::env::current_dir()
65+
.expect("current dir should be valid")
66+
.join(dest_dir);
67+
book.config.build.build_dir = build_dir;
68+
}
69+
}

src/cmd/serve.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6262
book.config
6363
.set("output.html.live-reload-endpoint", LIVE_RELOAD_ENDPOINT)
6464
.expect("live-reload-endpoint update failed");
65-
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
66-
book.config.build.build_dir = dest_dir.into();
67-
}
65+
set_dest_dir(args, book);
6866
// Override site-url for local serving of the 404 file
6967
book.config.set("output.html.site-url", "/").unwrap();
7068
};

src/cmd/watch.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
3838
let mut book = MDBook::load(&book_dir)?;
3939

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

0 commit comments

Comments
 (0)