diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-06-06 09:56:25 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-06-06 09:56:25 +0000 |
commit | 5d20c752b0054958ddcb6e2c9a4c98f1003ac68f (patch) | |
tree | 0ad587ca02d437f8788c52086719c84610e45905 | |
parent | 744e64e1dca1ab1553130aa8a4653d2bf5ca5f52 (diff) | |
parent | dafc0b76c32451a87af49790288b9b9b0c74698b (diff) | |
download | subplot-5d20c752b0054958ddcb6e2c9a4c98f1003ac68f.tar.gz |
Merge branch 'java-home' into 'main'
graphmarkup: Support JAVA_HOME for plantuml launch
Closes #199
See merge request subplot/subplot!175
-rw-r--r-- | src/graphmarkup.rs | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/src/graphmarkup.rs b/src/graphmarkup.rs index 4f6d737..1e6b7c1 100644 --- a/src/graphmarkup.rs +++ b/src/graphmarkup.rs @@ -2,7 +2,10 @@ use crate::{Result, SubplotError}; // use roadmap; +use std::env; +use std::ffi::OsString; use std::io::prelude::*; +use std::path::PathBuf; // use std::path::Path; use std::process::{Command, Stdio}; @@ -116,12 +119,28 @@ impl PlantumlMarkup { markup: markup.to_owned(), } } + + // If JAVA_HOME is set, and PATH is set, then: + // Check if JAVA_HOME/bin is in PATH, if not, prepend it and return a new + // PATH + fn build_java_path() -> Option<OsString> { + let java_home = env::var_os("JAVA_HOME")?; + let cur_path = env::var_os("PATH")?; + let cur_path: Vec<_> = env::split_paths(&cur_path).collect(); + let java_home = PathBuf::from(java_home); + let java_bin = java_home.join("bin"); + if cur_path.iter().any(|v| v.as_os_str() == java_bin) { + // No need to add JAVA_HOME/bin it's already on-path + return None; + } + env::join_paths(Some(java_bin).iter().chain(cur_path.iter())).ok() + } } impl GraphMarkup for PlantumlMarkup { fn as_svg(&self) -> Result<Vec<u8>> { - let mut child = Command::new("java") - .arg("-Djava.awt.headless=true") + let mut cmd = Command::new("java"); + cmd.arg("-Djava.awt.headless=true") .arg("-jar") .arg("/usr/share/plantuml/plantuml.jar") .arg("--") @@ -130,8 +149,11 @@ impl GraphMarkup for PlantumlMarkup { .arg("-v") .stdin(Stdio::piped()) .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn()?; + .stderr(Stdio::piped()); + if let Some(path) = Self::build_java_path() { + cmd.env("PATH", path); + } + let mut child = cmd.spawn()?; if let Some(stdin) = child.stdin.as_mut() { stdin.write_all(self.markup.as_bytes())?; let output = child.wait_with_output()?; |