From 2d1e97a133302172d77e63802c63ba4786d788cd Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 19 Jun 2021 11:02:35 +0100 Subject: cli: Add flexibility in dot and plantuml locations Signed-off-by: Daniel Silverstone --- src/bin/subplot.rs | 12 +++++++++--- src/graphmarkup.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- src/lib.rs | 2 +- 3 files changed, 57 insertions(+), 9 deletions(-) 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; @@ -26,6 +28,9 @@ struct Toplevel { #[structopt(flatten)] 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..78ffe89 100644 --- a/src/graphmarkup.rs +++ b/src/graphmarkup.rs @@ -1,13 +1,53 @@ 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, + #[structopt( + long = "plantuml-jar", + help = "Path to the `plantuml.jar` file.", + name = "PLANTUMLJARPATH", + env = "SUBPLOT_PLANTUML_JAR_PATH" + )] + plantuml_jar_path: Option, +} + +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(); + } + } +} + +lazy_static! { + static ref DOT_PATH: Mutex = Mutex::new("dot".into()); + static ref PLANTUML_JAR_PATH: Mutex = + Mutex::new("/usr/share/plantuml/plantuml.jar".into()); +} /// A code block with markup for a graph. /// @@ -80,7 +120,7 @@ impl DotMarkup { impl GraphMarkup for DotMarkup { fn as_svg(&self) -> Result> { - 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,6 +175,8 @@ impl PlantumlMarkup { } env::join_paths(Some(java_bin).iter().chain(cur_path.iter())).ok() } + + // Acquire path to JAR for pandoc } impl GraphMarkup for PlantumlMarkup { @@ -142,7 +184,7 @@ impl GraphMarkup for PlantumlMarkup { let mut cmd = Command::new("java"); 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") diff --git a/src/lib.rs b/src/lib.rs index 51712a6..960e3ef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; -- cgit v1.2.1