Support proper locating of Nest.toml; Cleanup.
This commit is contained in:
@@ -35,10 +35,7 @@ pub fn get_javac_ver() -> anyhow::Result<semver::Version> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cwd<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
pub fn set_cwd<P: AsRef<Path>>(path: P) -> io::Result<()> {
|
||||||
let mut cwd = crate::PROJECT_ROOT
|
let mut cwd = crate::PROJECT_ROOT.clone();
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock Mutex")
|
|
||||||
.to_owned();
|
|
||||||
cwd.push(path.as_ref());
|
cwd.push(path.as_ref());
|
||||||
std::env::set_current_dir(cwd)
|
std::env::set_current_dir(cwd.as_path())
|
||||||
}
|
}
|
||||||
|
|||||||
65
src/main.rs
65
src/main.rs
@@ -8,7 +8,7 @@ mod nest;
|
|||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{LazyLock, Mutex};
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use cli::{CONFIG, Command};
|
use cli::{CONFIG, Command};
|
||||||
use java::{JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
|
use java::{JAVA_EXT_CLASS, JAVA_EXT_SOURCE};
|
||||||
@@ -19,19 +19,29 @@ use bytesize::ByteSize;
|
|||||||
|
|
||||||
const DIR_TARGET: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("target/"));
|
const DIR_TARGET: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("target/"));
|
||||||
const DIR_SRC: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("src/"));
|
const DIR_SRC: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("src/"));
|
||||||
/// Cannot flatten this dir out of existence due to IDEs, Linters,
|
|
||||||
/// and probably also Java expecting it to exist.
|
|
||||||
const DIR_MAIN: LazyLock<PathBuf> = LazyLock::new(|| DIR_SRC.join("main/"));
|
const DIR_MAIN: LazyLock<PathBuf> = LazyLock::new(|| DIR_SRC.join("main/"));
|
||||||
/// Once again, I would love to pull this out of src/ and into parent, but
|
|
||||||
/// I can't do that due to IDEs, linters, and probably also Java expecting it to exist in src/.
|
|
||||||
const DIR_TEST: LazyLock<PathBuf> = LazyLock::new(|| DIR_SRC.join("test/"));
|
const DIR_TEST: LazyLock<PathBuf> = LazyLock::new(|| DIR_SRC.join("test/"));
|
||||||
|
|
||||||
const F_NEST_TOML: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.toml"));
|
const F_NEST_TOML: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.toml"));
|
||||||
const F_NEST_LOCK: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.lock"));
|
const F_NEST_LOCK: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.lock"));
|
||||||
|
|
||||||
/// This may not actually be the project root.
|
pub static PROJECT_ROOT: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||||
pub static PROJECT_ROOT: LazyLock<Mutex<PathBuf>> = LazyLock::new(|| {
|
// Start from CWD
|
||||||
Mutex::new(std::env::current_dir().expect("Failed to get current working directory"))
|
let cwd = std::env::current_dir().expect("Failed to get current working directory");
|
||||||
|
let mut probe = cwd.clone();
|
||||||
|
|
||||||
|
while !probe.join(F_NEST_TOML.as_path()).exists() {
|
||||||
|
if !probe.pop() {
|
||||||
|
// This is easier than having a Result everywhere. For now.
|
||||||
|
panic!(
|
||||||
|
"No {} found in current directory or any ancestor. (cwd: {})",
|
||||||
|
F_NEST_TOML.display(),
|
||||||
|
cwd.display()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
probe
|
||||||
});
|
});
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
@@ -70,12 +80,11 @@ fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn init() -> anyhow::Result<()> {
|
fn init() -> anyhow::Result<()> {
|
||||||
let is_empty = std::fs::read_dir(PROJECT_ROOT.lock().expect("Failed to lock mutex").as_path())
|
let cwd = std::env::current_dir()?;
|
||||||
.is_ok_and(|tree| tree.count() == 0);
|
|
||||||
|
|
||||||
let project_name = PROJECT_ROOT
|
let is_empty = std::fs::read_dir(cwd.as_path()).is_ok_and(|tree| tree.count() == 0);
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
let project_name = cwd
|
||||||
.file_name()
|
.file_name()
|
||||||
.context("Invalid directory name")?
|
.context("Invalid directory name")?
|
||||||
.to_str()
|
.to_str()
|
||||||
@@ -167,18 +176,11 @@ fn init() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn new(project_name: String) -> anyhow::Result<()> {
|
fn new(project_name: String) -> anyhow::Result<()> {
|
||||||
let cwd = PROJECT_ROOT
|
let cwd = std::env::current_dir()?.join(project_name);
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(project_name);
|
|
||||||
|
|
||||||
std::fs::create_dir(&cwd)?;
|
std::fs::create_dir(&cwd)?;
|
||||||
std::env::set_current_dir(&cwd)?;
|
std::env::set_current_dir(&cwd)?;
|
||||||
|
|
||||||
// Replace PathBuf within mutex
|
|
||||||
let mut state = PROJECT_ROOT.lock().expect("Failed to lock mutex");
|
|
||||||
*state = std::env::current_dir().expect("Failed to change current working directory");
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,12 +248,7 @@ fn test(assertions: bool) -> anyhow::Result<(Option<String>, Option<String>)> {
|
|||||||
.compile(DIR_TEST.as_path())?;
|
.compile(DIR_TEST.as_path())?;
|
||||||
|
|
||||||
// Change cwd to avoid Java pathing issues.
|
// Change cwd to avoid Java pathing issues.
|
||||||
crate::env::set_cwd(
|
crate::env::set_cwd(PROJECT_ROOT.join(DIR_TARGET.as_path()))?;
|
||||||
PROJECT_ROOT
|
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(DIR_TARGET.as_path()),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
java::JVMBuilder::new()
|
java::JVMBuilder::new()
|
||||||
.assertions(assertions)
|
.assertions(assertions)
|
||||||
@@ -263,16 +260,6 @@ fn test(assertions: bool) -> anyhow::Result<(Option<String>, Option<String>)> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn clean() {
|
fn clean() {
|
||||||
let _ = std::fs::remove_file(
|
let _ = std::fs::remove_file(PROJECT_ROOT.join(F_NEST_LOCK.as_path()));
|
||||||
PROJECT_ROOT
|
let _ = std::fs::remove_dir_all(PROJECT_ROOT.join(DIR_TARGET.as_path()));
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(F_NEST_LOCK.as_path()),
|
|
||||||
);
|
|
||||||
let _ = std::fs::remove_dir_all(
|
|
||||||
PROJECT_ROOT
|
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(DIR_TARGET.as_path()),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/nest.rs
14
src/nest.rs
@@ -11,14 +11,8 @@ use anyhow::Context;
|
|||||||
use semver::Version;
|
use semver::Version;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub static NEST: LazyLock<anyhow::Result<Nest>> = LazyLock::new(|| {
|
pub static NEST: LazyLock<anyhow::Result<Nest>> =
|
||||||
Nest::try_from(
|
LazyLock::new(|| Nest::try_from(PROJECT_ROOT.join(F_NEST_TOML.as_path())));
|
||||||
PROJECT_ROOT
|
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(F_NEST_TOML.as_path()),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct Nest {
|
pub struct Nest {
|
||||||
@@ -133,16 +127,12 @@ impl Class {
|
|||||||
// If the source file exists, hasn't been moved to a package (path is a file) and
|
// If the source file exists, hasn't been moved to a package (path is a file) and
|
||||||
// the associated compiled class file exists in DIR_TARGET
|
// the associated compiled class file exists in DIR_TARGET
|
||||||
PROJECT_ROOT
|
PROJECT_ROOT
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(DIR_TARGET.as_path())
|
.join(DIR_TARGET.as_path())
|
||||||
.join(self.name.as_path())
|
.join(self.name.as_path())
|
||||||
.with_extension(JAVA_EXT_CLASS)
|
.with_extension(JAVA_EXT_CLASS)
|
||||||
.exists()
|
.exists()
|
||||||
&& sha256::try_digest(
|
&& sha256::try_digest(
|
||||||
PROJECT_ROOT
|
PROJECT_ROOT
|
||||||
.lock()
|
|
||||||
.expect("Failed to lock mutex")
|
|
||||||
.join(if self.test {
|
.join(if self.test {
|
||||||
DIR_TEST.clone()
|
DIR_TEST.clone()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user