Skip to content

Commit

Permalink
feat(psa): module and content root path change to relative path.
Browse files Browse the repository at this point in the history
  • Loading branch information
ynfeng committed Feb 26, 2021
1 parent 41512a6 commit 5eb0a73
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 50 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions psa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ walkdir = "2"
regex = "1"

maplit="0.1.6"

pathdiff="0.2.0"
23 changes: 21 additions & 2 deletions psa/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pathdiff::diff_paths;
use std::collections::HashSet;
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 Down Expand Up @@ -77,3 +77,22 @@ pub fn join_path(root_path: &str, file: Vec<&str>) -> String {
}
parent_path.display().to_string()
}

pub fn to_relative_path(base_path: &str, absolute_path: &str) -> String {
diff_paths(absolute_path, base_path)
.unwrap()
.display()
.to_string()
}

#[cfg(test)]
mod tests {
use crate::files::to_relative_path;

#[test]
fn should_convert_absolute_path_to_relative_path() {
let relative_path = to_relative_path("/a/b/c", "/a/b/c/d/");

assert_eq!(relative_path, "d");
}
}
58 changes: 36 additions & 22 deletions psa/src/jvm/maven_module.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use std::path::Path;

use crate::files::{find_in_path, list_file_names, list_sub_dirs};
use crate::files::{find_in_path, list_file_names, list_sub_dirs, to_relative_path};
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);
fn detect_sub_modules(
&self,
project_path: &str,
module_path: &str,
module: &mut Option<Module>,
) {
let sub_modules = &mut self.analysis_sub_modules(project_path, module_path);
module.as_mut().unwrap().add_sub_modules(sub_modules);
}

fn analysis_sub_modules(&self, module_path: &str) -> Vec<Module> {
fn analysis_sub_modules(&self, project_path: &str, 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);
let sub_module = self.analysis(project_path, each_sub_dir);
match sub_module {
Some(sub_module) => sub_modules.push(sub_module),
_ => continue,
Expand All @@ -33,10 +38,12 @@ impl MavenModuleAnalyzer {
}

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"]);
let source_root = find_in_path(module_path, vec!["src", "main", "java"]);
match source_root {
Some(source_root) => module.as_mut().unwrap().add_source_root(source_root),
Some(source_root) => {
let relative_path = to_relative_path(module_path, source_root.as_str());
module.as_mut().unwrap().add_source_root(relative_path)
}
_ => (),
}
}
Expand All @@ -45,7 +52,10 @@ impl MavenModuleAnalyzer {
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),
Some(resource_root) => {
let relative_path = to_relative_path(module_path, resource_root.as_str());
module.as_mut().unwrap().add_resource_root(relative_path)
}
_ => (),
}
}
Expand All @@ -54,10 +64,10 @@ impl MavenModuleAnalyzer {
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),
Some(test_source_root) => {
let relative_path = to_relative_path(module_path, test_source_root.as_str());
module.as_mut().unwrap().add_test_source_root(relative_path)
}
_ => (),
}
}
Expand All @@ -66,20 +76,23 @@ impl MavenModuleAnalyzer {
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),
Some(test_resource_root) => {
let relative_path = to_relative_path(module_path, test_resource_root.as_str());
module
.as_mut()
.unwrap()
.add_test_resource_root(relative_path)
}
_ => (),
}
}
}

impl ModuleAnalyzer for MavenModuleAnalyzer {
fn analysis(&self, module_path: &str) -> Option<Module> {
let mut module = create_module(module_path);
fn analysis(&self, project_path: &str, module_path: &str) -> Option<Module> {
let mut module = create_module(project_path, module_path);
if !module.is_none() {
self.detect_sub_modules(&module_path, &mut module);
self.detect_sub_modules(project_path, &module_path, &mut module);
self.detect_content_root(module_path, &mut module);
}
module
Expand All @@ -90,10 +103,11 @@ impl ModuleAnalyzer for MavenModuleAnalyzer {
}
}

fn create_module(module_path: &str) -> Option<Module> {
fn create_module(project_path: &str, module_path: &str) -> Option<Module> {
let module_name = get_module_name(module_path);
let relative_path = to_relative_path(project_path, module_path);
match has_build_file(module_path) {
true => Some(Module::new(module_name.as_str(), module_path)),
true => Some(Module::new(module_name.as_str(), relative_path.as_str())),
_ => None,
}
}
Expand Down
29 changes: 10 additions & 19 deletions psa/src/jvm/psa_jvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::psa_project::Project;
use crate::{files, Module, ProjectStructureAnalyzer};

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

Expand All @@ -27,7 +27,7 @@ impl JvmProjectStructureAnalyzer {
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(&project.path),
true => module_analyzer.analysis(&project.absolute_path, &project.absolute_path),
_ => continue,
};
}
Expand Down Expand Up @@ -136,26 +136,21 @@ mod tests {
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"]);
let expect_source_path = join_path("", vec!["src", "main", "java"]);
assert_eq!(project_content_root.source_root.len(), 1);
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"],
);
let expect_resource_path = join_path("", vec!["src", "main", "resources"]);
assert_eq!(project_content_root.resource_root.len(), 1);
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"]);
let expect_test_source_root = join_path("", vec!["src", "test", "java"]);
assert_eq!(project_content_root.test_source_root.len(), 1);
assert_eq!(
project_content_root
Expand All @@ -166,10 +161,7 @@ mod tests {
expect_test_source_root.as_str()
);

let expect_test_resources_root = join_path(
project_module.path.as_str(),
vec!["src", "test", "resources"],
);
let expect_test_resources_root = join_path("", vec!["src", "test", "resources"]);
assert_eq!(project_content_root.test_resource_root.len(), 1);
assert_eq!(
project_content_root.test_resource_root.get(0).unwrap(),
Expand All @@ -191,26 +183,25 @@ mod tests {
let module1 = project_module.sub_modules.get(0).unwrap();
let content_root = &module1.content_root;

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

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

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

let expect_test_resources_root =
join_path(module1.path.as_str(), vec!["src", "test", "resources"]);
let expect_test_resources_root = join_path("", vec!["src", "test", "resources"]);
assert_eq!(
content_root.test_resource_root.get(0).unwrap(),
expect_test_resources_root.as_str()
Expand Down
2 changes: 1 addition & 1 deletion psa/src/project_structure_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod tests {
let project = analyzer.run(project_dir.as_str()).unwrap();

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

#[test]
Expand Down
6 changes: 3 additions & 3 deletions psa/src/psa_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::psa_library::Library;
#[derive(Serialize)]
pub struct Module {
pub name: String,
pub path: String,
pub relative_path: String,
pub facets: Vec<Facet>,
pub libraries: Vec<Library>,
pub sub_modules: Vec<Module>,
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Module {
pub fn new(name: &str, path: &str) -> Self {
Module {
name: name.to_string(),
path: path.to_string(),
relative_path: path.to_string(),
facets: vec![],
libraries: vec![],
sub_modules: vec![],
Expand All @@ -68,7 +68,7 @@ mod tests {
let module = Module::new("foo", "test/path");

assert_eq!(module.name, "foo".to_string());
assert_eq!(module.path, "test/path".to_string());
assert_eq!(module.relative_path, "test/path".to_string());
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions psa/src/psa_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::psa_module::Module;
#[derive(Serialize)]
pub struct Project {
pub name: String,
pub path: String,
pub absolute_path: String,
pub modules: Vec<Module>,
pub project_type: String,
}
Expand All @@ -20,7 +20,7 @@ impl Project {
pub fn new(name: &str, path: &str, project_type: &str) -> Self {
Project {
name: name.to_string(),
path: path.to_string(),
absolute_path: path.to_string(),
modules: vec![],
project_type: project_type.to_string(),
}
Expand All @@ -36,7 +36,7 @@ mod tests {
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.absolute_path, "test/path".to_string());
assert_eq!(project.project_type, "maven".to_string());
assert_eq!(project.modules.is_empty(), true);
}
Expand Down

0 comments on commit 5eb0a73

Please sign in to comment.