1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::domain::Target;
use anyhow::{Context, Result};
use async_std::fs;
use async_std::path::Path;

pub async fn clean_target_output_paths(target: &Target) -> Result<()> {
    if let Some(output) = target.output() {
        for resource in &output.files {
            if resource.extensions.is_some() {
                let resource_files =
                    crate::fs::list_files_in_paths(&resource.paths, &resource.extensions).await;
                for file in resource_files {
                    fs::remove_file(&file)
                        .await
                        .with_context(|| format!("Failed to remove file {}", file.display()))?;
                }
            } else {
                for output_path in &resource.paths {
                    clean_path(output_path).await?;
                }
            }
        }
    }

    Ok(())
}

async fn clean_path(path: &Path) -> Result<()> {
    if path.exists().await {
        if path.is_file().await {
            fs::remove_file(&path)
                .await
                .with_context(|| format!("Failed to remove file {}", path.display()))?;
        } else if path.is_dir().await {
            fs::remove_dir_all(&path)
                .await
                .with_context(|| format!("Failed to remove directory {}", path.display()))?;
        } else {
            log::warn!("Failed to remove {}", path.display())
        }
    }

    Ok(())
}