Skip to content

Commit

Permalink
feat(psa): module content root detect.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynfeng committed Feb 25, 2021
1 parent 049dd65 commit 9b20141
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 17 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions psa/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::Path;
use std::path::PathBuf;

use std::collections::HashSet;
use walkdir::WalkDir;

pub fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> {
Expand All @@ -18,6 +20,23 @@ pub fn list_file_names<P: AsRef<Path>>(path: P) -> Vec<String> {
files
}

pub fn list_all<P: AsRef<Path>>(path: P) -> HashSet<String> {
let mut dirs = HashSet::new();
let walk_dir = WalkDir::new(path);
for dir_entry in walk_dir
.min_depth(1)
.sort_by(|a, b| a.file_name().cmp(b.file_name()))
.into_iter()
{
if dir_entry.is_err() {
panic!("{}", dir_entry.err().unwrap());
}

dirs.insert(dir_entry.unwrap().path().display().to_string());
}
dirs
}

pub fn list_sub_dirs<P: AsRef<Path>>(path: P) -> Vec<String> {
let mut dirs = Vec::new();
let walk_dir = WalkDir::new(path);
Expand All @@ -38,3 +57,23 @@ pub fn list_sub_dirs<P: AsRef<Path>>(path: P) -> Vec<String> {
}
dirs
}

pub fn find_in_path(root_path: &str, file: Vec<&str>) -> Option<String> {
let all_files = list_all(root_path);
let mut parent_path = PathBuf::from(root_path).to_path_buf();
for each_part in file.into_iter() {
parent_path.push(each_part);
}
match all_files.contains(&parent_path.display().to_string()) {
true => Some(parent_path.display().to_string()),
_ => None,
}
}

pub fn join_path(root_path: &str, file: Vec<&str>) -> String {
let mut parent_path = PathBuf::from(root_path).to_path_buf();
for each_part in file.into_iter() {
parent_path.push(each_part);
}
parent_path.display().to_string()
}
60 changes: 57 additions & 3 deletions psa/src/jvm/maven_module.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::path::Path;

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

pub struct MavenModuleAnalyzer {}

impl MavenModuleAnalyzer {
fn detect_sub_modules(&self, module_path: &str, module: &mut Option<Module>) {
let sub_modules = &mut self.analysis_sub_modules(module_path);
module.as_mut().unwrap().add_sub_modules(sub_modules);
}

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));
Expand All @@ -19,14 +24,63 @@ impl MavenModuleAnalyzer {
}
sub_modules
}

fn detect_content_root(&self, module_path: &str, mut module: &mut Option<Module>) {
self.detect_source_root(module_path, &mut module);
self.detect_resource_root(module_path, &mut module);
self.detect_test_source_root(module_path, &mut module);
self.detect_test_resource_root(module_path, &mut module);
}

fn detect_source_root(&self, module_path: &str, module: &mut Option<Module>) {
let path = module_path;
let source_root = find_in_path(path, vec!["src", "main", "java"]);
match source_root {
Some(source_root) => module.as_mut().unwrap().add_source_root(source_root),
_ => (),
}
}

fn detect_resource_root(&self, module_path: &str, module: &mut Option<Module>) {
let path = module_path;
let resource_root = find_in_path(path, vec!["src", "main", "resources"]);
match resource_root {
Some(resource_root) => module.as_mut().unwrap().add_resource_root(resource_root),
_ => (),
}
}

fn detect_test_source_root(&self, module_path: &str, module: &mut Option<Module>) {
let path = module_path;
let test_source_root = find_in_path(path, vec!["src", "test", "java"]);
match test_source_root {
Some(test_source_root) => module
.as_mut()
.unwrap()
.add_test_source_root(test_source_root),
_ => (),
}
}

fn detect_test_resource_root(&self, module_path: &str, module: &mut Option<Module>) {
let path = module_path;
let test_resource_root = find_in_path(path, vec!["src", "test", "resources"]);
match test_resource_root {
Some(test_resource_root) => module
.as_mut()
.unwrap()
.add_test_resource_root(test_resource_root),
_ => (),
}
}
}

impl ModuleAnalyzer for MavenModuleAnalyzer {
fn analysis(&self, module_path: &str) -> Option<Module> {
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);
self.detect_sub_modules(&module_path, &mut module);
self.detect_content_root(module_path, &mut module);
}
module
}
Expand Down
85 changes: 71 additions & 14 deletions psa/src/jvm/psa_jvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,18 @@ fn is_build_file(file_name: &str) -> bool {
mod tests {
use std::path::PathBuf;

use crate::files::join_path;
use crate::jvm::psa_jvm::JvmProjectStructureAnalyzer;
use crate::ProjectStructureAnalyzer;
use crate::{Project, ProjectStructureAnalyzer};

#[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());
let project = do_analysis(vec![
"_fixtures",
"projects",
"java",
"multi_mod_maven_project",
]);

let modules = project.modules;
let project_module = modules.get(0).unwrap();
Expand All @@ -129,4 +123,67 @@ mod tests {
assert_eq!(module1.name, "module1");
assert_eq!(module2.name, "module2");
}

#[test]
fn should_detect_project_model_content_root() {
let project = do_analysis(vec![
"_fixtures",
"projects",
"java",
"multi_mod_maven_project",
]);
let modules = project.modules;
let project_module = modules.get(0).unwrap();
let project_content_root = &project_module.content_root;

let expect_source_path =
join_path(project_module.path.as_str(), vec!["src", "main", "java"]);
assert_eq!(
project_content_root.source_root.get(0).unwrap().as_str(),
expect_source_path.as_str()
);

let expect_resource_path = join_path(
project_module.path.as_str(),
vec!["src", "main", "resources"],
);
assert_eq!(
project_content_root.resource_root.get(0).unwrap().as_str(),
expect_resource_path.as_str()
);

let expect_test_source_root =
join_path(project_module.path.as_str(), vec!["src", "test", "java"]);
assert_eq!(
project_content_root
.test_source_root
.get(0)
.unwrap()
.as_str(),
expect_test_source_root.as_str()
);

let expect_test_resources_root = join_path(
project_module.path.as_str(),
vec!["src", "test", "resources"],
);
assert_eq!(
project_content_root.test_resource_root.get(0).unwrap(),
expect_test_resources_root.as_str()
);
}

fn do_analysis(path: Vec<&str>) -> Project {
let mut project_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.to_path_buf();

for path in path.into_iter() {
project_dir.push(path);
}

let analyzer = JvmProjectStructureAnalyzer::default();
analyzer.analysis(project_dir.display().to_string().as_str())
}
}
18 changes: 18 additions & 0 deletions psa/src/psa_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ impl Module {
self.sub_modules.append(sub_modules);
}

pub fn add_source_root(&mut self, source_root: String) {
self.content_root.add_source_root(source_root.as_str());
}

pub fn add_resource_root(&mut self, resource_root: String) {
self.content_root.add_resource_root(resource_root.as_str());
}

pub fn add_test_source_root(&mut self, test_source_root: String) {
self.content_root
.add_test_source_root(test_source_root.as_str());
}

pub(crate) fn add_test_resource_root(&mut self, test_resource_root: String) {
self.content_root
.add_test_resource_root(test_resource_root.as_str());
}

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

0 comments on commit 9b20141

Please sign in to comment.