Skip to content

Commit

Permalink
feat(psa): module analyzer framework and fake impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynfeng committed Feb 24, 2021
1 parent c104cf3 commit 785da51
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 30 deletions.
Empty file.
82 changes: 80 additions & 2 deletions psa/src/jvm/psa_jvm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
use crate::project_structure_analyzer::StructureAnalyzer;
use crate::psa_project::Project;
use crate::Module;
use std::path::Path;
use walkdir::WalkDir;

trait ModuleAnalyzer {
fn analysis(module_path: &str) -> Option<Module>;
fn is_related(&self, project: &Project) -> bool;
}

pub struct JvmProjectStructureAnalyzer {}

impl JvmProjectStructureAnalyzer {
fn analysis_modules(&self) -> Vec<Module> {
vec![
Module::new("multi_mod_maven_project", "foo"),
Module::new("module1", "foo"),
Module::new("module2", "foo"),
]
}
}

impl Default for JvmProjectStructureAnalyzer {
fn default() -> Self {
JvmProjectStructureAnalyzer {}
Expand All @@ -14,7 +30,13 @@ impl Default for JvmProjectStructureAnalyzer {
impl StructureAnalyzer for JvmProjectStructureAnalyzer {
fn analysis(&self, project_path: &str) -> Project {
let project_name = get_project_name(project_path);
Project::new(project_name.as_str(), project_path)
let build_file = get_build_file(project_path).unwrap();
let project_type = get_project_type(build_file);

let mut project = Project::new(project_name.as_str(), project_path, project_type.as_str());
let modules = &mut self.analysis_modules();
project.add_modules(modules);
project
}

fn is_related(&self, project_path: &str) -> bool {
Expand All @@ -28,6 +50,18 @@ impl StructureAnalyzer for JvmProjectStructureAnalyzer {
}
}

fn get_project_type(build_file: String) -> String {
return match build_file.as_str() {
"pom.xml" => "maven".to_string(),
_ => "UnKnow".to_string(),
};
}

fn get_build_file(path: &str) -> Option<String> {
let files = list_file_names(Path::new(path));
files.into_iter().find(|file| is_build_file(file))
}

fn get_project_name(project_path: &str) -> String {
Path::new(project_path)
.file_name()
Expand All @@ -39,8 +73,8 @@ fn get_project_name(project_path: &str) -> String {

fn is_build_file(file_name: &str) -> bool {
match file_name {
"build.gradle" => true,
"pom.xml" => true,
"build.gradle" => true,
_ => false,
}
}
Expand All @@ -60,3 +94,47 @@ fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> {
}
files
}

#[allow(dead_code)]
fn first_level_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.max_depth(1).into_iter() {
if dir_entry.is_err() {
panic!("{}", dir_entry.err().unwrap());
}

let entry = dir_entry.unwrap();
if entry.metadata().unwrap().is_dir() {
dirs.push(entry.path().display().to_string())
}
}
dirs
}

#[cfg(test)]
mod tests {
use crate::jvm::psa_jvm::JvmProjectStructureAnalyzer;
use crate::project_structure_analyzer::StructureAnalyzer;
use std::path::PathBuf;

#[test]
fn should_analysis_maven_project_sub_modules() {
let project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
.join("_fixtures")
.join("projects")
.join("java")
.join("multi_mod_maven_project")
.clone();

let analyzer = JvmProjectStructureAnalyzer::default();

let project = analyzer.analysis(project_dir.display().to_string().as_str());

assert_eq!(project.project_type, "maven");
assert_eq!(project.modules.len(), 3);
}
}
29 changes: 4 additions & 25 deletions psa/src/project_structure_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::jvm::psa_jvm::JvmProjectStructureAnalyzer;
use crate::psa_project::Project;
use std::path::Path;
use walkdir::WalkDir;

pub trait StructureAnalyzer {
fn analysis(&self, project_path: &str) -> Project;
Expand Down Expand Up @@ -32,31 +30,14 @@ impl Default for ProjectAnalyzer {
}
}

#[allow(dead_code)]
fn first_level_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.max_depth(1).into_iter() {
if dir_entry.is_err() {
panic!("{}", dir_entry.err().unwrap());
}

let entry = dir_entry.unwrap();
if entry.metadata().unwrap().is_dir() {
dirs.push(entry.path().display().to_string())
}
}
dirs
}

#[cfg(test)]
mod tests {
use crate::ProjectAnalyzer;
use std::path::PathBuf;

#[test]
fn should_analysis_project() {
let project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
let project_dir_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
Expand All @@ -66,13 +47,11 @@ mod tests {
.join("simple")
.clone();
let analyzer = ProjectAnalyzer::default();

let project = analyzer
.run(project_dir.display().to_string().as_str())
.unwrap();
let project_dir = project_dir_path.display().to_string();
let project = analyzer.run(project_dir.as_str()).unwrap();

assert_eq!(project.name, "simple");
assert_eq!(project.path.contains("simple"), true);
assert_eq!(project.path, project_dir.as_str());
}

#[test]
Expand Down
13 changes: 10 additions & 3 deletions psa/src/psa_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ pub struct Project {
pub name: String,
pub path: String,
pub modules: Vec<Module>,
pub project_type: String,
}

impl Project {
pub fn add_module(&mut self, module: Module) {
self.modules.push(module);
}

pub fn new(name: &str, path: &str) -> Self {
pub fn add_modules(&mut self, modules: &mut Vec<Module>) {
self.modules.append(modules)
}

pub fn new(name: &str, path: &str, project_type: &str) -> Self {
Project {
name: name.to_string(),
path: path.to_string(),
modules: vec![],
project_type: project_type.to_string(),
}
}
}
Expand All @@ -26,16 +32,17 @@ mod tests {

#[test]
fn should_create_project() {
let project = Project::new("foo", "test/path");
let project = Project::new("foo", "test/path", "maven");

assert_eq!(project.name, "foo".to_string());
assert_eq!(project.path, "test/path".to_string());
assert_eq!(project.project_type, "maven".to_string());
assert_eq!(project.modules.is_empty(), true);
}

#[test]
fn should_add_modules() {
let mut project = Project::new("foo", "test/path");
let mut project = Project::new("foo", "test/path", "maven");

project.add_module(Module::new("module1", "test/path/module1"));
project.add_module(Module::new("module2", "test/path/module2"));
Expand Down

0 comments on commit 785da51

Please sign in to comment.