diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-06-19 11:02:44 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-06-19 11:02:44 +0000 |
commit | 71efbcbb425cfda3a37af2f62ceeb2663cb65ac8 (patch) | |
tree | 6067af404601796ff9b77a272e17cb3cfd2d9048 | |
parent | b670a457b01e79bac5d1c57276bb977a15a38d5b (diff) | |
parent | 3624c01669c56640dd239bdc84d7996491df7e91 (diff) | |
download | subplot-71efbcbb425cfda3a37af2f62ceeb2663cb65ac8.tar.gz |
Merge branch 'plantuml-flexibility' into 'main'
Add flexibility in dot, java, and plantuml.jar locations
See merge request subplot/subplot!184
-rw-r--r-- | build.rs | 21 | ||||
-rw-r--r-- | src/bin/subplot.rs | 12 | ||||
-rw-r--r-- | src/graphmarkup.rs | 67 | ||||
-rw-r--r-- | src/lib.rs | 2 |
4 files changed, 92 insertions, 10 deletions
@@ -118,6 +118,24 @@ fn write_out_resource_file<'a>(paths: impl Iterator<Item = &'a Path>) -> Result< Ok(()) } +/// Adopt an environment variable into the build. +/// +/// This entails watching `SUBPLOT_{var}` and setting build environment +/// to contain `BUILTIN_{var}` to either the env value, or `{def}` if not +/// provided. +fn adopt_env_var(var: &str, def: &str) { + println!("cargo:rerun-if-env-changed=SUBPLOT_{var}", var = var); + if let Ok(value) = std::env::var(format!("SUBPLOT_{var}", var = var)) { + println!( + "cargo:rustc-env=BUILTIN_{var}={value}", + var = var, + value = value + ); + } else { + println!("cargo:rustc-env=BUILTIN_{var}={def}", var = var, def = def); + } +} + fn main() { println!("cargo:rerun-if-env-changed=DEB_BUILD_OPTIONS"); let paths = if std::env::var("DEB_BUILD_OPTIONS").is_err() { @@ -129,4 +147,7 @@ fn main() { }; write_out_resource_file(paths.iter().map(PathBuf::as_path)) .expect("Unable to write the resource file out"); + adopt_env_var("DOT_PATH", "dot"); + adopt_env_var("PLANTUML_JAR_PATH", "/usr/share/plantuml/plantuml.jar"); + adopt_env_var("JAVA_PATH", "java"); } diff --git a/src/bin/subplot.rs b/src/bin/subplot.rs index 47dc72b..867ffb2 100644 --- a/src/bin/subplot.rs +++ b/src/bin/subplot.rs @@ -5,7 +5,9 @@ use anyhow::Result; use chrono::{Local, TimeZone}; use structopt::StructOpt; -use subplot::{generate_test_program, resource, template_spec, DataFile, Document, Style}; +use subplot::{ + generate_test_program, resource, template_spec, DataFile, Document, MarkupOpts, Style, +}; use std::convert::TryFrom; use std::ffi::OsString; @@ -27,6 +29,9 @@ struct Toplevel { resources: resource::ResourceOpts, #[structopt(flatten)] + markup: MarkupOpts, + + #[structopt(flatten)] command: Cmd, } @@ -35,9 +40,10 @@ impl Toplevel { self.command.run() } - fn handle_resources(&self) { + fn handle_special_args(&self) { let doc_path = self.command.doc_path(); self.resources.handle(doc_path); + self.markup.handle(); } } @@ -408,7 +414,7 @@ fn main() { let argparser = argparser.long_version(version.as_str()); let args = argparser.get_matches(); let args = Toplevel::from_clap(&args); - args.handle_resources(); + args.handle_special_args(); match args.run() { Ok(_) => {} Err(e) => { diff --git a/src/graphmarkup.rs b/src/graphmarkup.rs index 1e6b7c1..dc0a37e 100644 --- a/src/graphmarkup.rs +++ b/src/graphmarkup.rs @@ -1,13 +1,64 @@ 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}; +use std::sync::Mutex; + +use lazy_static::lazy_static; +use structopt::StructOpt; + +/// Resources used to configure paths for dot, plantuml.jar, and friends + +#[allow(missing_docs)] +#[derive(Debug, StructOpt)] +pub struct MarkupOpts { + #[structopt( + long = "dot", + help = "Path to the `dot` binary.", + name = "DOTPATH", + env = "SUBPLOT_DOT_PATH" + )] + dot_path: Option<PathBuf>, + #[structopt( + long = "plantuml-jar", + help = "Path to the `plantuml.jar` file.", + name = "PLANTUMLJARPATH", + env = "SUBPLOT_PLANTUML_JAR_PATH" + )] + plantuml_jar_path: Option<PathBuf>, + #[structopt( + long = "java", + help = "Path to Java executable (note, effectively overrides JAVA_HOME if set to an absolute path)", + name = "JAVA_PATH", + env = "SUBPLOT_JAVA_PATH" + )] + java_path: Option<PathBuf>, +} + +impl MarkupOpts { + /// Handle CLI arguments and environment variables for markup binaries + pub fn handle(&self) { + if let Some(dotpath) = &self.dot_path { + *DOT_PATH.lock().unwrap() = dotpath.clone(); + } + if let Some(plantuml_path) = &self.plantuml_jar_path { + *PLANTUML_JAR_PATH.lock().unwrap() = plantuml_path.clone(); + } + if let Some(java_path) = &self.java_path { + *JAVA_PATH.lock().unwrap() = java_path.clone(); + } + } +} + +lazy_static! { + static ref DOT_PATH: Mutex<PathBuf> = Mutex::new(env!("BUILTIN_DOT_PATH").into()); + static ref PLANTUML_JAR_PATH: Mutex<PathBuf> = + Mutex::new(env!("BUILTIN_PLANTUML_JAR_PATH").into()); + static ref JAVA_PATH: Mutex<PathBuf> = Mutex::new(env!("BUILTIN_JAVA_PATH").into()); +} /// A code block with markup for a graph. /// @@ -80,7 +131,7 @@ impl DotMarkup { impl GraphMarkup for DotMarkup { fn as_svg(&self) -> Result<Vec<u8>> { - let mut child = Command::new("dot") + let mut child = Command::new(DOT_PATH.lock().unwrap().clone()) .arg("-Tsvg") .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -135,18 +186,22 @@ impl PlantumlMarkup { } env::join_paths(Some(java_bin).iter().chain(cur_path.iter())).ok() } + + // Acquire path to JAR for pandoc } impl GraphMarkup for PlantumlMarkup { fn as_svg(&self) -> Result<Vec<u8>> { - let mut cmd = Command::new("java"); + let mut cmd = Command::new(JAVA_PATH.lock().unwrap().clone()); cmd.arg("-Djava.awt.headless=true") .arg("-jar") - .arg("/usr/share/plantuml/plantuml.jar") + .arg(PLANTUML_JAR_PATH.lock().unwrap().clone()) .arg("--") .arg("-pipe") .arg("-tsvg") .arg("-v") + .arg("-graphvizdot") + .arg(DOT_PATH.lock().unwrap().clone()) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); @@ -20,7 +20,7 @@ pub use error::SubplotError; pub mod resource; mod graphmarkup; -pub use graphmarkup::{DotMarkup, GraphMarkup, PikchrMarkup, PlantumlMarkup}; +pub use graphmarkup::{DotMarkup, GraphMarkup, MarkupOpts, PikchrMarkup, PlantumlMarkup}; mod datafiles; pub use datafiles::DataFile; |