use anyhow::anyhow;
use super::*;
#[derive(Parser, Debug)]
pub struct LeoClean {}
impl Command for LeoClean {
type Input = ();
type Output = ();
fn log_span(&self) -> Span {
tracing::span!(tracing::Level::INFO, "Leo")
}
fn prelude(&self, _: Context) -> Result<Self::Input> {
Ok(())
}
fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
match context.resolve_targets()? {
Some(targets) => {
for target in &targets {
let member_name = target.file_name().and_then(|n| n.to_str()).unwrap_or("?");
if targets.len() > 1 {
println!("\n--- workspace member '{member_name}' ---");
}
clean_package(context.with_path(target.clone()))?;
}
Ok(())
}
None => clean_package(context),
}
}
}
fn clean_package(context: Context) -> Result<()> {
let path = context.dir()?;
let manifest_path = path.join(leo_package::MANIFEST_FILENAME);
if !manifest_path.exists() {
return Err(anyhow!(
"{} doesn't exist - this doesn't appear to be a Leo package.",
leo_package::MANIFEST_FILENAME
)
.into());
}
let legacy_outputs = path.join("outputs");
if std::fs::remove_dir_all(&legacy_outputs).is_ok() {
tracing::info!("🧹 Cleaned the legacy outputs directory {}", legacy_outputs.display().to_string().dimmed());
}
let build_path = path.join(leo_package::BUILD_DIRECTORY);
if std::fs::remove_dir_all(&build_path).is_ok() {
tracing::info!("🧹 Cleaned the build directory {}", build_path.display().to_string().dimmed());
}
Ok(())
}