mdbook/src/cmd/clean.rs

33 lines
851 B
Rust
Raw Normal View History

2022-07-04 23:16:31 +08:00
use super::command_prelude::*;
use crate::get_book_dir;
use anyhow::Context;
2018-07-23 12:45:01 -05:00
use mdbook::MDBook;
use std::fs;
2022-07-04 23:16:31 +08:00
use std::path::PathBuf;
2018-02-04 16:00:29 +03:00
// Create clap subcommand arguments
2022-07-04 23:16:31 +08:00
pub fn make_subcommand() -> Command {
Command::new("clean")
2018-08-02 15:48:22 -05:00
.about("Deletes a built book")
2022-07-04 23:16:31 +08:00
.arg_dest_dir()
.arg_root_dir()
2018-02-04 16:00:29 +03:00
}
// Clean command implementation
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)?;
2022-07-04 23:16:31 +08:00
let dir_to_remove = match args.get_one::<PathBuf>("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),
};
if dir_to_remove.exists() {
fs::remove_dir_all(&dir_to_remove)
.with_context(|| "Unable to remove the build directory")?;
}
2018-02-04 16:00:29 +03:00
Ok(())
}