forked from Cutieguwu/raven
I think I finished it...
This commit is contained in:
@@ -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
|
||||
.prey_lock
|
||||
.clone()
|
||||
.unwrap()
|
||||
.classes
|
||||
.iter()
|
||||
.map(|mut *class| &mut class)
|
||||
.collect());
|
||||
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());
|
||||
}
|
||||
|
||||
for mut tracked in self.prey_lock.unwrap().classes {
|
||||
if !tracked.is_updated()? {
|
||||
targets.push(&mut tracked);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(targets)
|
||||
Ok(self
|
||||
.prey_lock
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.classes
|
||||
.iter()
|
||||
.filter_map(|tracked| {
|
||||
if tracked.is_updated().is_ok_and(|v| v == true) {
|
||||
Some(tracked)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.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"))?;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user