2019-05-26 02:50:41 +08:00
|
|
|
use crate::get_book_dir;
|
2020-05-21 05:32:00 +08:00
|
|
|
use anyhow::Context;
|
2018-02-04 21:00:29 +08:00
|
|
|
use clap::{App, ArgMatches, SubCommand};
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::MDBook;
|
|
|
|
use std::fs;
|
2018-02-04 21:00:29 +08:00
|
|
|
|
|
|
|
// Create clap subcommand arguments
|
|
|
|
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
|
|
|
|
SubCommand::with_name("clean")
|
2018-08-03 04:48:22 +08:00
|
|
|
.about("Deletes a built book")
|
2018-02-04 21:00:29 +08:00
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\
|
2018-09-06 23:24:42 +08:00
|
|
|
Relative paths are interpreted relative to the book's root directory.{n}\
|
|
|
|
Running this command deletes this directory.{n}\
|
|
|
|
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'",
|
2019-05-05 22:57:43 +08:00
|
|
|
)
|
|
|
|
.arg_from_usage(
|
2018-08-03 04:48:22 +08:00
|
|
|
"[dir] 'Root directory for the book{n}\
|
|
|
|
(Defaults to the Current Directory when omitted)'",
|
2018-02-04 21:00:29 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean command implementation
|
2019-05-31 00:12:33 +08:00
|
|
|
pub fn execute(args: &ArgMatches) -> mdbook::errors::Result<()> {
|
2018-02-04 21:00:29 +08:00
|
|
|
let book_dir = get_book_dir(args);
|
|
|
|
let book = MDBook::load(&book_dir)?;
|
|
|
|
|
|
|
|
let dir_to_remove = match args.value_of("dest-dir") {
|
2018-08-03 04:48:22 +08:00
|
|
|
Some(dest_dir) => dest_dir.into(),
|
2018-02-04 21:00:29 +08:00
|
|
|
None => book.root.join(&book.config.build.build_dir),
|
|
|
|
};
|
2019-10-06 03:59:34 +08:00
|
|
|
|
|
|
|
if dir_to_remove.exists() {
|
2020-05-21 05:32:00 +08:00
|
|
|
fs::remove_dir_all(&dir_to_remove)
|
|
|
|
.with_context(|| "Unable to remove the build directory")?;
|
2019-10-06 03:59:34 +08:00
|
|
|
}
|
2018-02-04 21:00:29 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|