aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/pre_cache.rs
blob: b456224fd692c4a5d9791b7236b1d6a055c59f56 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::{
    fs::FileType,
    path::{Path, PathBuf},
};

use anyhow::Result;
use xshell::rm_rf;

use crate::flags;

impl flags::PreCache {
    /// Cleans the `./target` dir after the build such that only
    /// dependencies are cached on CI.
    pub(crate) fn run(self) -> Result<()> {
        let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
        if !slow_tests_cookie.exists() {
            panic!("slow tests were skipped on CI!")
        }
        rm_rf(slow_tests_cookie)?;

        for path in read_dir("./target/debug", FileType::is_file)? {
            // Can't delete yourself on windows :-(
            if !path.ends_with("xtask.exe") {
                rm_rf(&path)?
            }
        }

        rm_rf("./target/.rustc_info.json")?;

        let to_delete = read_dir("./crates", FileType::is_dir)?
            .into_iter()
            .map(|path| path.file_name().unwrap().to_string_lossy().replace('-', "_"))
            .collect::<Vec<_>>();

        for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() {
            for path in read_dir(dir, |_file_type| true)? {
                if path.ends_with("xtask.exe") {
                    continue;
                }
                let file_name = path.file_name().unwrap().to_string_lossy();
                let (stem, _) = match rsplit_once(&file_name, '-') {
                    Some(it) => it,
                    None => {
                        rm_rf(path)?;
                        continue;
                    }
                };
                let stem = stem.replace('-', "_");
                if to_delete.contains(&stem) {
                    rm_rf(path)?;
                }
            }
        }

        Ok(())
    }
}
fn read_dir(path: impl AsRef<Path>, cond: impl Fn(&FileType) -> bool) -> Result<Vec<PathBuf>> {
    read_dir_impl(path.as_ref(), &cond)
}

fn read_dir_impl(path: &Path, cond: &dyn Fn(&FileType) -> bool) -> Result<Vec<PathBuf>> {
    let mut res = Vec::new();
    for entry in path.read_dir()? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        if cond(&file_type) {
            res.push(entry.path())
        }
    }
    Ok(res)
}

fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
    let mut split = haystack.rsplitn(2, delim);
    let suffix = split.next()?;
    let prefix = split.next()?;
    Some((prefix, suffix))
}