Improve javac version fetching.

This commit is contained in:
Cutieguwu
2026-02-16 20:40:36 -05:00
parent dcbf67e203
commit 1009a84c06
3 changed files with 39 additions and 38 deletions

View File

@@ -3,7 +3,7 @@
[package] [package]
name = "java" name = "java"
version = "0.2.1" version = "0.2.2"
edition.workspace = true edition.workspace = true
license.workspace = true license.workspace = true

View File

@@ -1,9 +1,11 @@
use std::fmt::Display;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use subprocess::Exec; use subprocess::Exec;
use subprocess::Redirection; use subprocess::Redirection;
use crate::JAVA_BIN_COMPILER; use crate::JAVA_BIN_COMPILER;
use crate::JAVA_EXT_SOURCE;
use crate::Result; use crate::Result;
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
@@ -48,26 +50,6 @@ pub struct Compiler {
} }
impl Compiler { impl Compiler {
/*
pub fn compile<P: AsRef<Path>>(self, path: P) -> Result<(Option<String>, Option<String>)> {
let mut cmd: Vec<String> = vec![JAVA_BIN_COMPILER.to_string()];
cmd.extend(
self.flags
.clone()
.into_iter()
.flat_map(|f| Into::<Vec<String>>::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<P: AsRef<Path>>(self, path: P) -> Result<bool> { pub fn compile<P: AsRef<Path>>(self, path: P) -> Result<bool> {
Ok(Exec::cmd(JAVA_BIN_COMPILER) Ok(Exec::cmd(JAVA_BIN_COMPILER)
.args( .args(
@@ -76,7 +58,7 @@ impl Compiler {
.into_iter() .into_iter()
.flat_map(|f| Into::<Vec<String>>::into(f)), .flat_map(|f| Into::<Vec<String>>::into(f)),
) )
.arg(path.as_ref()) .arg(path.as_ref().with_extension(JAVA_EXT_SOURCE))
.stdout(Redirection::Pipe) .stdout(Redirection::Pipe)
.detached() .detached()
.start()? .start()?
@@ -89,6 +71,7 @@ impl Compiler {
pub enum CompilerFlag { pub enum CompilerFlag {
Classpath { path: PathBuf }, Classpath { path: PathBuf },
Destination { path: PathBuf }, Destination { path: PathBuf },
Version,
} }
impl Into<Vec<String>> for CompilerFlag { impl Into<Vec<String>> for CompilerFlag {
@@ -96,6 +79,18 @@ impl Into<Vec<String>> for CompilerFlag {
match self { match self {
Self::Classpath { path } => vec!["-classpath".to_string(), path.display().to_string()], Self::Classpath { path } => vec!["-classpath".to_string(), path.display().to_string()],
Self::Destination { path } => vec!["-d".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"),
} }
} }
} }

View File

@@ -8,6 +8,8 @@ pub use error::{Error, Result};
use runtime::VMFlag; use runtime::VMFlag;
use subprocess::Exec; use subprocess::Exec;
use crate::compiler::CompilerFlag;
pub const JAVA_BIN_VM: &str = "java"; pub const JAVA_BIN_VM: &str = "java";
pub const JAVA_BIN_COMPILER: &str = "javac"; 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"; pub const F_JAVA_VERSION: &str = ".java-version";
/// Uses the java binary to parse its stdout for version information. /// Uses the javac binary to get information about the install compiler.
/// pub fn get_javac_version() -> Result<semver::Version> {
/// This is non-caching. // Try to pull from javac first.
pub fn get_javac_ver() -> Result<semver::Version> { if let Ok(version) = semver::Version::from_str(
// TODO: Consider making this pull the version info from javac instead? Exec::cmd(JAVA_BIN_COMPILER)
.arg(CompilerFlag::Version.to_string())
/* .capture()?
* $ java --version .stdout_str()
* openjdk 21.0.9 2025-10-21 .replace("javac", "")
* OpenJDK Runtime Environment (build 21.0.9+10) .trim(),
* OpenJDK 64-Bit Server VM (build 21.0.9+10, mixed mode, sharing) ) {
*/ return Ok(version);
}
Ok(semver::Version::from_str( Ok(semver::Version::from_str(
get_java_version_info()? get_java_version_info()?
.lines()
.nth(0)
.ok_or(Error::EmptyStdout)?
.split_ascii_whitespace() .split_ascii_whitespace()
.nth(1) .nth(1)
.ok_or(Error::NthOutOfBounds)?, .ok_or(Error::NthOutOfBounds)?,
@@ -42,8 +42,14 @@ pub fn get_javac_ver() -> Result<semver::Version> {
/// Calls the java binary, returning the complete stdout version information. /// Calls the java binary, returning the complete stdout version information.
fn get_java_version_info() -> Result<String> { fn get_java_version_info() -> Result<String> {
/*
* $ 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) Ok(Exec::cmd(JAVA_BIN_VM)
.arg(VMFlag::Version.to_string().as_str()) .arg(VMFlag::Version.to_string())
.capture()? .capture()?
.stdout_str()) .stdout_str())
} }