formatters -> eval_cache

Group all the evaluation cache functions together
This commit is contained in:
zimbatm 2021-02-18 14:13:01 +01:00
parent 4e6698e9e8
commit 6a4b7997e3
No known key found for this signature in database
GPG Key ID: 71BAF6D40C1D63D7
5 changed files with 78 additions and 103 deletions

View File

@ -1,10 +1,6 @@
//! The main formatting engine logic should be in this module.
use crate::formatters::{
check::check_treefmt,
manifest::{create_manifest, read_manifest},
RootManifest,
};
use crate::eval_cache::{check_treefmt, create_manifest, read_manifest, RootManifest};
use crate::{config, customlog, CmdContext, FileMeta, CLOG};
use anyhow::{anyhow, Error, Result};
use filetime::FileTime;

View File

@ -1,13 +1,20 @@
use super::RootManifest;
//! Keep track of evaluations
use crate::{customlog, CmdContext, CLOG};
use anyhow::{anyhow, Result};
use anyhow::{anyhow, Error, Result};
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::path::PathBuf;
#[derive(Debug, Deserialize, Serialize)]
/// RootManifest
pub struct RootManifest {
/// Map of manifests config based on its formatter
pub manifest: BTreeMap<String, CmdContext>,
}
/// Create <hex(hash(path-to-treefmt))>.toml and put it in $XDG_CACHE_DIR/treefmt/eval-cache/
pub fn create_manifest(
treefmt_toml: PathBuf,
@ -88,9 +95,60 @@ fn create_hash(treefmt_toml: &PathBuf) -> Result<String> {
Ok(manifest_toml)
}
/// Checking content of cache's file and current treefmt runs
pub fn check_treefmt(
treefmt_toml: &PathBuf,
cmd_context: &[CmdContext],
cache: &RootManifest,
) -> Result<Vec<CmdContext>> {
let cache_context = cache.manifest.values();
let results = cmd_context.iter().zip(cache_context);
let cache_context: Vec<CmdContext> = results
.clone()
.map(|(new, old)| {
Ok(CmdContext {
command: new.command.clone(),
options: new.options.clone(),
metadata: if new.command != old.command || new.options != old.options {
// If either the command or the options have changed, invalidate old entries
new.metadata.clone()
} else {
new.metadata.difference(&old.metadata).cloned().collect()
},
})
})
.filter(|c| match c {
Ok(x) => !x.metadata.is_empty(),
_ => false,
})
.collect::<Result<Vec<CmdContext>, Error>>()?;
if cache_context.iter().all(|f| f.metadata.is_empty()) {
CLOG.debug(&format!("No changes found in {}", treefmt_toml.display()));
return Ok(Vec::new());
}
CLOG.info("The following file has changed or newly added:");
for cmd in &cache_context {
if !cmd.metadata.is_empty() {
for p in &cmd.metadata {
CLOG.info(&format!(
" - {} last modification time: {}",
p.path.display(),
p.mtimes
));
}
}
}
// return Err(anyhow!("treefmt failed to run."));
Ok(cache_context)
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
/// Every same path produce same hash
#[test]
@ -100,4 +158,20 @@ mod tests {
assert_eq!(create_hash(&file_path)?, treefmt_hash);
Ok(())
}
/// Every same path produce same hash
#[test]
fn test_check_treefmt() -> Result<()> {
let treefmt_path = PathBuf::from(r"examples/monorepo/treefmt.toml");
let cache: RootManifest = RootManifest {
manifest: BTreeMap::new(),
};
let cmd_context: Vec<CmdContext> = Vec::new();
assert_eq!(
check_treefmt(&treefmt_path, &cmd_context, &cache)?,
cmd_context
);
Ok(())
}
}

View File

@ -1,77 +0,0 @@
use super::RootManifest;
use crate::{CmdContext, CLOG};
use anyhow::{Error, Result};
use std::path::PathBuf;
use std::vec::Vec;
/// Checking content of cache's file and current treefmt runs
pub fn check_treefmt(
treefmt_toml: &PathBuf,
cmd_context: &[CmdContext],
cache: &RootManifest,
) -> Result<Vec<CmdContext>> {
let cache_context = cache.manifest.values();
let results = cmd_context.iter().zip(cache_context);
let cache_context: Vec<CmdContext> = results
.clone()
.map(|(new, old)| {
Ok(CmdContext {
command: new.command.clone(),
options: new.options.clone(),
metadata: if new.command != old.command || new.options != old.options {
// If either the command or the options have changed, invalidate old entries
new.metadata.clone()
} else {
new.metadata.difference(&old.metadata).cloned().collect()
},
})
})
.filter(|c| match c {
Ok(x) => !x.metadata.is_empty(),
_ => false,
})
.collect::<Result<Vec<CmdContext>, Error>>()?;
if cache_context.iter().all(|f| f.metadata.is_empty()) {
CLOG.debug(&format!("No changes found in {}", treefmt_toml.display()));
return Ok(Vec::new());
}
CLOG.info("The following file has changed or newly added:");
for cmd in &cache_context {
if !cmd.metadata.is_empty() {
for p in &cmd.metadata {
CLOG.info(&format!(
" - {} last modification time: {}",
p.path.display(),
p.mtimes
));
}
}
}
// return Err(anyhow!("treefmt failed to run."));
Ok(cache_context)
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::BTreeMap;
/// Every same path produce same hash
#[test]
fn test_check_treefmt() -> Result<()> {
let treefmt_path = PathBuf::from(r"examples/monorepo/treefmt.toml");
let cache: RootManifest = RootManifest {
manifest: BTreeMap::new(),
};
let cmd_context: Vec<CmdContext> = Vec::new();
assert_eq!(
check_treefmt(&treefmt_path, &cmd_context, &cache)?,
cmd_context
);
Ok(())
}
}

View File

@ -1,18 +0,0 @@
//! Functionality related to installing prebuilt binaries
#![deny(missing_docs)]
/// File checking utility
pub mod check;
/// Manifest configuration
pub mod manifest;
use crate::CmdContext;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Deserialize, Serialize)]
/// RootManifest
pub struct RootManifest {
/// Map of manifests config based on its formatter
pub manifest: BTreeMap<String, CmdContext>,
}

View File

@ -5,7 +5,7 @@ pub mod command;
pub mod config;
pub mod customlog;
pub mod engine;
pub mod formatters;
pub mod eval_cache;
use customlog::CustomLogOutput;
use serde::{Deserialize, Serialize};