rs_clean/
lib.rs

1pub mod constant;
2pub mod cmd;
3pub mod utils;
4
5use crate::cmd::Cmd;
6use crate::constant::EXCLUDE_DIR;
7use colored::*;
8use futures::future;
9use std::path::Path;
10use walkdir::WalkDir;
11
12pub async fn do_clean_all(dir: &Path, cmd_list: &Vec<Cmd<'_>>) -> u32 {
13    let entries: Vec<_> = WalkDir::new(dir)
14        .into_iter()
15        .filter_map(|e| e.ok())
16        .filter(|e| e.file_type().is_dir())
17        .collect();
18
19    let cleaning_futures: Vec<_> = entries
20        .iter()
21        .filter_map(|entry| {
22            let path = entry.path();
23            if let Some(dir_name) = path.file_name().and_then(|n| n.to_str()) {
24                if EXCLUDE_DIR.contains(&dir_name) || dir_name.starts_with('.') {
25                    return None;
26                }
27            }
28
29            let mut futures_for_dir = vec![];
30            for cmd in cmd_list.iter() {
31                if cmd
32                    .related_files
33                    .iter()
34                    .any(|file| path.join(file).exists())
35                {
36                    let path_buf = path.to_path_buf();
37                    let future = async move {
38                        println!(
39                            "{} {} {}",
40                            "Cleaning".bright_black(),
41                            path_buf.display(),
42                            format!("({})", cmd.name).blue()
43                        );
44                        if cmd.run_clean(&path_buf).await.is_ok() {
45                            1
46                        } else {
47                            0
48                        }
49                    };
50                    futures_for_dir.push(future);
51                }
52            }
53            Some(futures_for_dir)
54        })
55        .flatten()
56        .collect();
57
58    let results = future::join_all(cleaning_futures).await;
59    results.into_iter().sum()
60}