use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{Context, Result, bail};
use crate::cli::DisplayOptions;
use crate::color;
use crate::executor::{Executor, ExecutorOptions};
use super::Builder;
impl Builder {
pub fn clean(&self, verbose: bool) -> Result<()> {
println!("{}", color::bold("Cleaning build artifacts..."));
let processors = self.create_processors()?;
let graph = self.build_graph_for_clean_with_processors(&processors)?;
let executor = Executor::new(&processors, ExecutorOptions {
parallel: 1,
verbose: false,
display_opts: DisplayOptions::minimal(),
batch_size: None,
explain: false,
retry: 0,
}, Arc::new(std::sync::atomic::AtomicBool::new(false)));
let stats = executor.clean(&graph, verbose)?;
let mut dirs_removed = 0usize;
let out_dir = std::path::PathBuf::from("out");
if out_dir.is_dir() {
for entry in fs::read_dir(&out_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir()
&& let Ok(()) = fs::remove_dir(&path)
{
dirs_removed += 1;
if verbose {
println!("Removed empty directory: {}", path.display());
}
}
}
if let Ok(()) = fs::remove_dir(&out_dir) {
dirs_removed += 1;
if verbose {
println!("Removed empty directory: {}", out_dir.display());
}
}
}
let total_files: usize = stats.values().sum();
if total_files == 0 && dirs_removed == 0 {
println!("{}", color::dim("Clean summary: nothing to clean"));
} else {
println!("{}", color::bold("Clean summary:"));
let sorted_stats: std::collections::BTreeMap<_, _> = stats.iter().collect();
for (proc, count) in &sorted_stats {
println!(" {}: {} file(s)", proc, count);
}
if dirs_removed > 0 {
println!(" {} empty dir(s) removed", dirs_removed);
}
println!("{}", color::green(&format!(
"Total: {} file(s) removed",
total_files,
)));
}
Ok(())
}
pub fn distclean(&self) -> Result<()> {
println!("{}", color::bold("Removing build directories..."));
let rsconstruct_dir = std::path::Path::new(".rsconstruct");
if rsconstruct_dir.exists() {
fs::remove_dir_all(rsconstruct_dir)
.context("Failed to remove .rsconstruct/ directory")?;
println!("Removed {}", rsconstruct_dir.display());
}
let out_dir = std::path::Path::new("out");
if out_dir.exists() {
fs::remove_dir_all(out_dir)
.context("Failed to remove out/ directory")?;
println!("Removed {}", out_dir.display());
}
println!("{}", color::green("Distclean completed!"));
Ok(())
}
pub fn hardclean(&self) -> Result<()> {
use std::process::Command;
if !std::path::Path::new(".git").exists() {
bail!("Not a git repository. Hardclean requires a git repository.");
}
println!("{}", color::bold("Running git clean -qffxd..."));
let output = Command::new("git")
.args(["clean", "-qffxd"])
.output()
.context("Failed to run git clean")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
bail!("git clean failed:\n{}", stderr);
}
println!("{}", color::green("Hardclean completed!"));
Ok(())
}
pub fn clean_unknown(&self, force: bool, verbose: bool, respect_gitignore: bool) -> Result<()> {
use ignore::WalkBuilder;
use std::process::Command;
if !std::path::Path::new(".git").exists() {
bail!("Not a git repository. clean unknown requires a git repository.");
}
let processors = self.create_processors()?;
let graph = self.build_graph_for_clean_with_processors(&processors)?;
let mut rsconstruct_outputs: HashSet<PathBuf> = HashSet::new();
let mut rsconstruct_output_dirs: Vec<PathBuf> = Vec::new();
for product in graph.products() {
for output in &product.outputs {
rsconstruct_outputs.insert(output.clone());
}
if let Some(ref dir) = product.output_dir {
rsconstruct_output_dirs.push(dir.as_ref().clone());
}
}
let output = Command::new("git")
.args(["ls-files", "--cached"])
.output()
.context("Failed to run git ls-files")?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
bail!("git ls-files failed:\n{}", stderr);
}
let git_tracked: HashSet<PathBuf> = String::from_utf8_lossy(&output.stdout)
.lines()
.map(PathBuf::from)
.collect();
let mut unknown_files: Vec<PathBuf> = Vec::new();
let walker = WalkBuilder::new(".")
.hidden(false)
.git_ignore(respect_gitignore)
.git_global(respect_gitignore)
.git_exclude(respect_gitignore)
.build();
for entry in walker {
let entry = match entry {
Ok(e) => e,
Err(_) => continue,
};
if !entry.file_type().is_some_and(|ft| ft.is_file()) {
continue;
}
let path = entry.path().strip_prefix("./").unwrap_or(entry.path());
let path = PathBuf::from(path);
if path.starts_with(".git") || path.starts_with(".rsconstruct") {
continue;
}
if git_tracked.contains(&path) {
continue;
}
if rsconstruct_outputs.contains(&path) {
continue;
}
if rsconstruct_output_dirs.iter().any(|dir| path.starts_with(dir)) {
continue;
}
unknown_files.push(path);
}
unknown_files.sort();
if unknown_files.is_empty() {
println!("{}", color::green("No unknown files found."));
return Ok(());
}
if force {
println!("{}", color::bold("Removing unknown files..."));
let mut removed = 0usize;
for path in &unknown_files {
if let Err(e) = fs::remove_file(path) {
eprintln!(" Failed to remove {}: {}", path.display(), e);
} else {
if verbose {
println!(" {}", path.display());
}
removed += 1;
}
}
let mut dirs_to_check: HashSet<PathBuf> = HashSet::new();
for path in &unknown_files {
let mut current = path.parent();
while let Some(dir) = current {
if dir == std::path::Path::new("") {
break;
}
dirs_to_check.insert(dir.to_path_buf());
current = dir.parent();
}
}
let mut sorted_dirs: Vec<PathBuf> = dirs_to_check.into_iter().collect();
sorted_dirs.sort_by_key(|b| std::cmp::Reverse(b.components().count()));
let mut dirs_removed = 0usize;
for dir in &sorted_dirs {
if fs::remove_dir(dir).is_ok() {
dirs_removed += 1;
}
}
let mut summary = format!("Removed {} unknown file(s)", removed);
if dirs_removed > 0 {
summary.push_str(&format!(", {} empty dir(s)", dirs_removed));
}
println!("{}", color::green(&summary));
} else {
println!("{}", color::bold(&format!("Found {} unknown file(s):", unknown_files.len())));
for path in &unknown_files {
println!(" {}", path.display());
}
println!();
println!("{}", color::dim("This is a dry run. Run without --dry-run to delete them."));
}
Ok(())
}
}