Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cargos fingerprint data to trim to only install rust versions #14

Merged
merged 3 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ To clean all build files older than 30 days in the local cargo project run:
cargo sweep -t 30
```

To clean all build files not made by the currently installed (by rustup) nightly compiler:

```
cargo sweep --toolchains="nightly"
```

This can be useful if you checked that your library works on stable, but mostly develop on nightly.


To clean all build files not made by any of the currently installed (by rustup) compilers:

```
cargo sweep -i
```

This can be useful if you just updated your compilers with a `rustup update`.


To preview the results of a sweep run, which is recommended as a first step, add the `-d` flag, for instance:

```
Expand All @@ -38,7 +56,7 @@ cargo sweep -t 30 <path>

To clean everything but the latest build you will need to run it in several steps.

**DEPRICATED** This behavior can be too agressive, since cargo can skip reading files when building a near-identical build, see #2 and #11. Also, this will be replaced once `build-plan` is stabilized in cargo.
**DEPRICATED** This behavior can be too aggressive, since cargo can skip reading files when building a near-identical build, see #2 and #11. Also, this will be replaced once `build-plan` is stabilized in cargo.

```
cargo sweep -s
Expand Down
79 changes: 64 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern crate serde_json;
use clap::{App, Arg, ArgGroup, SubCommand};
use failure::Error;
use fern::colors::{Color, ColoredLevelConfig};
use rust_version::remove_not_built_with;
use std::{
env,
fs::remove_file,
Expand All @@ -22,6 +23,7 @@ use std::{
};
use walkdir::WalkDir;

mod rust_version;
mod stamp;
mod util;
use self::stamp::Timestamp;
Expand Down Expand Up @@ -55,7 +57,8 @@ fn setup_logging(verbose: bool) {
level = colors_level.color(record.level()),
message = message,
));
}).level(level)
})
.level(level)
.level_for("pretty_colored", log::LevelFilter::Trace)
.chain(std::io::stdout())
.apply()
Expand Down Expand Up @@ -97,14 +100,14 @@ fn find_cargo_projects(root: &Path) -> Vec<PathBuf> {
/// keeping only files which have been accessed within the given duration.
/// Dry specifies if files should actually be removed or not.
/// Returns a list of the deleted file/dir paths.
fn try_clean_path<'a>(
path: &'a Path,
fn try_clean_path(
path: &Path,
keep_duration: &Duration,
dry_run: bool,
) -> Result<(u64), Error> {
let mut total_disk_space = 0;
let mut target_path = path.to_path_buf();
target_path.push("target/");
target_path.push("target");
for entry in WalkDir::new(target_path.to_str().unwrap())
.min_depth(1)
.contents_first(true)
Expand All @@ -117,7 +120,7 @@ fn try_clean_path<'a>(
total_disk_space += metadata.len();
if !dry_run {
match remove_file(entry.path()) {
Ok(_) => info!("Successfuly removed: {:?}", entry.path()),
Ok(_) => info!("Successfully removed: {:?}", entry.path()),
Err(e) => warn!("Failed to remove: {:?} {}", entry.path(), e),
};
} else {
Expand All @@ -129,6 +132,7 @@ fn try_clean_path<'a>(
Ok(total_disk_space)
}

#[allow(clippy::cyclomatic_complexity)]
fn main() {
let matches = App::new("Cargo sweep")
.version("0.1")
Expand All @@ -140,42 +144,63 @@ fn main() {
Arg::with_name("verbose")
.short("v")
.help("Turn verbose information on"),
).arg(
)
.arg(
Arg::with_name("recursive")
.short("r")
.help("Apply on all projects below the given path"),
).arg(
)
.arg(
Arg::with_name("dry-run")
.short("d")
.help("Dry run which will not delete any files"),
).arg(
)
.arg(
Arg::with_name("stamp")
.short("s")
.long("stamp")
.help("Store timestamp file at the given path, is used by file option"),
).arg(
)
.arg(
Arg::with_name("file")
.short("f")
.long("file")
.help("Load timestamp file in the given path, cleaning everything older"),
).arg(
)
.arg(
Arg::with_name("installed")
.short("i")
.long("installed")
.help("Keep only artefacts made by Toolchains currently installed by rustup")
)
.arg(
Arg::with_name("toolchains")
.long("toolchains")
.value_name("toolchains")
.help("Toolchains (currently installed by rustup) that shuld have there artefacts kept.")
.takes_value(true),
)
.arg(
Arg::with_name("time")
.short("t")
.long("time")
.value_name("days")
.help("Number of days to backwards to keep")
.help("Number of days backwards to keep. If no value is set uses 30.")
.takes_value(true),
).group(
)
.group(
ArgGroup::with_name("timestamp")
.args(&["stamp", "file", "time"])
.args(&["stamp", "file", "time", "installed", "toolchains"])
.required(true),
).arg(
)
.arg(
Arg::with_name("path")
.index(1)
.value_name("path")
.help("Path to check"),
),
).get_matches();
)
.get_matches();

if let Some(matches) = matches.subcommand_matches("sweep") {
let verbose = matches.is_present("verbose");
Expand Down Expand Up @@ -207,6 +232,30 @@ fn main() {
Ok(_) => {}
Err(e) => error!("Failed to write timestamp file: {}", e),
}
return;
}

if matches.is_present("installed") || matches.is_present("toolchains") {
if matches.is_present("recursive") {
for project_path in find_cargo_projects(&path) {
match remove_not_built_with(&project_path, matches.value_of("toolchains"), dry_run) {
Ok(cleaned_amount) if dry_run => {
info!("Would clean: {}", format_bytes(cleaned_amount))
}
Ok(cleaned_amount) => info!("Cleaned {}", format_bytes(cleaned_amount)),
Err(e) => error!("Failed to clean {:?}: {}", path, e),
};
}
} else {
match remove_not_built_with(&path, matches.value_of("toolchains"), dry_run) {
Ok(cleaned_amount) if dry_run => {
info!("Would clean: {}", format_bytes(cleaned_amount))
}
Ok(cleaned_amount) => info!("Cleaned {}", format_bytes(cleaned_amount)),
Err(e) => error!("Failed to clean {:?}: {}", path, e),
};
}
return;
}

if matches.is_present("recursive") {
Expand Down
Loading