Speed up brute force building with Prey.lock hashing and caching.
This commit is contained in:
@@ -1,30 +1,35 @@
|
||||
use std::hash::Hash;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use fs::expand_files;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::class::Class;
|
||||
use crate::prey::{F_PREY_LOCK, F_PREY_TOML, Prey, PreyLock};
|
||||
|
||||
pub const DIR_JAVA: &str = "java/";
|
||||
|
||||
/// Hashing is only based off the Prey.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct PackageHandler {
|
||||
prey: Prey,
|
||||
prey_lock: Option<PreyLock>,
|
||||
prey_lock: PreyLock,
|
||||
/// Path relative to WORKSPACE/src
|
||||
package_root: PathBuf,
|
||||
target_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl PackageHandler {
|
||||
const DIR_JAVA: &str = "java/";
|
||||
|
||||
pub fn new<P: AsRef<Path>>(src_dir: P, package_root: P, target_dir: P) -> crate::Result<Self> {
|
||||
let package_root = src_dir.as_ref().join(package_root.as_ref());
|
||||
let prey_lock = if let Ok(prey_lock) = PreyLock::try_from(package_root.join(F_PREY_LOCK)) {
|
||||
prey_lock
|
||||
} else {
|
||||
PreyLock::new(package_root.clone(), package_root.join(DIR_JAVA))?
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
prey: Prey::try_from(package_root.join(F_PREY_TOML))?,
|
||||
prey_lock: PreyLock::try_from(package_root.join(F_PREY_LOCK)).ok(),
|
||||
prey_lock,
|
||||
package_root,
|
||||
target_dir: target_dir.as_ref().to_path_buf(),
|
||||
})
|
||||
@@ -42,32 +47,26 @@ impl PackageHandler {
|
||||
self.prey.version()
|
||||
}
|
||||
|
||||
pub fn get_update_targets(&mut self) -> crate::Result<Vec<&Class>> {
|
||||
if self.prey_lock.is_none() {
|
||||
// Try to pass a reference to the class so that there's mutability of the object
|
||||
// available at the workspace level instead of parsing all the way down the
|
||||
// tree. How I do this, idk. My brain is friend from a few days of JS.
|
||||
pub fn prey_lock(&mut self) -> &mut PreyLock {
|
||||
&mut self.prey_lock
|
||||
}
|
||||
|
||||
self.prey_lock = Some(PreyLock::from(expand_files(
|
||||
self.package_root.join(Self::DIR_JAVA),
|
||||
)?));
|
||||
return Ok(self.prey_lock.as_ref().unwrap().classes.iter().collect());
|
||||
}
|
||||
|
||||
Ok(self
|
||||
.prey_lock
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.classes
|
||||
pub fn get_outdated<P: AsRef<Path>>(&self, class_path: P) -> Vec<Class> {
|
||||
self.prey_lock
|
||||
.clone()
|
||||
.classes()
|
||||
.iter()
|
||||
.filter_map(|tracked| {
|
||||
if tracked.is_updated().is_ok_and(|v| v == true) {
|
||||
Some(tracked)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.filter(|class| {
|
||||
class
|
||||
.is_updated(class_path.as_ref())
|
||||
.is_ok_and(|b| b == false)
|
||||
})
|
||||
.collect())
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn write_lock(&self) -> crate::Result<()> {
|
||||
self.prey_lock.write(self.package_root.as_path())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user