I think I finished it...
This commit is contained in:
28
Cargo.lock
generated
28
Cargo.lock
generated
@@ -155,26 +155,6 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "const_format"
|
||||
version = "0.2.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7faa7469a93a566e9ccc1c73fe783b4a65c274c5ace346038dca9c39fe0030ad"
|
||||
dependencies = [
|
||||
"const_format_proc_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "const_format_proc_macros"
|
||||
version = "0.2.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d57c2eccfb16dbac1f4e61e206105db5820c9d26c3c472bc17c774259ef7744"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.10.0"
|
||||
@@ -383,13 +363,6 @@ version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "path"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"const_format",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pathsub"
|
||||
version = "0.1.1"
|
||||
@@ -442,7 +415,6 @@ dependencies = [
|
||||
"fs",
|
||||
"io",
|
||||
"java",
|
||||
"path",
|
||||
"toml",
|
||||
]
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ license = "MIT"
|
||||
repository = "https://gitea.cutieguwu.ca/Cutieguwu/raven"
|
||||
|
||||
publish = false
|
||||
test = true
|
||||
|
||||
[workspace.dependencies]
|
||||
#
|
||||
@@ -21,7 +20,6 @@ fs = { path = "crates/fs" }
|
||||
io = { path = "crates/io" }
|
||||
java = { path = "crates/java" }
|
||||
core = { path = "crates/core" }
|
||||
path = { path = "crates/path" }
|
||||
pom = { path = "crates/pom" }
|
||||
raven = { path = "crates/raven" }
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ description = "Raven's CLI"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies.clap]
|
||||
version = "4.5"
|
||||
|
||||
@@ -19,7 +19,7 @@ pub enum Command {
|
||||
New {
|
||||
#[clap(flatten)]
|
||||
type_: ProjectFlag,
|
||||
name: String,
|
||||
project_name: String,
|
||||
},
|
||||
/// Create a new raven project in an existing directory
|
||||
Init,
|
||||
@@ -49,7 +49,7 @@ pub struct Assertions {
|
||||
assertions: bool,
|
||||
}
|
||||
|
||||
impl Into<bool> for Assertions {
|
||||
impl Into<bool> for &Assertions {
|
||||
fn into(self) -> bool {
|
||||
self.assertions
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ description = "Raven's core, including metadata tooling and resources"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
derive_more.workspace = true
|
||||
|
||||
@@ -45,6 +45,6 @@ impl From<PackageHandler> for Dependency {
|
||||
}
|
||||
|
||||
/// TODO: This is just a placeholder at present.
|
||||
fn is_url<S: ToString>(path: S) -> bool {
|
||||
fn is_url<S: ToString>(_path: S) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use derive_more::{Display, From};
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, From, Display)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
pub mod class;
|
||||
pub mod dependency;
|
||||
pub mod error;
|
||||
|
||||
@@ -5,8 +5,8 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::dependency::Dependency;
|
||||
use crate::meta::Meta;
|
||||
use crate::prelude::Dependency;
|
||||
use crate::workspace::Workspace;
|
||||
|
||||
pub const F_NEST_TOML: &str = "Nest.toml";
|
||||
@@ -30,6 +30,11 @@ impl Nest {
|
||||
}
|
||||
|
||||
pub fn write<P: AsRef<Path>>(&self, path: P) -> crate::Result<()> {
|
||||
let mut path = path.as_ref().to_path_buf();
|
||||
if path.is_dir() {
|
||||
path = path.join(F_NEST_TOML);
|
||||
}
|
||||
|
||||
Ok(OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
|
||||
@@ -19,8 +19,8 @@ pub struct PackageHandler {
|
||||
impl PackageHandler {
|
||||
const DIR_JAVA: &str = "java/";
|
||||
|
||||
pub fn new<P: AsRef<Path>>(package_root: P, target_dir: P) -> crate::Result<Self> {
|
||||
let package_root = package_root.as_ref().to_path_buf();
|
||||
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());
|
||||
|
||||
Ok(Self {
|
||||
prey: Prey::try_from(package_root.join(F_PREY_TOML))?,
|
||||
@@ -42,32 +42,32 @@ impl PackageHandler {
|
||||
self.prey.version()
|
||||
}
|
||||
|
||||
pub fn get_update_targets(&mut self) -> crate::Result<Vec<&mut Class>> {
|
||||
let mut targets = vec![];
|
||||
|
||||
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.
|
||||
|
||||
self.prey_lock = Some(PreyLock::from(expand_files(Self::DIR_JAVA)?));
|
||||
return Ok(self
|
||||
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
|
||||
.clone()
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.classes
|
||||
.iter()
|
||||
.map(|mut *class| &mut class)
|
||||
.collect());
|
||||
.filter_map(|tracked| {
|
||||
if tracked.is_updated().is_ok_and(|v| v == true) {
|
||||
Some(tracked)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
for mut tracked in self.prey_lock.unwrap().classes {
|
||||
if !tracked.is_updated()? {
|
||||
targets.push(&mut tracked);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(targets)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::class::Class;
|
||||
use crate::meta::Meta;
|
||||
use crate::package::Package;
|
||||
use crate::prelude::Dependency;
|
||||
|
||||
pub const F_PREY_TOML: &str = "Prey.toml";
|
||||
pub const F_PREY_LOCK: &str = "Prey.lock";
|
||||
@@ -21,6 +19,15 @@ pub struct Prey {
|
||||
}
|
||||
|
||||
impl Prey {
|
||||
pub fn new<S: ToString>(name: S) -> Self {
|
||||
Self {
|
||||
package: Package {
|
||||
entry_point: PathBuf::from(""),
|
||||
},
|
||||
meta: Meta::new(name),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn entry_point(&self) -> PathBuf {
|
||||
self.package.entry_point.clone()
|
||||
}
|
||||
@@ -56,12 +63,12 @@ impl TryFrom<File> for Prey {
|
||||
/// Data struct
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct PreyLock {
|
||||
pub classes: HashSet<Class>,
|
||||
pub classes: Vec<Class>,
|
||||
}
|
||||
|
||||
impl PreyLock {
|
||||
pub fn with_class(&mut self, class: Class) -> &mut Self {
|
||||
self.classes.insert(class);
|
||||
self.classes.push(class);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,9 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::Error;
|
||||
use crate::nest::{F_NEST_LOCK, F_NEST_TOML, Nest, NestLock};
|
||||
use crate::package::PackageHandler;
|
||||
use crate::prey::F_PREY_TOML;
|
||||
use crate::prey::{F_PREY_TOML, Prey};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WorkspaceHandler {
|
||||
nest: Nest,
|
||||
nest_lock: Option<NestLock>,
|
||||
@@ -56,7 +57,7 @@ impl WorkspaceHandler {
|
||||
}
|
||||
|
||||
pub fn write(&self) -> crate::Result<()> {
|
||||
self.nest.write(self.project_root.join(F_NEST_TOML))?;
|
||||
self.write_nest()?;
|
||||
|
||||
if let Option::Some(lock) = self.nest_lock.clone() {
|
||||
lock.write(self.project_root.join(F_NEST_LOCK))?;
|
||||
@@ -65,8 +66,6 @@ impl WorkspaceHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//pub fn refresh_packages(&mut self) -> crate::Result<()> {}
|
||||
|
||||
/*
|
||||
/// Future `build` method.
|
||||
pub fn compile(&self, target: Option<PathBuf>) -> crate::Result<()> {
|
||||
@@ -87,13 +86,9 @@ impl WorkspaceHandler {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn make_compiler_job<P: AsRef<Path>>(target: P) {
|
||||
// Generate dependency tree.
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn init(&mut self) -> crate::Result<()> {
|
||||
pub fn init(&mut self) -> crate::Result<&mut Self> {
|
||||
let is_empty = read_dir(self.project_root.as_path()).is_ok_and(|tree| tree.count() == 0);
|
||||
|
||||
// ORDER MATTERS. THIS MUST COME FIRST.
|
||||
@@ -103,10 +98,7 @@ impl WorkspaceHandler {
|
||||
// Make .java-version
|
||||
self.write_java_version()?;
|
||||
|
||||
// Make src/, target/, test/
|
||||
self.write_dir_tree()?;
|
||||
|
||||
if !is_empty {
|
||||
if is_empty {
|
||||
self.write_example_project()?;
|
||||
self.discover_packages()?;
|
||||
|
||||
@@ -134,30 +126,72 @@ impl WorkspaceHandler {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
// This is the naive build
|
||||
pub fn build(&mut self) -> crate::Result<()> {
|
||||
pub fn build(&mut self) -> crate::Result<&mut Self> {
|
||||
let mut targets = vec![];
|
||||
|
||||
for (_name, mut handler) in self.packages.clone() {
|
||||
for handler in self.packages.values_mut() {
|
||||
targets.append(&mut handler.get_update_targets()?);
|
||||
}
|
||||
|
||||
let compiler = java::compiler::CompilerBuilder::new()
|
||||
.class_path(Self::DIR_TARGET)
|
||||
.class_path(Self::DIR_TARGET)
|
||||
.destination(Self::DIR_TARGET)
|
||||
.build();
|
||||
|
||||
for target in targets {
|
||||
for target in targets.iter() {
|
||||
// Possibly come up with a source file handler for this?
|
||||
if let Ok(_) = compiler.clone().compile(target) {
|
||||
if let Ok(_) = compiler.clone().compile(target.path.as_path()) {
|
||||
// No, this does not run O(1)
|
||||
//target.update()?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn run<P: AsRef<Path>>(
|
||||
&mut self,
|
||||
entry_point: Option<P>,
|
||||
assertions: bool,
|
||||
) -> crate::Result<&mut Self> {
|
||||
let mut entry_point = if entry_point.is_none() {
|
||||
self.nest.default_package()
|
||||
} else {
|
||||
entry_point.unwrap().as_ref().to_path_buf()
|
||||
};
|
||||
|
||||
if !entry_point.is_file() {
|
||||
// Use is_file to skip messing with pathing for src/
|
||||
// If target is not a file (explicit entry point), check if it's a known package
|
||||
// and use that's package's default entry point.
|
||||
entry_point = entry_point.join(
|
||||
self.packages
|
||||
.get(&entry_point)
|
||||
.ok_or(Error::UnknownPackage)?
|
||||
.entry_point(),
|
||||
);
|
||||
}
|
||||
|
||||
// JRE pathing will be messed up without this.
|
||||
std::env::set_current_dir(Self::DIR_TARGET)?;
|
||||
|
||||
java::runtime::JVMBuilder::new(Self::DIR_TARGET)
|
||||
.assertions(assertions)
|
||||
.monitor(true)
|
||||
.build()
|
||||
.run(entry_point)?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn clean(&mut self) -> crate::Result<&mut Self> {
|
||||
std::fs::remove_file(self.project_root.join(F_NEST_LOCK))?;
|
||||
std::fs::remove_dir_all(Self::DIR_TARGET)?;
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Add any newly created packages.
|
||||
@@ -174,7 +208,7 @@ impl WorkspaceHandler {
|
||||
// Yes, I know this looks like shit.
|
||||
// That's because it is.
|
||||
|
||||
for file in read_dir(Self::DIR_SRC)?
|
||||
for file in read_dir(self.project_root.join(Self::DIR_SRC))?
|
||||
// Get directories
|
||||
.filter_map(|entry| {
|
||||
if entry.as_ref().is_ok_and(|entry| entry.path().is_dir()) {
|
||||
@@ -203,12 +237,22 @@ impl WorkspaceHandler {
|
||||
})
|
||||
.flatten()
|
||||
{
|
||||
let package_root =
|
||||
pathsub::sub_paths(file.as_path(), PathBuf::from(Self::DIR_SRC).as_path()).unwrap();
|
||||
let package_root = pathsub::sub_paths(
|
||||
file.as_path(),
|
||||
self.project_root.join(Self::DIR_SRC).as_path(),
|
||||
)
|
||||
.unwrap()
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf();
|
||||
|
||||
self.packages.insert(
|
||||
package_root.to_path_buf(),
|
||||
PackageHandler::new(package_root, PathBuf::from(Self::DIR_TARGET))?,
|
||||
PackageHandler::new(
|
||||
self.project_root.join(Self::DIR_SRC),
|
||||
package_root,
|
||||
self.project_root.join(Self::DIR_TARGET),
|
||||
)?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -216,15 +260,7 @@ impl WorkspaceHandler {
|
||||
}
|
||||
|
||||
fn write_nest(&self) -> crate::Result<()> {
|
||||
if let Result::Ok(mut f) = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(F_NEST_TOML)
|
||||
{
|
||||
f.write_all(toml::to_string_pretty(&self.nest)?.as_bytes())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(self.nest.write(self.project_root.clone())?)
|
||||
}
|
||||
|
||||
fn write_java_version(&self) -> crate::Result<()> {
|
||||
@@ -251,22 +287,46 @@ impl WorkspaceHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_example_project(&self) -> std::io::Result<()> {
|
||||
fn write_example_project(&self) -> crate::Result<()> {
|
||||
let main: PathBuf = PathBuf::from(Self::DIR_SRC).join("main/");
|
||||
let test: PathBuf = PathBuf::from(Self::DIR_SRC).join("test/");
|
||||
|
||||
// Make src/, target/, test/
|
||||
self.write_dir_tree()?;
|
||||
|
||||
// Make src/main/Prey.toml
|
||||
if let Result::Ok(mut f) = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(main.join(F_PREY_TOML))
|
||||
{
|
||||
f.write_all(toml::to_string_pretty(&Prey::new("main"))?.as_bytes())?;
|
||||
}
|
||||
|
||||
// Make src/main/Main.java
|
||||
if let Result::Ok(mut f) = OpenOptions::new().write(true).create_new(true).open(
|
||||
PathBuf::from(Self::DIR_SRC)
|
||||
.join("main/java/Main")
|
||||
.with_extension(JAVA_EXT_SOURCE),
|
||||
) {
|
||||
if let Result::Ok(mut f) = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(main.join("java/Main").with_extension(JAVA_EXT_SOURCE))
|
||||
{
|
||||
f.write_all(include_bytes!("../assets/Main.java"))?;
|
||||
}
|
||||
|
||||
// Make src/test/Prey.toml
|
||||
if let Result::Ok(mut f) = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(test.join(F_PREY_TOML))
|
||||
{
|
||||
f.write_all(toml::to_string_pretty(&Prey::new("test"))?.as_bytes())?;
|
||||
}
|
||||
|
||||
// Make src/test/MainTest.java
|
||||
if let Result::Ok(mut f) = OpenOptions::new().write(true).create_new(true).open(
|
||||
PathBuf::from(Self::DIR_SRC)
|
||||
.join("test/java/MainTest")
|
||||
.with_extension(JAVA_EXT_SOURCE),
|
||||
) {
|
||||
if let Result::Ok(mut f) = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(test.join("java/MainTest").with_extension(JAVA_EXT_SOURCE))
|
||||
{
|
||||
f.write_all(include_bytes!("../assets/MainTest.java"))?;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ description = "Raven's FS utilities"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
derive_more.workspace = true
|
||||
|
||||
@@ -8,7 +8,6 @@ description = "Raven's IO utilities"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
derive_more.workspace = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use derive_more::{Display, From};
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, From, Display)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -11,7 +11,6 @@ description = "Tools for interfacing with the Java Development Kit"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
bytesize.workspace = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use derive_more::{Display, From};
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, From, Display)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
[package]
|
||||
name = "path"
|
||||
version = "0.1.0"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
description = "Raven's pathing tools"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
const_format.workspace = true
|
||||
@@ -1,151 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
//TODO: Clean this up. Shouldn't need duplicated DIR_SRC consts about the workspace.
|
||||
|
||||
const DIR_SRC: &str = "src/";
|
||||
const DIR_TARGET: &str = "target/";
|
||||
|
||||
const DIR_MAIN: &str = const_format::concatcp!(DIR_SRC, "main/");
|
||||
const DIR_TEST: &str = const_format::concatcp!(DIR_SRC, "test/");
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PathHandler {
|
||||
root_path: PathBuf,
|
||||
// This is a short-living binary. This doesn't need an LRU like Moka.
|
||||
derived_path_cache: HashMap<String, PathBuf>,
|
||||
}
|
||||
|
||||
impl PathHandler {
|
||||
pub fn new(root_path: PathBuf) -> Self {
|
||||
Self::from(root_path)
|
||||
}
|
||||
|
||||
pub fn root_path(&self) -> PathBuf {
|
||||
self.root_path.clone()
|
||||
}
|
||||
|
||||
/// This is a readability helper.
|
||||
/// Make sure to set the root of this `PathHandler` to the project root.
|
||||
/// This simply calls upon the root_path() of the `PathHandler`.
|
||||
pub fn project_root(&self) -> PathBuf {
|
||||
self.root_path()
|
||||
}
|
||||
|
||||
pub fn dir_src(&mut self) -> PathBuf {
|
||||
self.get_path(DIR_SRC)
|
||||
}
|
||||
|
||||
pub fn dir_target(&mut self) -> PathBuf {
|
||||
self.get_path(DIR_TARGET)
|
||||
}
|
||||
|
||||
pub fn dir_main(&mut self) -> PathBuf {
|
||||
self.get_path(DIR_MAIN)
|
||||
}
|
||||
|
||||
pub fn dir_test(&mut self) -> PathBuf {
|
||||
self.get_path(DIR_TEST)
|
||||
}
|
||||
|
||||
/// Attempts to load from cache, else generates the path and clones it to the cache.
|
||||
/// Returns the requested path.
|
||||
fn get_path<S>(&mut self, k: S) -> PathBuf
|
||||
where
|
||||
S: ToString + AsRef<str>,
|
||||
{
|
||||
self.from_cache(k.as_ref())
|
||||
.unwrap_or_else(|| {
|
||||
self.gen_key(k.to_string(), self.root_path().join(k.to_string()));
|
||||
self.get_path(k.as_ref())
|
||||
})
|
||||
.to_path_buf()
|
||||
}
|
||||
|
||||
/// Attempts to pull the value for the given key from the cache.
|
||||
fn from_cache<S: AsRef<str>>(&self, path_key: S) -> Option<PathBuf> {
|
||||
self.derived_path_cache
|
||||
.get(path_key.as_ref())
|
||||
.and_then(|v| Some(v.to_owned()))
|
||||
}
|
||||
|
||||
/// Tries to generate a new key-value pair in the cache
|
||||
fn gen_key<P, S>(&mut self, k: S, v: P) -> Option<PathBuf>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
S: ToString,
|
||||
{
|
||||
self.derived_path_cache
|
||||
.insert(k.to_string(), v.as_ref().to_path_buf())
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> From<P> for PathHandler
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
fn from(value: P) -> Self {
|
||||
Self {
|
||||
root_path: value.as_ref().to_path_buf(),
|
||||
derived_path_cache: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait PathHandled {
|
||||
fn set_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) {}
|
||||
|
||||
fn with_path_handler(&mut self, ph: Arc<Mutex<PathHandler>>) -> &mut Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const ROOT: &str = "/root";
|
||||
|
||||
#[test]
|
||||
fn ph_get_path() {
|
||||
let root = PathBuf::from(ROOT);
|
||||
let expected = root.join(DIR_SRC);
|
||||
|
||||
let mut ph = PathHandler::from(root);
|
||||
|
||||
assert!(ph.dir_src() == expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ph_cache_gen() {
|
||||
let root = PathBuf::from(ROOT);
|
||||
let expected = root.join(DIR_SRC);
|
||||
|
||||
let ph = PathHandler::from(root);
|
||||
|
||||
assert!(
|
||||
ph.derived_path_cache
|
||||
.get(DIR_SRC)
|
||||
.is_some_and(|v| *v == expected)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ph_cache_pull() {
|
||||
let faux_path = "faux/path";
|
||||
|
||||
let mut ph = PathHandler::from("false/root");
|
||||
ph.derived_path_cache
|
||||
.insert(faux_path.to_string(), PathBuf::from(faux_path));
|
||||
|
||||
// Use the method that attempts a fallback.
|
||||
// By using a false root, this will create a different path to the injected one,
|
||||
// making it possible to determine if the cache load fails.
|
||||
//
|
||||
// I.e.
|
||||
// Expected: faux/path as PathBuf
|
||||
// Failed: false/root/faux/path as PathBuf
|
||||
assert!(ph.get_path(faux_path) == PathBuf::from(faux_path));
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ description = "Library for serializing and deserializing Maven's POM"
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
derive_more.workspace = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use derive_more::{Display, From};
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, From, Display)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -10,7 +10,6 @@ categories = ["development-tools::build-utils"]
|
||||
repository.workspace = true
|
||||
|
||||
publish.workspace = true
|
||||
test.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
@@ -20,5 +19,4 @@ core.workspace = true
|
||||
fs.workspace = true
|
||||
io.workspace = true
|
||||
java.workspace = true
|
||||
path.workspace = true
|
||||
toml.workspace = true
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
pub fn in_path<S: AsRef<str>>(binary: S) -> Result<bool, std::env::VarError> {
|
||||
std::env::var("PATH").and_then(|paths| {
|
||||
Ok(paths
|
||||
.split(":")
|
||||
.map(|p| PathBuf::from(p).join(binary.as_ref()))
|
||||
.any(|p| p.exists()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_project_root() -> anyhow::Result<PathBuf> {
|
||||
nest::locate_nest().context(
|
||||
"Attempted to find Nest.toml, but it could not be located.\n
|
||||
It's likely that a call for get_project_root occurred before runtime checks were ran.",
|
||||
)
|
||||
}
|
||||
@@ -1,68 +1,37 @@
|
||||
mod env;
|
||||
mod manager;
|
||||
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use core::prelude::WorkspaceHandler;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use cli::{CLI_ARGS, Command};
|
||||
use java::{FN_JAVA_VERSION, JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
|
||||
//use nest::prelude::{Class, F_NEST_LOCK, F_NEST_TOML, Nest, NestLock, Prey, PreyLock};
|
||||
//use path::{PathHandled, PathHandler};
|
||||
|
||||
use anyhow::{Context, anyhow};
|
||||
use bytesize::ByteSize;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
// Ensure that ph is constructed with the assumption that it is at the project root.
|
||||
let mut ph = match CLI_ARGS.command.clone() {
|
||||
Command::Init => PathHandler::new(std::env::current_dir()?),
|
||||
Command::New { name, .. } => PathHandler::new(std::env::current_dir()?.join(name)),
|
||||
_ => PathHandler::new(crate::env::get_project_root()?),
|
||||
};
|
||||
|
||||
// Ensure that Nest.toml exists in the way functions need.
|
||||
// Init does not need one, but it's easier to deal with the minor unnecessary computation
|
||||
// of running the default contrustor, thand to fight the compiler.
|
||||
let mut nest = match CLI_ARGS.command {
|
||||
Command::Build | Command::Run { .. } => Nest::try_from(ph.project_root().join(F_NEST_TOML))
|
||||
.map_err(|err| {
|
||||
anyhow!(
|
||||
"No {} found in project directory: {}.\n{}",
|
||||
F_NEST_TOML,
|
||||
ph.project_root().display(),
|
||||
err.to_string()
|
||||
)
|
||||
})?,
|
||||
_ => Nest::default(),
|
||||
};
|
||||
|
||||
match CLI_ARGS.command.clone() {
|
||||
Command::Init => init(ph)?,
|
||||
Command::New { name, .. } => {
|
||||
new(name.to_owned())?;
|
||||
init(ph)?;
|
||||
// type_ is yet unused.
|
||||
if let Command::New { project_name, .. } = &CLI_ARGS.command {
|
||||
new(project_name.to_owned())?;
|
||||
}
|
||||
Command::Build => {
|
||||
build(&mut ph, &mut nest)?;
|
||||
|
||||
let project_root = std::env::current_dir()?;
|
||||
|
||||
let mut wh: WorkspaceHandler = match &CLI_ARGS.command {
|
||||
Command::New { .. } | Command::Init => WorkspaceHandler::new(project_root),
|
||||
_ => WorkspaceHandler::load(project_root),
|
||||
}
|
||||
.map_err(|err| anyhow!(err))?;
|
||||
|
||||
dbg!();
|
||||
match &CLI_ARGS.command {
|
||||
Command::New { .. } | Command::Init => wh.init(),
|
||||
Command::Build => wh.build(),
|
||||
Command::Run {
|
||||
entry_point,
|
||||
assertions,
|
||||
} => {
|
||||
build(&mut ph, &mut nest)?;
|
||||
run(
|
||||
&mut ph,
|
||||
entry_point.unwrap_or(nest.workspace.default_package),
|
||||
assertions.into(),
|
||||
)?;
|
||||
}
|
||||
Command::Test { assertions } => {
|
||||
test(&mut ph, assertions.into())?;
|
||||
}
|
||||
Command::Clean => clean(&mut ph),
|
||||
} => wh
|
||||
.build()
|
||||
.map_err(|err| anyhow!(err))?
|
||||
.run(entry_point.to_owned(), assertions.into()),
|
||||
Command::Clean => wh.clean(),
|
||||
Command::Test { .. } => unimplemented!(),
|
||||
}
|
||||
.map_err(|err| anyhow!(err))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -75,24 +44,3 @@ fn new(project_name: String) -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run<P: AsRef<Path>>(
|
||||
ph: &mut PathHandler,
|
||||
entry_point: P,
|
||||
assertions: bool,
|
||||
) -> anyhow::Result<(Option<String>, Option<String>)> {
|
||||
// JRE pathing will be messed up without this.
|
||||
std::env::set_current_dir(ph.dir_target())?;
|
||||
|
||||
java::runtime::JVMBuilder::new(ph.dir_target())
|
||||
.assertions(assertions)
|
||||
.monitor(true)
|
||||
.build()
|
||||
.run(entry_point)
|
||||
.map_err(|err| anyhow!(err))
|
||||
}
|
||||
|
||||
fn clean(ph: &mut PathHandler) {
|
||||
let _ = std::fs::remove_file(ph.project_root().join(F_NEST_LOCK));
|
||||
let _ = std::fs::remove_dir_all(ph.dir_target());
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use nest::prelude::{Nest, NestLock};
|
||||
use path::PathHandler;
|
||||
|
||||
pub struct ProjectManager {
|
||||
ph: PathHandler,
|
||||
nest: Nest,
|
||||
nest_lock: NestLock,
|
||||
// HashSet<Crates { prey, prey_lock }>
|
||||
}
|
||||
1
demo/.java-version
Normal file
1
demo/.java-version
Normal file
@@ -0,0 +1 @@
|
||||
21
|
||||
8
demo/Nest.toml
Normal file
8
demo/Nest.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
dependencies = []
|
||||
|
||||
[workspace]
|
||||
default_package = "main"
|
||||
|
||||
[meta]
|
||||
name = "demo"
|
||||
version = "0.1.0"
|
||||
6
demo/src/main/Prey.toml
Normal file
6
demo/src/main/Prey.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
entry_point = ""
|
||||
|
||||
[meta]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
10
demo/src/main/java/Main.java
Normal file
10
demo/src/main/java/Main.java
Normal file
@@ -0,0 +1,10 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, world!");
|
||||
}
|
||||
|
||||
public static int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
6
demo/src/test/Prey.toml
Normal file
6
demo/src/test/Prey.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
entry_point = ""
|
||||
|
||||
[meta]
|
||||
name = "test"
|
||||
version = "0.1.0"
|
||||
10
demo/src/test/java/MainTest.java
Normal file
10
demo/src/test/java/MainTest.java
Normal file
@@ -0,0 +1,10 @@
|
||||
public class MainTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
testAdd();
|
||||
}
|
||||
|
||||
public static void testAdd() {
|
||||
assert Main.add(2, 2) == 4;
|
||||
}
|
||||
}
|
||||
BIN
demo/target/Main.class
Normal file
BIN
demo/target/Main.class
Normal file
Binary file not shown.
BIN
demo/target/MainTest.class
Normal file
BIN
demo/target/MainTest.class
Normal file
Binary file not shown.
Reference in New Issue
Block a user