aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/pre_cache.rs
diff options
context:
space:
mode:
authorDmitry <[email protected]>2020-08-18 13:37:22 +0100
committerDmitry <[email protected]>2020-08-18 13:37:22 +0100
commite18748ed152989953e39492a6b44f8001267ce5f (patch)
tree300dc7ce2998d9521319ff76f4df6ec2d165d0a1 /xtask/src/pre_cache.rs
parent73315c9168901ef6d676f017daaa9b4976380c03 (diff)
parentb8dfc331abbfce6aad0c248c91c57bd9890a668f (diff)
Merge remote-tracking branch 'rust-analyzer/master'
Diffstat (limited to 'xtask/src/pre_cache.rs')
-rw-r--r--xtask/src/pre_cache.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/xtask/src/pre_cache.rs b/xtask/src/pre_cache.rs
new file mode 100644
index 000000000..47ba6ba24
--- /dev/null
+++ b/xtask/src/pre_cache.rs
@@ -0,0 +1,80 @@
1use std::{
2 fs::FileType,
3 path::{Path, PathBuf},
4};
5
6use anyhow::Result;
7
8use crate::not_bash::{fs2, rm_rf};
9
10pub struct PreCacheCmd;
11
12impl PreCacheCmd {
13 /// Cleans the `./target` dir after the build such that only
14 /// dependencies are cached on CI.
15 pub fn run(self) -> Result<()> {
16 let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
17 if !slow_tests_cookie.exists() {
18 panic!("slow tests were skipped on CI!")
19 }
20 rm_rf(slow_tests_cookie)?;
21
22 for path in read_dir("./target/debug", FileType::is_file)? {
23 // Can't delete yourself on windows :-(
24 if !path.ends_with("xtask.exe") {
25 rm_rf(&path)?
26 }
27 }
28
29 fs2::remove_file("./target/.rustc_info.json")?;
30
31 let to_delete = read_dir("./crates", FileType::is_dir)?
32 .into_iter()
33 .map(|path| path.file_name().unwrap().to_string_lossy().replace('-', "_"))
34 .collect::<Vec<_>>();
35
36 for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() {
37 for path in read_dir(dir, |_file_type| true)? {
38 if path.ends_with("xtask.exe") {
39 continue;
40 }
41 let file_name = path.file_name().unwrap().to_string_lossy();
42 let (stem, _) = match rsplit_once(&file_name, '-') {
43 Some(it) => it,
44 None => {
45 rm_rf(path)?;
46 continue;
47 }
48 };
49 let stem = stem.replace('-', "_");
50 if to_delete.contains(&stem) {
51 rm_rf(path)?;
52 }
53 }
54 }
55
56 Ok(())
57 }
58}
59fn read_dir(path: impl AsRef<Path>, cond: impl Fn(&FileType) -> bool) -> Result<Vec<PathBuf>> {
60 read_dir_impl(path.as_ref(), &cond)
61}
62
63fn read_dir_impl(path: &Path, cond: &dyn Fn(&FileType) -> bool) -> Result<Vec<PathBuf>> {
64 let mut res = Vec::new();
65 for entry in path.read_dir()? {
66 let entry = entry?;
67 let file_type = entry.file_type()?;
68 if cond(&file_type) {
69 res.push(entry.path())
70 }
71 }
72 Ok(res)
73}
74
75fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> {
76 let mut split = haystack.rsplitn(2, delim);
77 let suffix = split.next()?;
78 let prefix = split.next()?;
79 Some((prefix, suffix))
80}