Skip to content

Commit

Permalink
feat: add the discovery patterns for PlantUML files as a configuratio…
Browse files Browse the repository at this point in the history
…n entry

fix #15
  • Loading branch information
tmorin committed Sep 21, 2024
1 parent 532d1fe commit 483e4f1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/Continuous-Integration.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Continuous-Integration

on: [ push, pull_request ]
on:
push:
branches:
- main
pull_request: { }

jobs:

Expand Down
11 changes: 11 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::constants::SOURCE_PATTERNS;
use clap::builder::{PossibleValuesParser, ValueParser};
use clap::{
crate_authors, crate_description, crate_version, value_parser, Arg, ArgAction, Command,
Expand All @@ -14,6 +15,15 @@ pub fn build_cli() -> Command {
.env("PLANTUML_GENERATOR_SOURCE_DIRECTORY")
.help("The directory where the .puml will be discovered.");

let arg_source_patterns: Arg = Arg::new("source_patterns")
.short('p')
.long("patterns")
.default_value(SOURCE_PATTERNS)
.action(ArgAction::Set)
.num_args(1)
.env("PLANTUML_GENERATOR_SOURCE_DIRECTORY")
.help("The directory where the .puml will be discovered.");

let arg_cache_directory: Arg = Arg::new("cache_directory")
.short('C')
.long("cache")
Expand Down Expand Up @@ -173,6 +183,7 @@ pub fn build_cli() -> Command {
Command::new("generate")
.about("Generate discovered .puml files which has been mutated since the last generation.")
.arg(&arg_source_directory)
.arg(&arg_source_patterns)
.arg(Arg::new("do_force_generation")
.short('f')
.long("force")
Expand Down
22 changes: 16 additions & 6 deletions src/cmd/diagram/generate/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ use std::path::Path;
use clap::ArgMatches;
use serde::{Deserialize, Serialize};

use crate::constants::get_default_cache_directory;
use crate::constants::get_default_java_binary;
use crate::constants::get_default_plantuml_jar;
use crate::constants::get_default_plantuml_version;
use crate::constants::get_default_source_directory;
use crate::constants::{get_default_cache_directory, get_default_source_patterns};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
/// The path to the output directory.
#[serde(default = "get_default_source_directory")]
pub source_directory: String,
/// The patterns to discover the source files separated by comas.
#[serde(default = "get_default_source_patterns")]
pub source_patterns: String,
/// The path to the cache directory.
#[serde(default = "get_default_cache_directory")]
pub cache_directory: String,
Expand All @@ -35,6 +38,11 @@ impl Config {
.map(|v| v.to_string())
.unwrap_or_else(|| self.source_directory.clone());

let source_patterns = args
.get_one::<String>("source_patterns")
.map(|v| v.to_string())
.unwrap_or_else(|| self.source_patterns.clone());

let cache_directory = args
.get_one::<String>("cache_directory")
.map(|v| v.to_string())
Expand Down Expand Up @@ -62,6 +70,7 @@ impl Config {

Config {
source_directory,
source_patterns,
cache_directory,
plantuml_version,
plantuml_jar,
Expand All @@ -78,19 +87,20 @@ impl Default for Config {
Config {
source_directory: std::env::var("PLANTUML_GENERATOR_SOURCE_DIRECTORY")
.unwrap_or_else(|_| get_default_source_directory()),
source_patterns: std::env::var("PLANTUML_GENERATOR_SOURCE_PATTERNS")
.unwrap_or_else(|_| get_default_source_patterns()),
cache_directory: std::env::var("PLANTUML_GENERATOR_CACHE_DIRECTORY")
.unwrap_or_else(|_| get_default_cache_directory()),
plantuml_version: std::env::var("PLANTUML_GENERATOR_PLANTUML_VERSION")
.unwrap_or_else(|_| get_default_plantuml_version()),
plantuml_jar: std::env::var("PLANTUML_GENERATOR_PLANTUML_JAR")
.unwrap_or_else(|_| get_default_plantuml_jar()),
java_binary: match std::env::var("PLANTUML_GENERATOR_JAVA_BINARY") {
Ok(v) => v,
Err(_) => match std::env::var("JAVA_HOME") {
java_binary: std::env::var("PLANTUML_GENERATOR_JAVA_BINARY").unwrap_or_else(|_| {
match std::env::var("JAVA_HOME") {
Ok(v) => format!("{}/bin/java", v),
Err(_) => get_default_java_binary(),
},
},
}
}),
}
}
}
50 changes: 35 additions & 15 deletions src/cmd/diagram/generate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::fs::{read_to_string, OpenOptions};
use std::io::Write;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use anyhow::Result;
use chrono::prelude::*;
use clap::ArgMatches;
use glob::{glob, Paths};
use glob::glob;

use crate::cmd::diagram::generate::config::Config;
use crate::plantuml::create_plantuml;
Expand Down Expand Up @@ -73,14 +73,25 @@ fn save_last_generation_timestamp(last_gen_path: &Path) -> Result<()> {
Ok(())
}

fn get_puml_paths(config: &Config) -> Result<Paths> {
let glob_pattern = format!("{}/**/*.puml", config.source_directory);
glob(&glob_pattern).map_err(|e| {
anyhow::Error::new(e).context(format!(
"unable to parse the glob pattern ({})",
&glob_pattern
))
})
fn get_puml_paths(config: &Config) -> Vec<PathBuf> {
config
.source_patterns
.split(",")
.map(str::trim)
.map(|pattern| format!("{}/{}", config.source_directory, pattern))
.flat_map(|glob_pattern| {
glob(&glob_pattern)
.map(|paths| paths.flatten())
.map_err(|e| {
anyhow::Error::new(e).context(format!(
"unable to parse the glob pattern ({})",
&glob_pattern
))
})
.map(|paths| paths.collect::<Vec<PathBuf>>())
.unwrap()
})
.collect::<Vec<PathBuf>>()
}

pub fn execute_diagram_generate(arg_matches: &ArgMatches) -> Result<()> {
Expand All @@ -107,9 +118,9 @@ pub fn execute_diagram_generate(arg_matches: &ArgMatches) -> Result<()> {
plantuml.download()?;
// get latest generation
let last_generation_timestamp = get_last_generation_timestamp(last_gen_path)?;
// discover .puml files
let puml_paths = get_puml_paths(config)?.flatten();
// generate .puml file
// discover source files
let puml_paths = get_puml_paths(config);
// generate source files
for source_path in puml_paths {
let last_modification_timestamp = get_last_modified(&source_path)?;
log::debug!(
Expand Down Expand Up @@ -142,7 +153,11 @@ mod test {
#[test]
fn test_diagram_generation() {
delete_file_or_directory("target/tests/cmd/diagram/generate".as_ref()).unwrap();
for source_file in &["diagrams_a.puml", "folder_a/diagrams_b.puml"] {
for source_file in &[
"diagrams_a.puml",
"diagrams_c.plantuml",
"folder_a/diagrams_b.puml",
] {
let from_prefix = "test/source";
let from_path = Path::new(from_prefix).join(source_file);
let to_prefix = "target/tests/cmd/diagram/generate/source";
Expand Down Expand Up @@ -180,14 +195,19 @@ mod test {
let path_diagram_b_1_png =
Path::new("target/tests/cmd/diagram/generate/source/folder_a/diagram_b_1.png");
assert!(path_diagram_b_1_png.exists());
let path_diagram_b_0_png =
Path::new("target/tests/cmd/diagram/generate/source/diagram_c_0.png");
assert!(path_diagram_b_0_png.exists());
let path_diagram_b_1_png =
Path::new("target/tests/cmd/diagram/generate/source/diagram_c_1.png");
assert!(path_diagram_b_1_png.exists());
// get path_diagram_a_0_src modified
let path_diagram_a_0_png_modified_before =
path_diagram_a_0_png.metadata().unwrap().modified().unwrap();
// mutate path_diagram_a_0_src
let path_diagram_a_0_src =
Path::new("target/tests/cmd/diagram/generate/source/diagrams_a.puml");
let mut file_diagram_a_0_src = OpenOptions::new()
.write(true)
.append(true)
.open(path_diagram_a_0_src)
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions src/cmd/workspace/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod test {
use crate::cli::build_cli;
use crate::constants::WORKSPACE_MANIFEST;
use crate::utils::delete_file_or_directory;
use std::fs::OpenOptions;

use super::*;

Expand Down Expand Up @@ -89,9 +90,7 @@ mod test {
delete_file_or_directory(source_path).unwrap();
create_parent_directory(&manifest_path).unwrap();

std::fs::OpenOptions::new()
.write(true)
.create(true)
OpenOptions::create(OpenOptions::new().write(true), true)
.open(manifest_path)
.expect("Couldn't open file");

Expand Down
6 changes: 2 additions & 4 deletions src/cmd/workspace/install/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn execute_workspace_install(arg_matches: &ArgMatches) -> anyhow::Result<()>

#[cfg(test)]
mod test {
use std::fs::copy;
use std::fs::{copy, OpenOptions};

use crate::cli::build_cli;
use crate::constants::WORKSPACE_MANIFEST;
Expand All @@ -145,9 +145,7 @@ mod test {
create_parent_directory(manifest_path).unwrap();
copy(Path::new("test/workspace-simple.yaml"), manifest_path).unwrap();

std::fs::OpenOptions::new()
.write(true)
.create(true)
OpenOptions::create(OpenOptions::new().write(true), true)
.open(manifest_path)
.expect("Couldn't open file");

Expand Down
6 changes: 6 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub fn get_default_source_directory() -> String {
SOURCE_DIRECTORY.to_string()
}

pub const SOURCE_PATTERNS: &str = "**/*.puml,**/*.pu,**/*.pml,**/*.plantuml";

pub fn get_default_source_patterns() -> String {
SOURCE_PATTERNS.to_string()
}

pub const OUTPUT_DIRECTORY: &str = "distribution";

pub fn get_default_output_directory() -> String {
Expand Down
11 changes: 11 additions & 0 deletions test/source/diagrams_c.plantuml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@startuml diagram_c_0
object ObjectA
object ObjectB
ObjectA -> ObjectB
@enduml

@startuml diagram_c_1
object ObjectC
object ObjectD
ObjectC -> ObjectD
@enduml

0 comments on commit 483e4f1

Please sign in to comment.