| 1 |
use super::command_prelude::*; |
| 2 |
use crate::get_book_dir; |
| 3 |
use anyhow::Context; |
| 4 |
use anyhow::Result; |
| 5 |
use mdbook_driver::MDBook; |
| 6 |
use std::mem::take; |
| 7 |
use std::path::PathBuf; |
| 8 |
use std::{fmt, fs}; |
| 9 |
|
| 10 |
// Create clap subcommand arguments |
| 11 |
pub fn make_subcommand() -> Command { |
| 12 |
Command::new("clean") |
| 13 |
.about("Deletes a built book") |
| 14 |
.arg_dest_dir() |
| 15 |
.arg_root_dir() |
| 16 |
} |
| 17 |
|
| 18 |
// Clean command implementation |
| 19 |
pub fn execute(args: &ArgMatches) -> Result<()> { |
| 20 |
let book_dir = get_book_dir(args); |
| 21 |
let book = MDBook::load(book_dir)?; |
| 22 |
|
| 23 |
let dir_to_remove = match args.get_one::<PathBuf>("dest-dir") { |
| 24 |
Some(dest_dir) => std::env::current_dir() |
| 25 |
.expect("current dir should be valid") |
| 26 |
.join(dest_dir), |
| 27 |
None => book.root.join(&book.config.build.build_dir), |
| 28 |
}; |
| 29 |
|
| 30 |
let removed = Clean::new(&dir_to_remove)?; |
| 31 |
println!("{removed}"); |
| 32 |
|
| 33 |
Ok(()) |
| 34 |
} |
| 35 |
|
| 36 |
/// Formats a number of bytes into a human readable SI-prefixed size. |
| 37 |
/// Returns a tuple of `(quantity, units)`. |
| 38 |
pub fn human_readable_bytes(bytes: u64) -> (f32, &'static str) { |
| 39 |
static UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; |
| 40 |
let bytes = bytes as f32; |
| 41 |
let i = ((bytes.log2() / 10.0) as usize).min(UNITS.len() - 1); |
| 42 |
(bytes / 1024_f32.powi(i as i32), UNITS[i]) |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Debug)] |
| 46 |
pub struct Clean { |
| 47 |
num_files_removed: u64, |
| 48 |
num_dirs_removed: u64, |
| 49 |
total_bytes_removed: u64, |
| 50 |
} |
| 51 |
|
| 52 |
impl Clean { |
| 53 |
fn new(dir: &PathBuf) -> Result<Clean> { |
| 54 |
let mut files = vec![dir.clone()]; |
| 55 |
let mut children = Vec::new(); |
| 56 |
let mut num_files_removed = 0; |
| 57 |
let mut num_dirs_removed = 0; |
| 58 |
let mut total_bytes_removed = 0; |
| 59 |
|
| 60 |
if dir.exists() { |
| 61 |
while !files.is_empty() { |
| 62 |
for file in files { |
| 63 |
if let Ok(meta) = file.metadata() { |
| 64 |
// Note: This can over-count bytes removed for hard-linked |
| 65 |
// files. It also under-counts since it only counts the exact |
| 66 |
// byte sizes and not the block sizes. |
| 67 |
total_bytes_removed += meta.len(); |
| 68 |
} |
| 69 |
if file.is_file() { |
| 70 |
num_files_removed += 1; |
| 71 |
} else if file.is_dir() { |
| 72 |
num_dirs_removed += 1; |
| 73 |
for entry in fs::read_dir(file)? { |
| 74 |
children.push(entry?.path()); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
files = take(&mut children); |
| 79 |
} |
| 80 |
fs::remove_dir_all(&dir).with_context(|| "Unable to remove the build directory")?; |
| 81 |
} |
| 82 |
|
| 83 |
Ok(Clean { |
| 84 |
num_files_removed, |
| 85 |
num_dirs_removed, |
| 86 |
total_bytes_removed, |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl fmt::Display for Clean { |
| 92 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 93 |
write!(f, "Removed ")?; |
| 94 |
match (self.num_files_removed, self.num_dirs_removed) { |
| 95 |
(0, 0) => write!(f, "0 files")?, |
| 96 |
(0, 1) => write!(f, "1 directory")?, |
| 97 |
(0, 2..) => write!(f, "{} directories", self.num_dirs_removed)?, |
| 98 |
(1, _) => write!(f, "1 file")?, |
| 99 |
(2.., _) => write!(f, "{} files", self.num_files_removed)?, |
| 100 |
} |
| 101 |
|
| 102 |
if self.total_bytes_removed == 0 { |
| 103 |
Ok(()) |
| 104 |
} else { |
| 105 |
// Don't show a fractional number of bytes. |
| 106 |
if self.total_bytes_removed < 1024 { |
| 107 |
write!(f, ", {}B total", self.total_bytes_removed) |
| 108 |
} else { |
| 109 |
let (bytes, unit) = human_readable_bytes(self.total_bytes_removed); |
| 110 |
write!(f, ", {bytes:.2}{unit} total") |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |