From 1009a84c063bd4bbd8a176a0822b092172cf19a3 Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Mon, 16 Feb 2026 20:40:36 -0500 Subject: [PATCH] Improve javac version fetching. --- crates/java/Cargo.toml | 2 +- crates/java/src/compiler.rs | 37 ++++++++++++++++-------------------- crates/java/src/lib.rs | 38 +++++++++++++++++++++---------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/crates/java/Cargo.toml b/crates/java/Cargo.toml index a3ca566..56d05c5 100644 --- a/crates/java/Cargo.toml +++ b/crates/java/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "java" -version = "0.2.1" +version = "0.2.2" edition.workspace = true license.workspace = true diff --git a/crates/java/src/compiler.rs b/crates/java/src/compiler.rs index e685536..9f6d46c 100644 --- a/crates/java/src/compiler.rs +++ b/crates/java/src/compiler.rs @@ -1,9 +1,11 @@ +use std::fmt::Display; use std::path::{Path, PathBuf}; use subprocess::Exec; use subprocess::Redirection; use crate::JAVA_BIN_COMPILER; +use crate::JAVA_EXT_SOURCE; use crate::Result; #[derive(Debug, Default, Clone)] @@ -48,26 +50,6 @@ pub struct Compiler { } impl Compiler { - /* - pub fn compile>(self, path: P) -> Result<(Option, Option)> { - let mut cmd: Vec = vec![JAVA_BIN_COMPILER.to_string()]; - - cmd.extend( - self.flags - .clone() - .into_iter() - .flat_map(|f| Into::>::into(f)), - ); - cmd.extend( - fs::expand_files(path)? - .into_iter() - .filter_map(|f| Some(f.to_str()?.to_string())), - ); - - Ok(io::run_process(cmd.as_slice())?) - } - */ - pub fn compile>(self, path: P) -> Result { Ok(Exec::cmd(JAVA_BIN_COMPILER) .args( @@ -76,7 +58,7 @@ impl Compiler { .into_iter() .flat_map(|f| Into::>::into(f)), ) - .arg(path.as_ref()) + .arg(path.as_ref().with_extension(JAVA_EXT_SOURCE)) .stdout(Redirection::Pipe) .detached() .start()? @@ -89,6 +71,7 @@ impl Compiler { pub enum CompilerFlag { Classpath { path: PathBuf }, Destination { path: PathBuf }, + Version, } impl Into> for CompilerFlag { @@ -96,6 +79,18 @@ impl Into> for CompilerFlag { match self { Self::Classpath { path } => vec!["-classpath".to_string(), path.display().to_string()], Self::Destination { path } => vec!["-d".to_string(), path.display().to_string()], + Self::Version => vec!["--version".to_string()], + } + } +} + +// TODO: Clean this up a bit? +impl Display for CompilerFlag { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Classpath { path } => write!(f, "-classpath {}", path.display()), + Self::Destination { path } => write!(f, "-d {}", path.display()), + Self::Version => write!(f, "--version"), } } } diff --git a/crates/java/src/lib.rs b/crates/java/src/lib.rs index 2caae94..ab1391c 100644 --- a/crates/java/src/lib.rs +++ b/crates/java/src/lib.rs @@ -8,6 +8,8 @@ pub use error::{Error, Result}; use runtime::VMFlag; use subprocess::Exec; +use crate::compiler::CompilerFlag; + pub const JAVA_BIN_VM: &str = "java"; pub const JAVA_BIN_COMPILER: &str = "javac"; @@ -16,24 +18,22 @@ pub const JAVA_EXT_CLASS: &str = "class"; pub const F_JAVA_VERSION: &str = ".java-version"; -/// Uses the java binary to parse its stdout for version information. -/// -/// This is non-caching. -pub fn get_javac_ver() -> Result { - // TODO: Consider making this pull the version info from javac instead? - - /* - * $ java --version - * openjdk 21.0.9 2025-10-21 - * OpenJDK Runtime Environment (build 21.0.9+10) - * OpenJDK 64-Bit Server VM (build 21.0.9+10, mixed mode, sharing) - */ +/// Uses the javac binary to get information about the install compiler. +pub fn get_javac_version() -> Result { + // Try to pull from javac first. + if let Ok(version) = semver::Version::from_str( + Exec::cmd(JAVA_BIN_COMPILER) + .arg(CompilerFlag::Version.to_string()) + .capture()? + .stdout_str() + .replace("javac", "") + .trim(), + ) { + return Ok(version); + } Ok(semver::Version::from_str( get_java_version_info()? - .lines() - .nth(0) - .ok_or(Error::EmptyStdout)? .split_ascii_whitespace() .nth(1) .ok_or(Error::NthOutOfBounds)?, @@ -42,8 +42,14 @@ pub fn get_javac_ver() -> Result { /// Calls the java binary, returning the complete stdout version information. fn get_java_version_info() -> Result { + /* + * $ java --version + * openjdk 21.0.9 2025-10-21 + * OpenJDK Runtime Environment (build 21.0.9+10) + * OpenJDK 64-Bit Server VM (build 21.0.9+10, mixed mode, sharing) + */ Ok(Exec::cmd(JAVA_BIN_VM) - .arg(VMFlag::Version.to_string().as_str()) + .arg(VMFlag::Version.to_string()) .capture()? .stdout_str()) }