Skip to content

Commit

Permalink
feat(uml): make uml working in dir
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 27, 2021
1 parent 35fe62f commit 560faca
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
21 changes: 17 additions & 4 deletions core_model/src/url_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ use url::Url;
use crate::Settings;

pub fn json_filename_suffix(text: &str, suffix_str: Option<&str>) -> String {
filename_suffix(text, suffix_str, Some("json"))
}

pub fn filename_suffix(text: &str, name_suffix: Option<&str>, file_suffix: Option<&str>) -> String {
let mut filename_suffix = "";
if let Some(suf) = name_suffix {
filename_suffix = suf;
}

let mut suffix = "";
if let Some(suf) = suffix_str {
if let Some(suf) = file_suffix {
suffix = suf;
}

Expand All @@ -17,10 +26,10 @@ pub fn json_filename_suffix(text: &str, suffix_str: Option<&str>) -> String {
return match path.file_name() {
Some(name) => {
let filename = name.to_str().unwrap().to_string();
format!("{}{}.{}", filename, suffix, "json")
format!("{}{}.{}", filename, filename_suffix, suffix)
}
None => {
format!("default{}.{}", suffix, "json")
format!("default{}.{}", filename_suffix, suffix)
}
};
}
Expand All @@ -31,13 +40,17 @@ pub fn json_filename_suffix(text: &str, suffix_str: Option<&str>) -> String {
.map(|c| c.collect::<Vec<_>>())
.unwrap();

return format!("{}{}.{}", paths.last().unwrap(), suffix, "json");
return format!("{}{}.{}", paths.last().unwrap(), filename_suffix, "json");
}

pub fn json_filename(text: &str) -> String {
json_filename_suffix(text, None)
}

pub fn uml_filename(text: &str) -> String {
filename_suffix(text, None, Some("uml"))
}

pub fn uri_to_path(url: &str) -> PathBuf {
let uri_path = match Url::parse(url) {
Ok(url) => url,
Expand Down
11 changes: 7 additions & 4 deletions plugins/coco_struct_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,19 @@ mod tests {

execute(config);

let output_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
let base_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join(".coco")
.join("reporter")
.join("struct_analysis")
.join("source.json");
.join("struct_analysis");
let output_dir = base_dir.join("source.json");

let mut file = File::open(output_dir).unwrap();
let mut code = String::new();
file.read_to_string(&mut code).unwrap();
let classes: Vec<ClassInfo> = serde_json::from_str(&code).unwrap();
assert_eq!(6, classes.len());
assert_eq!(9, classes.len());

let output_dir = base_dir.join("source.uml");
let _file = File::open(output_dir).unwrap();
}
}
12 changes: 6 additions & 6 deletions plugins/coco_struct_analysis/src/plantuml_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ pub trait PlantUml {
pub struct PlantUmlRender;

impl PlantUmlRender {
pub fn render(classes: Vec<ClassInfo>) -> String {
pub fn render(classes: &Vec<ClassInfo>) -> String {
let mut rendered: Vec<String> = vec![];
for clazz in classes {
let mut members = vec![];
for member in clazz.members {
for member in &clazz.members {
members.push(format!(" {}{}\n", member.access, member.name))
}
let mut methods = vec![];
let mut content = format!("{}", members.join(""));
for method in clazz.methods {
for method in &clazz.methods {
methods.push(format!(" {}{}()\n", method.access, method.name))
}
content = format!("{}{}", content, methods.join(""));
Expand All @@ -42,7 +42,7 @@ mod tests {
#[test]
fn should_render_empty() {
let classes = vec![];
let str = PlantUmlRender::render(classes);
let str = PlantUmlRender::render(&classes);
assert_eq!("@startuml\n\n\n\n@enduml", str);
}

Expand All @@ -52,7 +52,7 @@ mod tests {
let demo = ClassInfo::new("Demo");
classes.push(demo);

let str = PlantUmlRender::render(classes);
let str = PlantUmlRender::render(&classes);
assert_eq!("@startuml\n\nclass {\n}\n\n@enduml", str);
}

Expand All @@ -69,7 +69,7 @@ mod tests {

classes.push(demo);

let str = PlantUmlRender::render(classes);
let str = PlantUmlRender::render(&classes);
assert_eq!(
"@startuml\n\nclass {\n -demo\n -method()\n}\n\n@enduml",
str
Expand Down
9 changes: 9 additions & 0 deletions plugins/coco_struct_analysis/src/struct_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::cmd_ctags::CmdCtags;
use crate::coco_struct::ClassInfo;
use crate::ctags_opt::Opt;
use crate::ctags_parser::CtagsParser;
use crate::plantuml_render::PlantUmlRender;

pub fn execute(config: CocoConfig) {
for repo in &config.repos {
Expand All @@ -36,6 +37,7 @@ pub fn execute(config: CocoConfig) {
let classes = run_ctags(&opt, &files);

let result = serde_json::to_string_pretty(&classes).unwrap();
write_to_uml_file(url_str, &classes);
write_to_json_file(url_str, &result);
}
}
Expand All @@ -55,6 +57,13 @@ fn write_to_json_file(url_str: &str, result: &String) {
fs::write(output_file, result).expect("cannot write file");
}

fn write_to_uml_file(url_str: &str, classes: &Vec<ClassInfo>) {
let file_name = url_format::uml_filename(url_str);
let output_file = Settings::struct_analysis().join(file_name);
let result = PlantUmlRender::render(classes);
fs::write(output_file, result).expect("cannot write file");
}

fn run_ctags(opt: &Opt, files: &Vec<String>) -> Vec<ClassInfo> {
let outputs = CmdCtags::call(&opt, &files).unwrap();
let mut iters = Vec::new();
Expand Down

0 comments on commit 560faca

Please sign in to comment.