Shed old reference materials.
This commit is contained in:
@@ -1,114 +0,0 @@
|
|||||||
use std::hash::{self, Hash};
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use crate::{Error, Result};
|
|
||||||
|
|
||||||
use java::{JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
|
|
||||||
use path::PathHandler;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct Class {
|
|
||||||
#[serde(skip_deserializing, skip_serializing)]
|
|
||||||
ph: Option<Arc<Mutex<PathHandler>>>,
|
|
||||||
path: PathBuf,
|
|
||||||
checksum: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Class {
|
|
||||||
pub fn new<P: AsRef<Path>>(ph: Arc<Mutex<PathHandler>>, path: P, checksum: String) -> Self {
|
|
||||||
Self {
|
|
||||||
ph: Some(ph),
|
|
||||||
path: path.as_ref().to_path_buf(),
|
|
||||||
checksum,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_file<P: AsRef<Path>>(ph: Arc<Mutex<PathHandler>>, path: P) -> Result<Self> {
|
|
||||||
let mut path = path.as_ref().to_path_buf();
|
|
||||||
if path.is_relative() {
|
|
||||||
path = path.canonicalize()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let dir_src = ph.try_lock()?.dir_src();
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
ph: Some(ph),
|
|
||||||
path: PathBuf::from(
|
|
||||||
pathsub::sub_paths(path.as_path(), dir_src.as_path()).ok_or(Error::PathSub)?,
|
|
||||||
)
|
|
||||||
.with_extension(""),
|
|
||||||
checksum: sha256::try_digest(&path)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn path(&self) -> PathBuf {
|
|
||||||
self.path.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn checksum(&self) -> String {
|
|
||||||
self.checksum.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the path such that src/ and target/ don't matter.
|
|
||||||
/// E.g., `main/Main.java` as_subpath is `Main.java`
|
|
||||||
/// allowing it to match with `target/main/Main.class`
|
|
||||||
/// because Java diregards the top level subdir in `src/`
|
|
||||||
fn subpath(&self) -> PathBuf {
|
|
||||||
let mut p = self.path.components();
|
|
||||||
p.next(); // Remove the top level dir.
|
|
||||||
p.as_path().to_path_buf()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns true if the class needs updating.
|
|
||||||
/// This may also cautionarily return true if it cannot digest the file.
|
|
||||||
pub fn is_updated(&mut self) -> Result<bool> {
|
|
||||||
// Still doesn't handle inter-dependency checks.
|
|
||||||
// Hopefully then I'll clean this horror up.
|
|
||||||
let mut ph = self
|
|
||||||
.ph
|
|
||||||
.as_ref()
|
|
||||||
.ok_or(Error::MissingPathHandler)?
|
|
||||||
.try_lock()?;
|
|
||||||
|
|
||||||
Ok(ph
|
|
||||||
.dir_target()
|
|
||||||
.join(self.subpath())
|
|
||||||
.with_extension(JAVA_EXT_CLASS)
|
|
||||||
.exists()
|
|
||||||
&& sha256::try_digest(
|
|
||||||
ph.dir_src()
|
|
||||||
.join(self.path())
|
|
||||||
.with_extension(JAVA_EXT_SOURCE),
|
|
||||||
)
|
|
||||||
.is_ok_and(|hash| self.checksum == hash))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hash for Class {
|
|
||||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.path.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for Class {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.path == other.path
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ne(&self, other: &Self) -> bool {
|
|
||||||
self.path != other.path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for Class {}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Hash, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
||||||
pub struct Dependency {
|
|
||||||
name: String,
|
|
||||||
version: String,
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
use std::sync::{MutexGuard, PoisonError, TryLockError};
|
|
||||||
|
|
||||||
use derive_more::{Display, From};
|
|
||||||
use path::PathHandler;
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, Error>;
|
|
||||||
|
|
||||||
#[derive(Debug, From, Display)]
|
|
||||||
pub enum Error {
|
|
||||||
#[from]
|
|
||||||
Io(std::io::Error),
|
|
||||||
|
|
||||||
MutexLock(String),
|
|
||||||
|
|
||||||
MutexTryLock(String),
|
|
||||||
|
|
||||||
PathSub,
|
|
||||||
|
|
||||||
#[from]
|
|
||||||
Toml(toml::de::Error),
|
|
||||||
|
|
||||||
MissingPathHandler,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<PoisonError<MutexGuard<'_, PathHandler>>> for Error {
|
|
||||||
fn from(value: PoisonError<MutexGuard<PathHandler>>) -> Self {
|
|
||||||
Self::MutexLock(value.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<TryLockError<MutexGuard<'_, PathHandler>>> for Error {
|
|
||||||
fn from(value: TryLockError<MutexGuard<PathHandler>>) -> Self {
|
|
||||||
Self::MutexTryLock(value.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
mod class;
|
|
||||||
mod dependency;
|
|
||||||
mod error;
|
|
||||||
//mod lock;
|
|
||||||
mod meta;
|
|
||||||
mod nest;
|
|
||||||
mod package;
|
|
||||||
pub mod prelude;
|
|
||||||
mod prey;
|
|
||||||
mod workspace;
|
|
||||||
|
|
||||||
pub use error::{Error, Result};
|
|
||||||
|
|
||||||
use std::io;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
pub const FN_NEST: &str = "Nest";
|
|
||||||
pub const FN_PREY: &str = "Prey";
|
|
||||||
|
|
||||||
pub const F_NEST_TOML: &str = const_format::concatcp!(FN_NEST, fs::EXT_TOML);
|
|
||||||
pub const F_NEST_LOCK: &str = const_format::concatcp!(FN_NEST, fs::EXT_LOCK);
|
|
||||||
pub const F_PREY_TOML: &str = const_format::concatcp!(FN_PREY, fs::EXT_TOML);
|
|
||||||
pub const F_PREY_LOCK: &str = const_format::concatcp!(FN_PREY, fs::EXT_LOCK);
|
|
||||||
|
|
||||||
/// Return the location of the parent `Nest.toml` as [`PathBuf`]
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Possible cases:
|
|
||||||
///
|
|
||||||
/// * `Nest.toml` cannot be located.
|
|
||||||
/// * Current working directory does not exist.
|
|
||||||
/// * There are insufficient permissions to access the current directory.
|
|
||||||
pub fn locate_nest() -> io::Result<PathBuf> {
|
|
||||||
let cwd = std::env::current_dir()?;
|
|
||||||
let mut probe = cwd.clone();
|
|
||||||
|
|
||||||
while !probe.join(F_NEST_TOML).exists() {
|
|
||||||
if !probe.pop() {
|
|
||||||
return Err(io::ErrorKind::NotFound.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(probe)
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use std::collections::hash_set::Iter;
|
|
||||||
use std::fs::OpenOptions;
|
|
||||||
use std::hash::Hash;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use path::{PathHandled, PathHandler};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
|
||||||
pub struct Lock<T> {
|
|
||||||
#[serde(skip_deserializing, skip_serializing)]
|
|
||||||
ph: Option<Arc<Mutex<PathHandler>>>,
|
|
||||||
inner: HashSet<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Lock<T>
|
|
||||||
where
|
|
||||||
T: Lockable + Status + Deserialize<'de>,
|
|
||||||
{
|
|
||||||
pub fn new(ph: Arc<Mutex<PathHandler>>, inner: HashSet<T>) -> Self {
|
|
||||||
Self {
|
|
||||||
ph: Some(ph),
|
|
||||||
inner,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load<P>(ph: Arc<Mutex<PathHandler>>, path: P) -> crate::Result<Self>
|
|
||||||
where
|
|
||||||
P: AsRef<Path>,
|
|
||||||
{
|
|
||||||
let mut path = path.as_ref().to_path_buf();
|
|
||||||
if path.is_relative() {
|
|
||||||
path = path.canonicalize()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
OpenOptions::new()
|
|
||||||
.read(true)
|
|
||||||
.open(path)?
|
|
||||||
.read_to_end(&mut buf)?;
|
|
||||||
|
|
||||||
let lock: Lock<T> = toml::from_slice(buf.as_slice())?;
|
|
||||||
|
|
||||||
Ok(lock)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self) -> crate::Result<()> {
|
|
||||||
let ph = self.ph.as_ref().ok_or(Error::MissingPathHandler)?.clone();
|
|
||||||
|
|
||||||
self.inner = self
|
|
||||||
.inner
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|mut item| {
|
|
||||||
let item = item.with_path_handler(Arc::clone(&ph));
|
|
||||||
|
|
||||||
// If the class is up-to-date and no errors occurred during the check
|
|
||||||
if item.is_updated().is_ok_and(|b| b == true) {
|
|
||||||
Some(item.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Iterates over the entries in `NestLock`.
|
|
||||||
pub fn iter(&self) -> Iter<'_, T> {
|
|
||||||
self.inner.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(&mut self, class: T) -> bool {
|
|
||||||
self.inner.insert(class)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Lockable: Hash + Eq + Clone + PathHandled {}
|
|
||||||
|
|
||||||
trait Status {
|
|
||||||
type Error;
|
|
||||||
|
|
||||||
fn is_updated(&self) -> Result<bool, Self::Error>;
|
|
||||||
}
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct Meta {
|
|
||||||
name: String,
|
|
||||||
version: semver::Version,
|
|
||||||
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
|
|
||||||
authors: Option<Vec<String>>,
|
|
||||||
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
|
|
||||||
repository: Option<String>,
|
|
||||||
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
|
|
||||||
license: Option<License>,
|
|
||||||
#[serde(skip_serializing_if = "<Option<_>>::is_none")]
|
|
||||||
license_file: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Meta {
|
|
||||||
pub fn new<S: ToString>(name: S) -> Self {
|
|
||||||
let mut meta = Self::default();
|
|
||||||
meta.name = name.to_string();
|
|
||||||
meta
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn name(&self) -> String {
|
|
||||||
self.name.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Meta {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
name: Default::default(),
|
|
||||||
version: semver::Version::new(0, 1, 0),
|
|
||||||
authors: None,
|
|
||||||
repository: None,
|
|
||||||
license: None,
|
|
||||||
license_file: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
|
|
||||||
pub enum License {
|
|
||||||
Mit,
|
|
||||||
Apache,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for License {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}",
|
|
||||||
match self {
|
|
||||||
License::Mit => "MIT",
|
|
||||||
License::Apache => "Apache",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use std::fs::{File, OpenOptions};
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use crate::Error;
|
|
||||||
use crate::dependency::Dependency;
|
|
||||||
use crate::meta::Meta;
|
|
||||||
use crate::package::Package;
|
|
||||||
use crate::workspace::Workspace;
|
|
||||||
|
|
||||||
use path::{PathHandled, PathHandler};
|
|
||||||
use pom::Pom;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
|
||||||
pub struct Nest {
|
|
||||||
pub workspace: Workspace,
|
|
||||||
pub meta: Meta,
|
|
||||||
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
|
|
||||||
pub dependencies: HashSet<Dependency>,
|
|
||||||
#[serde(default, skip_serializing_if = "Pom::is_empty")]
|
|
||||||
pub pom: Pom,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Nest {
|
|
||||||
pub fn new<S: ToString>(name: S) -> Self {
|
|
||||||
Nest {
|
|
||||||
meta: Meta::new(name),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for Nest {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
value.read_to_string(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(toml::from_str(buf.as_str())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for Nest {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
Nest::try_from(OpenOptions::new().read(true).open(value)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: See if NestLock and PreyLock can be combined into one parent struct,
|
|
||||||
// but with different generics, and merely call upon a is_updated method set
|
|
||||||
// by the generics implementing a common IsUpdated trait.
|
|
||||||
//
|
|
||||||
// Not happening any time soon. An enum could get around the issue of being unable to
|
|
||||||
// implement Deserialize on a generic.
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
|
||||||
pub struct NestLock {
|
|
||||||
#[serde(skip_serializing, skip_deserializing)]
|
|
||||||
ph: Option<Arc<Mutex<PathHandler>>>,
|
|
||||||
pub packages: HashSet<Package>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NestLock {
|
|
||||||
/// Update, retaining all packages that still exist.
|
|
||||||
///
|
|
||||||
/// Presently only supports packages under src/
|
|
||||||
pub fn update(&mut self) -> crate::Result<()> {
|
|
||||||
let dir_src = self
|
|
||||||
.ph
|
|
||||||
.as_ref()
|
|
||||||
.ok_or(Error::MissingPathHandler)?
|
|
||||||
.try_lock()?
|
|
||||||
.dir_src();
|
|
||||||
|
|
||||||
self.packages = self
|
|
||||||
.packages
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|package| {
|
|
||||||
let path = dir_src.join(package.name());
|
|
||||||
|
|
||||||
if path.exists() && path.is_dir() {
|
|
||||||
return Some(package);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for NestLock {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
|
|
||||||
value.read_to_string(&mut buf)?;
|
|
||||||
Ok(toml::from_str(buf.as_str())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for NestLock {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
NestLock::try_from(OpenOptions::new().read(true).open(&value)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PathHandled for NestLock {
|
|
||||||
fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
use std::{collections::HashSet, hash::Hash};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::dependency::Dependency;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
||||||
pub struct Package {
|
|
||||||
name: String,
|
|
||||||
version: semver::Version,
|
|
||||||
source: String,
|
|
||||||
checksum: String,
|
|
||||||
dependencies: HashSet<Dependency>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Package {
|
|
||||||
pub fn name(&self) -> String {
|
|
||||||
self.name.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Package {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
name: Default::default(),
|
|
||||||
version: semver::Version::new(0, 1, 0),
|
|
||||||
source: Default::default(),
|
|
||||||
checksum: Default::default(),
|
|
||||||
dependencies: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hash for Package {
|
|
||||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.name.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
#![allow(unused_imports)]
|
|
||||||
|
|
||||||
pub use crate::class::Class;
|
|
||||||
pub use crate::dependency::Dependency;
|
|
||||||
pub use crate::meta::Meta;
|
|
||||||
pub use crate::nest::{Nest, NestLock};
|
|
||||||
pub use crate::package::Package;
|
|
||||||
pub use crate::prey::{Prey, PreyLock};
|
|
||||||
pub use crate::workspace::Workspace;
|
|
||||||
pub use crate::{F_NEST_LOCK, F_NEST_TOML, F_PREY_LOCK, F_PREY_TOML, FN_NEST, FN_PREY};
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use std::collections::hash_set::Iter;
|
|
||||||
use std::fs::{File, OpenOptions};
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use path::{PathHandled, PathHandler};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::Error;
|
|
||||||
use crate::class::Class;
|
|
||||||
use crate::meta::Meta;
|
|
||||||
use crate::package::Package;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
|
||||||
pub struct Prey {
|
|
||||||
package: Package,
|
|
||||||
meta: Meta,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Prey {
|
|
||||||
pub fn new<S: ToString>(name: S) -> Self {
|
|
||||||
Prey {
|
|
||||||
package: (),
|
|
||||||
meta: (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for Prey {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
value.read_to_string(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(toml::from_str(buf.as_str())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for Prey {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
Prey::try_from(OpenOptions::new().read(true).open(value)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
|
||||||
pub struct PreyLock {
|
|
||||||
#[serde(skip_deserializing, skip_serializing)]
|
|
||||||
ph: Option<Arc<Mutex<PathHandler>>>,
|
|
||||||
classes: HashSet<Class>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PreyLock {
|
|
||||||
pub fn new(ph: Arc<Mutex<PathHandler>>, classes: HashSet<Class>) -> Self {
|
|
||||||
Self {
|
|
||||||
ph: Some(ph),
|
|
||||||
classes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn update(&mut self) -> crate::Result<()> {
|
|
||||||
let ph = self.ph.as_ref().ok_or(Error::MissingPathHandler)?.clone();
|
|
||||||
|
|
||||||
self.classes = self
|
|
||||||
.classes
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|mut class| {
|
|
||||||
let class = class.with_path_handler(Arc::clone(&ph));
|
|
||||||
|
|
||||||
// If the class is up-to-date and no errors occurred during the check
|
|
||||||
if class.is_updated().is_ok_and(|b| b == true) {
|
|
||||||
Some(class.to_owned())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Iterates over the entries in `PreyLock`.
|
|
||||||
pub fn iter(&self) -> Iter<'_, Class> {
|
|
||||||
self.classes.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(&mut self, class: Class) -> bool {
|
|
||||||
self.classes.insert(class)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for PreyLock {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
value.read_to_string(&mut buf)?;
|
|
||||||
|
|
||||||
Ok(toml::from_str(buf.as_str())?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for PreyLock {
|
|
||||||
type Error = Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
PreyLock::try_from(OpenOptions::new().read(true).open(value)?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PathHandled for PreyLock {
|
|
||||||
fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
|
|
||||||
self.ph = Some(ph);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
|
||||||
pub struct Workspace {
|
|
||||||
pub default_package: PathBuf,
|
|
||||||
}
|
|
||||||
@@ -1,196 +0,0 @@
|
|||||||
#![allow(dead_code)]
|
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::fs::{File, OpenOptions};
|
|
||||||
use std::hash::{self, Hash};
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::sync::LazyLock;
|
|
||||||
|
|
||||||
use crate::java::{JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
|
|
||||||
use crate::{DIR_SRC, DIR_TARGET, F_NEST_LOCK, F_NEST_TOML, PROJECT_ROOT};
|
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use semver::Version;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
pub static NEST: LazyLock<anyhow::Result<Nest>> =
|
|
||||||
LazyLock::new(|| Nest::try_from(PROJECT_ROOT.join(F_NEST_TOML.as_path())));
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct Nest {
|
|
||||||
pub package: Package,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for Nest {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
value
|
|
||||||
.read_to_string(&mut buf)
|
|
||||||
.context("Failed to read Nest")?;
|
|
||||||
|
|
||||||
toml::from_str(buf.as_str()).context("Failed to deserialize Nest")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for Nest {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
Nest::try_from(
|
|
||||||
OpenOptions::new()
|
|
||||||
.read(true)
|
|
||||||
.open(value)
|
|
||||||
.expect("Failed to load Nest.toml"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rename to Prey
|
|
||||||
// Mark as deprecated soon.
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
pub struct Package {
|
|
||||||
pub name: String,
|
|
||||||
pub version: semver::Version,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Package {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
name: String::from("MyPackage"),
|
|
||||||
version: Version::new(0, 1, 0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
|
||||||
pub struct NestLock {
|
|
||||||
pub classes: HashSet<Class>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl NestLock {
|
|
||||||
pub fn load() -> anyhow::Result<NestLock> {
|
|
||||||
NestLock::try_from(PROJECT_ROOT.join(F_NEST_LOCK.as_path()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update, retaining all classes that still exist and whose paths are still files, rather than
|
|
||||||
/// being shifted into a package.
|
|
||||||
pub fn update(&mut self) {
|
|
||||||
self.classes = self
|
|
||||||
.classes
|
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|class| {
|
|
||||||
// Paths are almost correct, except that the target classes are expecting to path
|
|
||||||
// through:
|
|
||||||
// target/main/Main.class, not target/Main.class
|
|
||||||
// target/test/MainTest.class, not target/MainTest.class
|
|
||||||
|
|
||||||
if DIR_SRC
|
|
||||||
.join(&class.path)
|
|
||||||
.with_extension(JAVA_EXT_SOURCE)
|
|
||||||
.exists()
|
|
||||||
&& DIR_TARGET
|
|
||||||
.join(&class.subpath())
|
|
||||||
.with_extension(JAVA_EXT_CLASS)
|
|
||||||
.is_file()
|
|
||||||
{
|
|
||||||
return Some(class);
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<File> for NestLock {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
|
|
||||||
fn try_from(value: File) -> Result<Self, Self::Error> {
|
|
||||||
let mut value = value;
|
|
||||||
let mut buf = String::new();
|
|
||||||
|
|
||||||
value.read_to_string(&mut buf)?;
|
|
||||||
toml::from_str(buf.as_str()).context("Failed to deserialize NestLock")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for NestLock {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
|
|
||||||
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
NestLock::try_from(
|
|
||||||
OpenOptions::new()
|
|
||||||
.read(true)
|
|
||||||
.open(&value)
|
|
||||||
.with_context(|| format!("Failed to open {}", value.display()))?,
|
|
||||||
)
|
|
||||||
.context("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
|
||||||
pub struct Class {
|
|
||||||
path: PathBuf,
|
|
||||||
checksum: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Class {
|
|
||||||
/// Returns true if the class needs updating.
|
|
||||||
/// This may also cautionarily return true if it cannot digest the file.
|
|
||||||
pub fn is_updated(&self) -> bool {
|
|
||||||
// Still doesn't handle inter-dependency checks.
|
|
||||||
|
|
||||||
DIR_TARGET
|
|
||||||
.join(self.subpath())
|
|
||||||
.with_extension(JAVA_EXT_CLASS)
|
|
||||||
.exists()
|
|
||||||
&& sha256::try_digest(DIR_SRC.join(self.path()).with_extension(JAVA_EXT_SOURCE))
|
|
||||||
.is_ok_and(|hash| self.checksum == hash)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the path such that src/ and target/ don't matter.
|
|
||||||
/// E.g., `main/Main.java` as_subpath is `Main.java`
|
|
||||||
/// allowing it to match with `target/main/Main.class`
|
|
||||||
/// because Java diregards the top level subdir in `src/`
|
|
||||||
fn subpath(&self) -> PathBuf {
|
|
||||||
let mut p = self.path.components();
|
|
||||||
p.next(); // Remove the top level dir.
|
|
||||||
p.as_path().to_path_buf()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn path(&self) -> &Path {
|
|
||||||
self.path.as_path()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hash for Class {
|
|
||||||
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
||||||
self.path.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<PathBuf> for Class {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
|
|
||||||
fn try_from(mut value: PathBuf) -> Result<Self, Self::Error> {
|
|
||||||
if value.is_relative() {
|
|
||||||
value = value
|
|
||||||
.canonicalize()
|
|
||||||
.context("Failed to canonicalize path")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
path: PathBuf::from(
|
|
||||||
pathsub::sub_paths(value.as_path(), DIR_SRC.as_path())
|
|
||||||
.context("Failed to subtract paths to get class path")?,
|
|
||||||
)
|
|
||||||
.with_extension(""),
|
|
||||||
checksum: sha256::try_digest(&value)?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user