Skip to content

Commit

Permalink
feat(psa): some psa of jvm impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynfeng committed Feb 23, 2021
1 parent 60b2967 commit 0728810
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
45 changes: 41 additions & 4 deletions psa/src/jvm/psa_jvm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::project_structure_analyzer::StructureAnalyzer;
use crate::psa_project::Project;
use std::path::Path;
use walkdir::WalkDir;

pub struct JvmProjectStructureAnalyzer {}

Expand All @@ -10,11 +12,46 @@ impl Default for JvmProjectStructureAnalyzer {
}

impl StructureAnalyzer for JvmProjectStructureAnalyzer {
fn analysis(&self, _project_path: &str) -> Project {
Project::new("test", "test/path")
fn analysis(&self, project_path: &str) -> Project {
let project_name = get_project_name(project_path);
Project::new(project_name, project_path)
}

fn is_related(&self) -> bool {
true
fn is_related(&self, project_path: &str) -> bool {
let files = list_file_names(project_path);
for file_name in files.iter() {
if is_build_file(file_name) {
return true;
}
}
false
}
}

fn get_project_name(_project_path: &str) -> &str {
"test"
}

fn is_build_file(file_name: &str) -> bool {
match file_name {
"build.gradle" => true,
"pom.xml" => true,
_ => false,
}
}

fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> {
let mut files = 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_file() {
files.push(entry.file_name().to_os_string().into_string().unwrap());
}
}
files
}
45 changes: 37 additions & 8 deletions psa/src/project_structure_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
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;
fn is_related(&self) -> bool;
fn is_related(&self, project_path: &str) -> bool;
}

pub struct ProjectAnalyzer {
Expand All @@ -13,10 +15,13 @@ pub struct ProjectAnalyzer {
impl ProjectAnalyzer {
pub fn run(&self, path: &str) -> Vec<Project> {
let mut projects = Vec::new();
for analyzer in self.analyzers.iter() {
match analyzer.is_related() {
true => projects.push(analyzer.analysis(path)),
_ => continue,
let dirs = first_level_dirs(Path::new(path));
for each_dir in dirs.iter() {
for analyzer in self.analyzers.iter() {
match analyzer.is_related(each_dir) {
true => projects.push(analyzer.analysis(each_dir)),
_ => continue,
}
}
}
projects
Expand All @@ -31,17 +36,41 @@ impl Default for ProjectAnalyzer {
}
}

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_run_analyzer() {
let project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf()
.join("_fixtures")
.join("projects")
.join("java")
.clone();
let analyzer = ProjectAnalyzer::default();

let projects = analyzer.run("");
let projects = analyzer.run(project_dir.display().to_string().as_str());

assert_eq!(projects.len(), 1);
assert_eq!(projects.get(0).unwrap().name, "test".to_string())
assert_eq!(projects.len() >= 2, true);
}
}

0 comments on commit 0728810

Please sign in to comment.