Skip to main content

turtle_build/tool/
clean_dead.rs

1use crate::{context::Context, ir::Config};
2use core::error::Error;
3use futures::future::try_join_all;
4
5/// Cleans dead outputs.
6pub async fn clean_dead(context: &Context, config: &Config) -> Result<(), Box<dyn Error>> {
7    try_join_all(
8        context
9            .database()
10            .get_outputs()?
11            .iter()
12            .map(|output| remove_output(context, config, output)),
13    )
14    .await?;
15
16    Ok(())
17}
18
19async fn remove_output(
20    context: &Context,
21    config: &Config,
22    output: &str,
23) -> Result<(), Box<dyn Error>> {
24    if config.outputs().contains_key(output) {
25        return Ok(());
26    } else if let Ok(metadata) = context.file_system().metadata(output.as_ref()).await
27        && metadata.is_file()
28    {
29        context.file_system().remove_file(output.as_ref()).await?;
30    }
31
32    Ok(())
33}