From 6a7db8c701c329e66b135dd7c2b9beebf4c77fa6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jan 2020 16:01:41 +0100 Subject: Share cache cleaning logic between OSes --- xtask/src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++++- xtask/src/main.rs | 6 +++++- 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'xtask/src') diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index b76278635..e46c21db7 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -9,7 +9,7 @@ mod ast_src; use anyhow::Context; use std::{ - env, + env, fs, path::{Path, PathBuf}, process::{Command, Stdio}, }; @@ -101,3 +101,41 @@ pub fn run_fuzzer() -> Result<()> { run("rustup run nightly -- cargo fuzz run parser", "./crates/ra_syntax") } + +/// Cleans the `./target` dir after the build such that only +/// dependencies are cached on CI. +pub fn run_pre_cache() -> 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 entry in Path::new("./target/debug").read_dir()? { + let entry = entry?; + if entry.file_type().map(|it| it.is_file()).ok() == Some(true) { + // Can't delete yourself on windows :-( + if !entry.path().ends_with("xtask.exe") { + rm_rf(&entry.path())? + } + } + } + + fs::remove_file("./target/.rustc_info.json")?; + let to_delete = ["ra_", "heavy_test"]; + for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() { + for entry in Path::new(dir).read_dir()? { + let entry = entry?; + if to_delete.iter().any(|&it| entry.path().display().to_string().contains(it)) { + rm_rf(&entry.path())? + } + } + } + + Ok(()) +} + +fn rm_rf(path: &Path) -> Result<()> { + if path.is_file() { fs::remove_file(path) } else { fs::remove_dir_all(path) } + .with_context(|| format!("failed to remove {:?}", path)) +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 9309b2fbd..053453e6e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -14,7 +14,7 @@ use pico_args::Arguments; use xtask::{ codegen::{self, Mode}, install::{ClientOpt, InstallCmd, ServerOpt}, - pre_commit, run_clippy, run_fuzzer, run_rustfmt, Result, + pre_commit, run_clippy, run_fuzzer, run_pre_cache, run_rustfmt, Result, }; fn main() -> Result<()> { @@ -88,6 +88,10 @@ FLAGS: args.finish()?; run_fuzzer() } + "pre-cache" => { + args.finish()?; + run_pre_cache() + } _ => { eprintln!( "\ -- cgit v1.2.3