Skip to content

Commit

Permalink
cli: implement the --clear-cache option
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
zimbatm committed Feb 25, 2021
1 parent 72268ac commit 3e8eefc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/command/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use directories::ProjectDirs;
use std::fs;
use std::path::PathBuf;

pub fn format_cmd(work_dir: &PathBuf, paths: &[PathBuf]) -> anyhow::Result<()> {
pub fn format_cmd(work_dir: &PathBuf, paths: &[PathBuf], clear_cache: bool) -> anyhow::Result<()> {
let proj_dirs = match ProjectDirs::from("com", "NumTide", "treefmt") {
Some(x) => x,
None => {
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn format_cmd(work_dir: &PathBuf, paths: &[PathBuf]) -> anyhow::Result<()> {
));

// Finally run the main formatter logic from the engine.
run_treefmt(&work_dir, &cache_dir, &config_file, &paths)?;
run_treefmt(&work_dir, &cache_dir, &config_file, &paths, clear_cache)?;

Ok(())
}
6 changes: 5 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct Cli {
#[structopt(long = "init")]
pub init: bool,

/// Clear the evaluation cache. Use in case the cache is not precise enough.
#[structopt(long = "clear-cache")]
pub clear_cache: bool,

/// Log verbosity is based off the number of v used
#[structopt(long = "verbose", short = "v", parse(from_occurrences))]
pub verbosity: u8,
Expand Down Expand Up @@ -63,7 +67,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
if cli.init {
init_cmd(&cli.work_dir)?
} else {
format_cmd(&cli.work_dir, &cli.paths)?
format_cmd(&cli.work_dir, &cli.paths, cli.clear_cache)?
}

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn run_treefmt(
cache_dir: &PathBuf,
treefmt_toml: &PathBuf,
paths: &[PathBuf],
clear_cache: bool,
) -> anyhow::Result<()> {
assert!(work_dir.is_absolute());
assert!(cache_dir.is_absolute());
Expand Down Expand Up @@ -78,7 +79,12 @@ pub fn run_treefmt(
));

// Load the eval cache
let cache = CacheManifest::load(&cache_dir, &treefmt_toml);
let cache = if clear_cache {
// Start with an empty cache
CacheManifest::default()
} else {
CacheManifest::load(&cache_dir, &treefmt_toml)
};
CLOG.debug(&format!("load cache: {}", start_time.elapsed().as_millis()));
// Insert the new formatter configs
let cache = cache.update_formatters(formatters.clone());
Expand Down

0 comments on commit 3e8eefc

Please sign in to comment.