diff --git a/src/api/tools/java/mod.rs b/src/api/tools/java/mod.rs index e8ca771..87399e8 100644 --- a/src/api/tools/java/mod.rs +++ b/src/api/tools/java/mod.rs @@ -4,12 +4,41 @@ pub type JavaVersion = u32; mod installation; mod find; mod check; +use std::{path::Path, process::Stdio}; + +use anyhow::{anyhow, Result}; use futures::StreamExt; pub use installation::*; pub use check::*; +use tokio::process::{Child, Command}; + +use crate::api::utils::pathdiff::diff_paths; pub struct JavaProcess { - + child: Child, +} + +impl JavaProcess { + pub fn new( + dir: &Path, + java: &Path, + args: &[&str], + ) -> Result { + let dir = diff_paths(dir, std::env::current_dir()?) + .ok_or(anyhow!("Couldn't diff paths"))?; + + let child = Command::new(java) + .args(args) + .current_dir(dir) + .stderr(Stdio::inherit()) + .stdout(Stdio::piped()) + .stdin(Stdio::piped()) + .spawn()?; + + Ok(Self { + child, + }) + } } pub async fn get_java_installations() -> Vec { @@ -22,3 +51,10 @@ pub async fn get_java_installations() -> Vec { .collect() .await } + +pub async fn get_java_installation_for(ver: JavaVersion) -> Option { + get_java_installations() + .await + .into_iter() + .find(|v| v.version == ver) +}