Files
raven/src/env.rs
2026-01-26 21:19:51 -05:00

42 lines
1.2 KiB
Rust

use std::io;
use std::path::Path;
use std::str::FromStr;
use std::sync::LazyLock;
use crate::java::{JAVA_VM, VMFlag};
use anyhow::Context;
static JAVA_VERSION: LazyLock<String> = LazyLock::new(|| {
crate::io::run_process(&[JAVA_VM, VMFlag::Version.to_string().as_str()])
.expect("Failed to call java")
.0
.expect("Failed to read java version from stdout")
});
pub fn get_javac_ver() -> anyhow::Result<semver::Version> {
/*
* $ 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)
*/
semver::Version::from_str(
JAVA_VERSION
.lines()
.nth(0)
.context("stdout from Java is all f-cked up")?
.split_ascii_whitespace()
.nth(1)
.context("Java version position is all f-cked up")?,
)
.context("Failed to produce a version struct from string")
}
pub fn set_cwd<P: AsRef<Path>>(path: P) -> io::Result<()> {
let mut cwd = crate::PROJECT_ROOT.clone();
cwd.push(path.as_ref());
std::env::set_current_dir(cwd.as_path())
}