From 0bc1030a02b7677659aa67afc7631ed1d2ff6cdc Mon Sep 17 00:00:00 2001 From: Bulat Musin <9249387+bmusin@users.noreply.github.com> Date: Sun, 4 Feb 2018 16:00:29 +0300 Subject: [PATCH] implement `clean` subcommand (#583) --- src/bin/clean.rs | 30 ++++++++++++++++++++++++++++++ src/bin/mdbook.rs | 5 ++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/bin/clean.rs diff --git a/src/bin/clean.rs b/src/bin/clean.rs new file mode 100644 index 00000000..09845193 --- /dev/null +++ b/src/bin/clean.rs @@ -0,0 +1,30 @@ +use std::fs; +use std::path::PathBuf; +use clap::{App, ArgMatches, SubCommand}; +use mdbook::MDBook; +use mdbook::errors::*; +use get_book_dir; + +// Create clap subcommand arguments +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("clean") + .about("Delete built book") + .arg_from_usage( + "-d, --dest-dir=[dest-dir] 'The directory of built book{n}(Defaults to ./book when \ + omitted)'", + ) +} + +// Clean command implementation +pub fn execute(args: &ArgMatches) -> ::mdbook::errors::Result<()> { + let book_dir = get_book_dir(args); + let book = MDBook::load(&book_dir)?; + + let dir_to_remove = match args.value_of("dest-dir") { + Some(dest_dir) => PathBuf::from(dest_dir), + None => book.root.join(&book.config.build.build_dir), + }; + fs::remove_dir_all(&dir_to_remove).chain_err(|| "Unable to remove the build directory")?; + + Ok(()) +} diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index d8bd487a..a7424fe6 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -19,6 +19,7 @@ use env_logger::Builder; use mdbook::utils; pub mod build; +pub mod clean; pub mod init; pub mod test; #[cfg(feature = "serve")] @@ -44,7 +45,8 @@ fn main() { at: https://github.com/rust-lang-nursery/mdBook") .subcommand(init::make_subcommand()) .subcommand(build::make_subcommand()) - .subcommand(test::make_subcommand()); + .subcommand(test::make_subcommand()) + .subcommand(clean::make_subcommand()); #[cfg(feature = "watch")] let app = app.subcommand(watch::make_subcommand()); @@ -55,6 +57,7 @@ fn main() { let res = match app.get_matches().subcommand() { ("init", Some(sub_matches)) => init::execute(sub_matches), ("build", Some(sub_matches)) => build::execute(sub_matches), + ("clean", Some(sub_matches)) => clean::execute(sub_matches), #[cfg(feature = "watch")] ("watch", Some(sub_matches)) => watch::execute(sub_matches), #[cfg(feature = "serve")]