Skip to content

Commit

Permalink
feat(psa): module has sub module.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynfeng committed Feb 25, 2021
1 parent 3ec749c commit a3ea555
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion psa/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ pub fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> {
files
}

pub fn list_dirs<P: AsRef<Path>>(path: P) -> Vec<String> {
pub fn list_sub_dirs<P: AsRef<Path>>(path: P) -> Vec<String> {
let mut dirs = Vec::new();
let walk_dir = WalkDir::new(path);
for dir_entry in walk_dir
.min_depth(1)
.max_depth(1)
.sort_by(|a, b| a.file_name().cmp(b.file_name()))
.into_iter()
Expand Down
34 changes: 29 additions & 5 deletions psa/src/jvm/maven_module.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
use std::path::Path;

use crate::files::list_file_names;
use crate::files::{list_file_names, list_sub_dirs};
use crate::jvm::psa_jvm::ModuleAnalyzer;
use crate::{Module, Project};

pub struct MavenModuleAnalyzer {}

impl MavenModuleAnalyzer {
fn analysis_sub_modules(&self, module_path: &str) -> Vec<Module> {
let mut sub_modules = Vec::new();
let sub_dirs = list_sub_dirs(Path::new(module_path));
for each_sub_dir in sub_dirs.iter() {
let sub_module = self.analysis(each_sub_dir);
match sub_module {
Some(sub_module) => sub_modules.push(sub_module),
_ => continue,
}
}
sub_modules
}
}

impl ModuleAnalyzer for MavenModuleAnalyzer {
fn analysis(&self, module_path: &str) -> Option<Module> {
let module_name = get_module_name(module_path);
match has_build_file(module_path) {
true => Some(Module::new(module_name.as_str(), module_path)),
_ => None,
let mut module = create_module(module_path);
if !module.is_none() {
let sub_modules = &mut self.analysis_sub_modules(module_path);
module.as_mut().unwrap().add_sub_modules(sub_modules);
}
module
}

fn is_related(&self, project: &Project) -> bool {
project.project_type == "maven"
}
}

fn create_module(module_path: &str) -> Option<Module> {
let module_name = get_module_name(module_path);
match has_build_file(module_path) {
true => Some(Module::new(module_name.as_str(), module_path)),
_ => None,
}
}

fn has_build_file(module_path: &str) -> bool {
let file_names = list_file_names(module_path);
for file_name in file_names.iter() {
Expand Down
22 changes: 10 additions & 12 deletions psa/src/jvm/psa_jvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ pub struct JvmProjectStructureAnalyzer {
impl JvmProjectStructureAnalyzer {
fn analysis_modules(&self, project: &Project) -> Vec<Module> {
let mut modules = Vec::new();
let dirs = files::list_dirs(Path::new(&project.path));
for each_dir in dirs.iter() {
let module = self.analysis_module(project, each_dir);
match module {
Some(module) => modules.push(module),
_ => continue,
}
let module = self.analysis_module(project);
match module {
Some(module) => modules.push(module),
_ => (),
}
modules
}

fn analysis_module(&self, project: &Project, module_path: &String) -> Option<Module> {
fn analysis_module(&self, project: &Project) -> Option<Module> {
for module_analyzer in self.module_analyzers.iter() {
return match module_analyzer.is_related(project) {
true => module_analyzer.analysis(module_path),
true => module_analyzer.analysis(&project.path),
_ => continue,
};
}
Expand Down Expand Up @@ -123,9 +120,10 @@ mod tests {

let modules = project.modules;
let project_module = modules.get(0).unwrap();
let module1 = modules.get(1).unwrap();
let module2 = modules.get(2).unwrap();
assert_eq!(modules.len(), 3);
let module1 = project_module.sub_modules.get(0).unwrap();
let module2 = project_module.sub_modules.get(1).unwrap();
assert_eq!(modules.len(), 1);
assert_eq!(project_module.sub_modules.len(), 2);
assert_eq!(project.project_type, "maven");
assert_eq!(project_module.name, "multi_mod_maven_project");
assert_eq!(module1.name, "module1");
Expand Down
4 changes: 4 additions & 0 deletions psa/src/psa_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Module {
self.sub_modules.push(sub_module);
}

pub fn add_sub_modules(&mut self, sub_modules: &mut Vec<Module>) {
self.sub_modules.append(sub_modules);
}

pub fn new(name: &str, path: &str) -> Self {
Module {
name: name.to_string(),
Expand Down

0 comments on commit a3ea555

Please sign in to comment.