2019-05-26 01:50:41 +07:00
|
|
|
use crate::get_book_dir;
|
2020-05-20 14:32:00 -07:00
|
|
|
use anyhow::Context;
|
2022-01-19 10:10:53 -06:00
|
|
|
use clap::{arg, App, Arg, ArgMatches};
|
2018-07-23 12:45:01 -05:00
|
|
|
use mdbook::MDBook;
|
|
|
|
|
use std::fs;
|
2018-02-04 16:00:29 +03:00
|
|
|
|
|
|
|
|
// Create clap subcommand arguments
|
2022-01-18 15:56:45 -06:00
|
|
|
pub fn make_subcommand<'help>() -> App<'help> {
|
2022-01-18 16:19:40 -06:00
|
|
|
App::new("clean")
|
2018-08-02 15:48:22 -05:00
|
|
|
.about("Deletes a built book")
|
2022-01-19 10:10:53 -06:00
|
|
|
.arg(
|
|
|
|
|
Arg::new("dest-dir")
|
|
|
|
|
.short('d')
|
|
|
|
|
.long("dest-dir")
|
|
|
|
|
.value_name("dest-dir")
|
|
|
|
|
.help(
|
|
|
|
|
"Output directory for the book{n}\
|
|
|
|
|
Relative paths are interpreted relative to the book's root directory.{n}\
|
|
|
|
|
If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.",
|
|
|
|
|
),
|
2018-02-04 16:00:29 +03:00
|
|
|
)
|
2022-01-19 10:10:53 -06:00
|
|
|
.arg(arg!([dir]
|
|
|
|
|
"Root directory for the book{n}\
|
|
|
|
|
(Defaults to the Current Directory when omitted)"
|
|
|
|
|
))
|
2018-02-04 16:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clean command implementation
|
2019-05-30 23:12:33 +07:00
|
|
|
pub fn execute(args: &ArgMatches) -> mdbook::errors::Result<()> {
|
2018-02-04 16:00:29 +03: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-02 15:48:22 -05:00
|
|
|
Some(dest_dir) => dest_dir.into(),
|
2018-02-04 16:00:29 +03:00
|
|
|
None => book.root.join(&book.config.build.build_dir),
|
|
|
|
|
};
|
2019-10-05 15:59:34 -04:00
|
|
|
|
|
|
|
|
if dir_to_remove.exists() {
|
2020-05-20 14:32:00 -07:00
|
|
|
fs::remove_dir_all(&dir_to_remove)
|
|
|
|
|
.with_context(|| "Unable to remove the build directory")?;
|
2019-10-05 15:59:34 -04:00
|
|
|
}
|
2018-02-04 16:00:29 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|