Skip to content

Commit

Permalink
feat: add an option to append additional PlantUML arguments when gene…
Browse files Browse the repository at this point in the history
…rating diagrams

fix #11
  • Loading branch information
tmorin committed Sep 21, 2024
1 parent 8639d0c commit 9cc0a2f
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 67 deletions.
106 changes: 47 additions & 59 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::cmd::{
};

pub fn start_app<I, T>(args: I) -> i32
where
I: IntoIterator<Item=T>,
T: Into<OsString> + Clone,
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
let mut app = build_cli();

Expand Down Expand Up @@ -43,26 +43,22 @@ pub fn start_app<I, T>(args: I) -> i32
eprintln!("unable to configure the logger: {}", e);
}

return match app_matches.subcommand() {
match app_matches.subcommand() {
Some(("library", m)) => match m.subcommand() {
Some(("generate", m)) => {
return match execute_library_generate(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("schema", m)) => {
return match execute_library_schema(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("generate", m)) => match execute_library_generate(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
Some(("schema", m)) => match execute_library_schema(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
_ => {
log::warn!("the SUBCOMMAND is missing");
app.write_help(&mut io::stderr())
Expand All @@ -72,24 +68,20 @@ pub fn start_app<I, T>(args: I) -> i32
}
},
Some(("workspace", m)) => match m.subcommand() {
Some(("init", m)) => {
return match execute_workspace_init(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("install", m)) => {
return match execute_workspace_install(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("init", m)) => match execute_workspace_init(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
Some(("install", m)) => match execute_workspace_install(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
_ => {
log::warn!("the SUBCOMMAND is missing");
app.write_help(&mut io::stderr())
Expand All @@ -99,15 +91,13 @@ pub fn start_app<I, T>(args: I) -> i32
}
},
Some(("diagram", m)) => match m.subcommand() {
Some(("generate", m)) => {
return match execute_diagram_generate(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("generate", m)) => match execute_diagram_generate(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
_ => {
log::warn!("the SUBCOMMAND is missing");
app.write_help(&mut io::stderr())
Expand All @@ -116,21 +106,19 @@ pub fn start_app<I, T>(args: I) -> i32
2
}
},
Some(("completion", m)) => {
return match execute_completion(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
};
}
Some(("completion", m)) => match execute_completion(m) {
Ok(_) => 0,
Err(e) => {
log::error!("the command failed: {}", e);
2
}
},
_ => {
log::warn!("the SUBCOMMAND is missing");
app.write_help(&mut io::stderr())
.expect("unable to write help message");
eprintln!();
2
}
};
}
}
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ pub fn build_cli() -> Command {
.long("force")
.action(ArgAction::SetTrue)
.help("Force the rendering of discovered .puml file."))
.arg(Arg::new("plantuml_args")
.short('a')
.long("args")
.action(ArgAction::Set)
.num_args(1..)
.value_delimiter(' ')
.help("Extra arguments for PlantUML."))
.arg(&arg_cache_directory)
.arg(&arg_plantuml_version)
.arg(&arg_plantuml_jar)
Expand Down
8 changes: 7 additions & 1 deletion src/cmd/diagram/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ pub fn execute_diagram_generate(arg_matches: &ArgMatches) -> Result<()> {
);
if force_generation || last_modification_timestamp > last_generation_timestamp {
log::info!("generate {:?}", source_path);
plantuml.render(&source_path)?;
let plantuml_args = arg_matches
.get_many::<String>("plantuml_args")
.unwrap_or_default()
.map(|v| v.to_string())
.collect::<Vec<_>>();
plantuml.render(&source_path, Some(plantuml_args))?;
}
}
save_last_generation_timestamp(last_gen_path)?;
Expand Down Expand Up @@ -153,6 +158,7 @@ mod test {
"-s=target/tests/cmd/diagram/generate/source",
"-C=target/tests/cmd/diagram/generate/cache",
"-P=test/plantuml-1.2022.4.jar",
"-a=-png -v",
]);
execute_diagram_generate(
arg_matches
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/library/generate/tasks/item/element_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Task for ElementSnippetTask {

// render the snippet
let source_path = Path::new(&self.full_destination_source_path);
plantuml.render(source_path)?;
plantuml.render(source_path, None)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/library/generate/tasks/package/package_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Task for PackageExampleTask {

// render the snippet
let source_path = Path::new(&self.full_source_path);
plantuml.render(source_path)?;
plantuml.render(source_path, None)?;

Ok(())
}
Expand Down
16 changes: 11 additions & 5 deletions src/plantuml.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::fs::File;
use std::io;
use std::io::Write;
Expand All @@ -18,7 +19,7 @@ pub struct PlantUML {
}

impl PlantUML {
pub fn render(&self, source_path: &Path) -> Result<()> {
pub fn render(&self, source_path: &Path, p_args_as_strings: Option<Vec<String>>) -> Result<()> {
//get the source
let source = match source_path.to_str() {
None => {
Expand All @@ -30,18 +31,23 @@ impl PlantUML {
Some(s) => s,
};
// generate the file
let p_args = p_args_as_strings.map(|strings| {
strings
.iter()
.map(OsString::from)
.collect::<Vec<OsString>>()
});
let output = Command::new(&self.java_binary)
.arg("-jar")
.arg(&self.plantuml_jar)
.arg(source)
.args(p_args.unwrap_or_default())
.output()
.map_err(|e| anyhow::Error::new(e).context(format!("unable to render {}", source)))?;
io::stdout().write_all(&output.stdout).unwrap();
// .map_err(|e| anyhow::Error::new(e).context(format!("unable to write stdout"), Box::from(e)))?;
io::stderr().write_all(&output.stderr).unwrap();
io::stdout().write_all(&output.stdout)?;
io::stderr().write_all(&output.stderr)?;
// check the generation
if !output.status.success() {
// .map_err(|e| anyhow::Error::new(e).context(format!("unable to write stderr"), Box::from(e)))?;
return Err(anyhow::Error::msg(format!("failed to render {}", source)));
}

Expand Down

0 comments on commit 9cc0a2f

Please sign in to comment.