Improve javac version fetching.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
[package]
|
||||
name = "java"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
|
||||
@@ -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<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> {
|
||||
Ok(Exec::cmd(JAVA_BIN_COMPILER)
|
||||
.args(
|
||||
@@ -76,7 +58,7 @@ impl Compiler {
|
||||
.into_iter()
|
||||
.flat_map(|f| Into::<Vec<String>>::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<Vec<String>> for CompilerFlag {
|
||||
@@ -96,6 +79,18 @@ impl Into<Vec<String>> 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<semver::Version> {
|
||||
// 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<semver::Version> {
|
||||
// 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<semver::Version> {
|
||||
|
||||
/// Calls the java binary, returning the complete stdout version information.
|
||||
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)
|
||||
.arg(VMFlag::Version.to_string().as_str())
|
||||
.arg(VMFlag::Version.to_string())
|
||||
.capture()?
|
||||
.stdout_str())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user