Skip to content

Commit

Permalink
feat: add a new loader to load the library bootstrap, the package boo…
Browse files Browse the repository at this point in the history
…tstrap and all package's items in one `!include` statement
  • Loading branch information
tmorin committed Aug 15, 2022
1 parent f8e554b commit d32ea1e
Show file tree
Hide file tree
Showing 22 changed files with 462 additions and 224 deletions.
43 changes: 31 additions & 12 deletions src/cmd/library/generate/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,21 @@ impl Generator {
counter.stop();
Ok(())
}
fn render_templates(&self, tera: &Tera) -> Result<()> {
log::info!("Start the Render Templates phase.");
fn render_atomic_templates(&self, tera: &Tera) -> Result<()> {
log::info!("Start the Render Atomic Templates phase.");
let mut counter = Counter::start(self.tasks.len());
for task in &self.tasks {
task.render_templates(tera)?;
task.render_atomic_templates(tera)?;
counter.increase();
}
counter.stop();
Ok(())
}
fn render_composed_templates(&self, tera: &Tera) -> Result<()> {
log::info!("Start the Render Composed Templates phase.");
let mut counter = Counter::start(self.tasks.len());
for task in &self.tasks {
task.render_composed_templates(tera)?;
counter.increase();
}
counter.stop();
Expand All @@ -115,7 +125,8 @@ impl Generator {
) -> Result<()> {
self.cleanup(cleanup_scopes)?;
self.create_resources()?;
self.render_templates(tera)?;
self.render_atomic_templates(tera)?;
self.render_composed_templates(tera)?;
self.render_sources(plantuml)?;
Ok(())
}
Expand All @@ -136,10 +147,7 @@ mod tests {

#[test]
fn test_full_generation() {
env_logger::builder()
.filter_level(LevelFilter::Info)
.try_init()
.unwrap_or_default();
env_logger::builder().filter_level(LevelFilter::Info).init();
let config = &Config::default()
.rebase_directories("target/tests/generator/library-full".to_string())
.update_plantuml_jar("test/plantuml-1.2022.4.jar".to_string());
Expand All @@ -149,13 +157,24 @@ mod tests {
&config.plantuml_jar,
&config.plantuml_version,
)
.unwrap();
.unwrap();
let yaml = &read_to_string(Path::new("test/library-full.yaml")).unwrap();
let library: &Library = &serde_yaml::from_str(yaml).unwrap();
let generator = &Generator::create(config, library, &vec![]).unwrap();
let generator = &Generator::create(config, library, &[]).unwrap();
generator
.generate(&vec![CleanupScope::All], tera, plantuml)
.generate(&[CleanupScope::All], tera, plantuml)
.unwrap();

let c4model_single_content =
read_to_string("target/tests/generator/library-full/distribution/c4model/single.puml")
.unwrap();
assert!(c4model_single_content
.trim()
.contains("!global $INCLUSION_MODE"));
assert!(c4model_single_content
.trim()
.contains("!procedure C4Element("));
assert!(c4model_single_content.trim().contains("!procedure Person("));
}

#[test]
Expand All @@ -169,7 +188,7 @@ mod tests {
&config.plantuml_jar,
&config.plantuml_version,
)
.unwrap();
.unwrap();
let yaml = &read_to_string(Path::new("test/library-icon_reference.yaml")).unwrap();
let library: &Library = &serde_yaml::from_str(yaml).unwrap();
let generator = &Generator::create(config, library, &vec![]).unwrap();
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/library/generate/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ pub trait Task {
fn create_resources(&self) -> Result<()> {
Ok(())
}
fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
Ok(())
}
fn render_composed_templates(&self, _tera: &Tera) -> Result<()> {
Ok(())
}
fn render_sources(&self, _plantuml: &PlantUML) -> Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/library/generate/tasks/item/element_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Task for ElementSnippetTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{}/{}/{} - ElementSnippetTask - render templates",
&self.item_urn,
Expand Down Expand Up @@ -294,7 +294,7 @@ mod test {
properties: HashMap::default(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(generator.full_destination_source_path).unwrap();
if snippet_mode.eq(&Remote) {
assert!(content.contains(r##"!global $LIB_BASE_LOCATION="a remote url""##));
Expand Down Expand Up @@ -334,7 +334,7 @@ mod test {
properties: HashMap::default(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(generator.full_destination_source_path).unwrap();
if snippet_mode.eq(&Remote) {
assert!(content.contains(r##"!global $LIB_BASE_LOCATION="a remote url""##));
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/library/generate/tasks/item/item_documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ItemDocumentationTask {
})
}
pub fn get_relative_documentation_path(&self) -> Box<Path> {
Box::from(Path::new(format!("{}.md", self.item_urn,).as_str()))
Box::from(Path::new(format!("{}.md", self.item_urn, ).as_str()))
}
fn get_full_documentation_path(&self) -> Box<Path> {
Path::new(&self.output_directory)
Expand All @@ -116,7 +116,7 @@ impl Task for ItemDocumentationTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{} - ItemDocumentationTask - render templates",
&self.item_urn
Expand Down Expand Up @@ -196,12 +196,12 @@ mod test {
template: get_default_template_item_documentation(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(&tera).unwrap();
generator.render_atomic_templates(&tera).unwrap();
let content = read_to_string(format!(
"{}/{}.md",
generator.output_directory, generator.item_urn,
))
.unwrap();
.unwrap();
assert!(content.contains(r"# Item"));
assert!(content.contains(r"| Illustration | Icon | Card | Group |"));
assert!(content.contains(r"| ![illustration for Illustration](../../.././Icon.png) | ![illustration for Icon](../../.././Item.png) | ![illustration for Card](../../.././ItemCard.png) | ![illustration for Group](../../.././ItemGroup.png) |"));
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/library/generate/tasks/item/item_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::fs::{read_to_string, File};
use std::fs::{File, read_to_string};
use std::path::Path;

use serde::{Deserialize, Serialize};
Expand All @@ -8,7 +8,7 @@ use tera::{Context, Tera};

use crate::cmd::library::generate::config::Config;
use crate::cmd::library::generate::task::{CleanupScope, Task};
use crate::constants::{SPRITES, SPRITE_LG};
use crate::constants::{SPRITE_LG, SPRITES};
use crate::error::Error;
use crate::manifest::element::Shape;
use crate::manifest::item::Item;
Expand Down Expand Up @@ -172,7 +172,7 @@ impl ItemSourceTask {
})
}
fn get_relative_source_path(&self) -> Box<Path> {
Box::from(Path::new(format!("{}.puml", self.item_urn,).as_str()))
Box::from(Path::new(format!("{}.puml", self.item_urn, ).as_str()))
}
fn get_full_source_path(&self) -> Box<Path> {
Path::new(&self.output_directory)
Expand All @@ -190,7 +190,7 @@ impl Task for ItemSourceTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!("{} - ItemIconTask - render templates", &self.item_urn);

let destination_path = self.get_full_source_path();
Expand Down Expand Up @@ -298,12 +298,12 @@ mod test {
};
let tera = &create_tera(TEMPLATES.to_vec(), None).unwrap();
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(format!(
"{}/{}.puml",
generator.output_directory, generator.item_urn,
))
.unwrap();
.unwrap();
assert!(content.contains("LX_6N8UPcPbT0G"));
assert!(content.contains(
r"IconElement($id, 'IconElement', 'Package/Module/Family/BuiltInItem', $name, $tech, $desc)"
Expand All @@ -325,7 +325,7 @@ mod test {
keyB: [ itemA, itemB ]
"#,
)
.unwrap();
.unwrap();
let generator = ItemSourceTask {
item_urn: "Package/Module/Family/CustomItem".to_string(),
cached_sprite_paths: vec![],
Expand All @@ -338,12 +338,12 @@ mod test {
};
let tera = &create_tera(TEMPLATES.to_vec(), Some("test/tera/**".to_string())).unwrap();
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(format!(
"{}/{}.puml",
generator.output_directory, generator.item_urn,
))
.unwrap();
.unwrap();
assert!(content.contains("' valueA"));
assert!(content.contains("' itemA,itemB"));
assert!(content.contains("!procedure CustomItem($id)"));
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/library/generate/tasks/library/library_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Task for LibraryBootstrapTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{} - LibraryBootstrapTask - render templates",
self.library_name
Expand Down Expand Up @@ -137,7 +137,7 @@ mod test {
template: get_default_template_library_bootstrap(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content =
read_to_string(format!("{}/bootstrap.puml", generator.output_directory)).unwrap();
assert!(content.contains(r##"!global $LIB_BASE_LOCATION="a remote url""##));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Task for LibraryDocumentationTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{} - LibraryDocumentationTask - render templates",
self.library_name
Expand Down Expand Up @@ -128,7 +128,7 @@ mod test {
template: get_default_template_library_documentation(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(format!("{}/README.md", generator.output_directory)).unwrap();
assert!(content.contains(r##"The library provides 3 packages."##));
assert!(content.contains(r##"- [PackageA](PackageA/README.md)"##));
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/library/generate/tasks/module/module_documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl ModuleDocumentationTask {
}
fn get_relative_destination_path(&self) -> Box<Path> {
Box::from(Path::new(
format!("{}/README.md", self.module_urn,).as_str(),
format!("{}/README.md", self.module_urn, ).as_str(),
))
}
fn get_full_destination_path(&self) -> Box<Path> {
Expand All @@ -104,7 +104,7 @@ impl Task for ModuleDocumentationTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{} - ModuleDocumentationTask - render templates",
self.module_urn
Expand Down Expand Up @@ -185,12 +185,12 @@ mod test {
template: get_default_template_module_documentation(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(format!(
"{}/Package/Module/README.md",
generator.output_directory
))
.unwrap();
.unwrap();
assert!(content.contains("The module contains 4 items."));
assert!(content.contains("[Package/Module/itemD](../../Package/Module/itemD.md)"));
assert!(content.contains("## FamilyA"));
Expand Down
19 changes: 16 additions & 3 deletions src/cmd/library/generate/tasks/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ use crate::cmd::library::generate::config::Config;
use crate::cmd::library::generate::task::Task;
use crate::cmd::library::generate::tasks::package::package_bootstrap::PackageBootstrapTask;
use crate::cmd::library::generate::tasks::package::package_documentation::PackageDocumentationTask;
use crate::cmd::library::generate::tasks::package::package_embedded::{
EmbeddedMode, PackageEmbeddedTask,
};
use crate::cmd::library::generate::tasks::package::package_example::PackageExampleTask;
use crate::cmd::library::generate::tasks::package::package_full::PackageFullTask;
use crate::manifest::library::Library;
use crate::manifest::package::Package;
use crate::result::Result;

mod package_bootstrap;
mod package_documentation;
mod package_embedded;
mod package_example;
mod package_full;

pub fn parse_package(
_config: &Config,
Expand All @@ -28,7 +30,18 @@ pub fn parse_package(
}

tasks.push(Box::from(PackageBootstrapTask::create(_config, _package)?));
tasks.push(Box::from(PackageFullTask::create(_config, _package)?));
if !_package.rendering.skip_embedded {
tasks.push(Box::from(PackageEmbeddedTask::create(
_config,
_package,
EmbeddedMode::Single,
)?));
tasks.push(Box::from(PackageEmbeddedTask::create(
_config,
_package,
EmbeddedMode::Full,
)?));
}
tasks.push(Box::from(PackageDocumentationTask::create(
_config, _library, _package,
)?));
Expand Down
12 changes: 6 additions & 6 deletions src/cmd/library/generate/tasks/package/package_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Task for PackageBootstrapTask {
Ok(())
}

fn render_templates(&self, _tera: &Tera) -> Result<()> {
fn render_atomic_templates(&self, _tera: &Tera) -> Result<()> {
log::debug!(
"{} - PackageBootstrapTask - render templates",
self.package_urn
Expand Down Expand Up @@ -98,14 +98,14 @@ mod test {
template: "package_bootstrap_bis.tera".to_string(),
};
generator.cleanup(&vec![CleanupScope::All]).unwrap();
generator.render_templates(tera).unwrap();
generator.render_atomic_templates(tera).unwrap();
let content = read_to_string(format!(
"{}/Package/bootstrap.puml",
generator.output_directory
))
.unwrap();
assert!(content
.trim()
.contains("@startuml\n' header\n' content\n' footer\n@enduml"));
.unwrap();
assert!(content.trim().contains("header"));
assert!(content.trim().contains("content"));
assert!(content.trim().contains("footer"));
}
}
Loading

0 comments on commit d32ea1e

Please sign in to comment.