Fix Java project structure.

This commit is contained in:
Olivia Brooks
2026-01-26 14:29:43 -05:00
parent 80e5a27c20
commit 11737e67f8
4 changed files with 54 additions and 14 deletions

View File

@@ -1,6 +1,12 @@
package main;
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;
}
}

12
assets/src/test/Main.java Normal file
View File

@@ -0,0 +1,12 @@
package test;
public class Main {
public static void main(String[] args) {
testAdd();
}
public static void testAdd() {
assert main.Main.add(2, 2) == 4;
}
}

View File

@@ -10,7 +10,12 @@ pub fn expand_files<P: AsRef<Path>>(path: P) -> anyhow::Result<Vec<PathBuf>> {
Ok(std::fs::read_dir(path)?
.filter_map(|entry| {
let path = entry.ok()?.path();
if path.is_file() { Some(path) } else { None }
if path.is_dir() {
Some(expand_files(path).ok()?)
} else {
Some(vec![path])
}
})
.flatten()
.collect())
}

View File

@@ -18,8 +18,14 @@ use anyhow::Context;
use bytesize::ByteSize;
const DIR_TARGET: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("target/"));
const DIR_TEST: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("test/"));
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/"));
/// 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 F_NEST_TOML: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.toml"));
const F_NEST_LOCK: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("Nest.lock"));
@@ -77,8 +83,8 @@ fn init() -> anyhow::Result<()> {
.to_owned();
// Make src, target, tests
for dir in [DIR_SRC, DIR_TARGET, DIR_TEST] {
std::fs::create_dir(dir.clone())?;
for dir in [DIR_SRC, DIR_MAIN, DIR_TARGET, DIR_TEST] {
std::fs::create_dir_all(dir.clone())?;
}
// Make config file.
@@ -114,13 +120,24 @@ fn init() -> anyhow::Result<()> {
)?;
}
// Make Main.java
if let Result::Ok(mut f) = OpenOptions::new()
.write(true)
.create_new(is_empty)
.open(DIR_SRC.clone().join("Main").with_extension(JAVA_EXT_SOURCE))
{
f.write_all(include_bytes!("../assets/Main.java"))?;
// Make src/main/Main.java
if let Result::Ok(mut f) = OpenOptions::new().write(true).create_new(is_empty).open(
DIR_MAIN
.clone()
.join("Main")
.with_extension(JAVA_EXT_SOURCE),
) {
f.write_all(include_bytes!("../assets/src/main/Main.java"))?;
}
// Make src/test/Main.java
if let Result::Ok(mut f) = OpenOptions::new().write(true).create_new(is_empty).open(
DIR_TEST
.clone()
.join("Main")
.with_extension(JAVA_EXT_SOURCE),
) {
f.write_all(include_bytes!("../assets/src/test/Main.java"))?;
}
// git init .
@@ -133,7 +150,6 @@ fn init() -> anyhow::Result<()> {
.read(true)
.open(".gitignore")
{
dbg!();
let mut buf = String::new();
f.read_to_string(&mut buf)?;
@@ -141,7 +157,7 @@ fn init() -> anyhow::Result<()> {
DIR_TARGET.as_path().display().to_string(),
format!("*.{}", JAVA_EXT_CLASS),
] {
if dbg!(!buf.contains(&ignored)) {
if !buf.contains(&ignored) {
f.write(format!("{}\n", ignored).as_bytes())?;
}
}
@@ -192,7 +208,7 @@ fn build() -> anyhow::Result<()> {
for target in targets {
println!("Compiling {}", target.display());
if javac.clone().compile(target.as_path()).is_ok()
if javac.clone().compile(dbg!(target.as_path())).is_ok()
&& let Result::Ok(class) = Class::try_from(target.clone())
{
nest_lock.0.insert(target, class);
@@ -212,6 +228,7 @@ fn run<P: AsRef<Path>>(
entry_point: P,
assertions: bool,
) -> anyhow::Result<(Option<String>, Option<String>)> {
// JRE pathing will be messed up without this.
crate::env::set_cwd(DIR_TARGET.as_path())?;
java::JVMBuilder::new()