added project parser
This commit is contained in:
parent
decd49cf01
commit
3b170f7866
8 changed files with 57 additions and 8 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
|
use std::path::Path;
|
||||||
|
use undeepend::maven::project_parser::parse_project;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let message = &"xmlns:Hello, world!"[6..];
|
let project = parse_project(Path::new("tests/maven/resources/sample_project")).unwrap();
|
||||||
println!("{}",message);
|
println!("{:?}", project);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,3 +2,4 @@ pub mod metadata;
|
||||||
pub mod pom;
|
pub mod pom;
|
||||||
pub mod pom_view;
|
pub mod pom_view;
|
||||||
pub mod pom_parser;
|
pub mod pom_parser;
|
||||||
|
pub mod project_parser;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ pub struct Pom {
|
||||||
pub group_id: Option<String>,
|
pub group_id: Option<String>,
|
||||||
pub artifact_id: String,
|
pub artifact_id: String,
|
||||||
pub version: Option<String>,
|
pub version: Option<String>,
|
||||||
pub name: String,
|
pub name: Option<String>,
|
||||||
pub packaging: Option<String>,
|
pub packaging: Option<String>,
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
pub dependencies: Vec<Dependency>,
|
pub dependencies: Vec<Dependency>,
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use crate::xml::SaxError;
|
||||||
use crate::xml::dom_parser::{Node, get_document};
|
use crate::xml::dom_parser::{Node, get_document};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn get_pom(xml: &str) -> Result<Pom, SaxError> {
|
pub fn get_pom(xml: impl Into<String>) -> Result<Pom, SaxError> {
|
||||||
let mut group_id = None;
|
let mut group_id = None;
|
||||||
let mut artefact_id = None;
|
let mut artefact_id = None;
|
||||||
let mut parent = None;
|
let mut parent = None;
|
||||||
|
|
@ -16,7 +16,7 @@ pub fn get_pom(xml: &str) -> Result<Pom, SaxError> {
|
||||||
let mut properties = HashMap::new(); // useless assignment...
|
let mut properties = HashMap::new(); // useless assignment...
|
||||||
let mut modules = vec![]; // not useless assignment...
|
let mut modules = vec![]; // not useless assignment...
|
||||||
|
|
||||||
for child in get_document(xml)?.root.children {
|
for child in get_document(xml.into().as_str())?.root.children {
|
||||||
match child.name.as_str() {
|
match child.name.as_str() {
|
||||||
"groupId" => group_id = child.text,
|
"groupId" => group_id = child.text,
|
||||||
"artifactId" => artefact_id = child.text,
|
"artifactId" => artefact_id = child.text,
|
||||||
|
|
@ -37,7 +37,7 @@ pub fn get_pom(xml: &str) -> Result<Pom, SaxError> {
|
||||||
group_id,
|
group_id,
|
||||||
artifact_id: artefact_id.unwrap(),
|
artifact_id: artefact_id.unwrap(),
|
||||||
version,
|
version,
|
||||||
name: name.unwrap(),
|
name,
|
||||||
packaging,
|
packaging,
|
||||||
url,
|
url,
|
||||||
dependencies,
|
dependencies,
|
||||||
|
|
|
||||||
41
src/maven/project_parser.rs
Normal file
41
src/maven/project_parser.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
use crate::maven::pom::Pom;
|
||||||
|
use crate::maven::pom_parser::get_pom;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
pub fn parse_project(project_dir: &Path) -> Result<Project, String> {
|
||||||
|
if !project_dir.is_dir() {
|
||||||
|
return Err(format!("{:?} is not a directory", project_dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pom_file = project_dir.to_path_buf();
|
||||||
|
pom_file.push(Path::new("pom.xml"));
|
||||||
|
|
||||||
|
let pom_file = fs::read_to_string(pom_file).map_err(|e| e.to_string())?;
|
||||||
|
let root = get_pom(pom_file).map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
let modules= root.modules
|
||||||
|
.iter()
|
||||||
|
.map(|module| read_module_pom(project_dir, module))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(Project {
|
||||||
|
root,
|
||||||
|
modules,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_module_pom(project_dir: &Path, module: &String) -> Pom {
|
||||||
|
let mut module_file = project_dir.to_path_buf();
|
||||||
|
module_file.push(Path::new(module));
|
||||||
|
module_file.push(Path::new("pom.xml"));
|
||||||
|
let module_pom = fs::read_to_string(module_file)
|
||||||
|
.expect(format!("Cannot read file {}", module).as_str());
|
||||||
|
get_pom(module_pom).expect(format!("Cannot create module pom {}", module).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Project {
|
||||||
|
pub root: Pom,
|
||||||
|
pub modules: Vec<Pom>,
|
||||||
|
}
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
mod pom_parser_test;
|
mod pom_parser_test;
|
||||||
|
mod project_parser_test;
|
||||||
|
|
@ -4,7 +4,7 @@ use undeepend::maven::pom_parser::get_pom;
|
||||||
fn test_pom_parser_is_correct() {
|
fn test_pom_parser_is_correct() {
|
||||||
let test_xml = include_str!("../maven/resources/pom.xml");
|
let test_xml = include_str!("../maven/resources/pom.xml");
|
||||||
let pom = get_pom(test_xml).expect("failed to get document");
|
let pom = get_pom(test_xml).expect("failed to get document");
|
||||||
assert_eq!("Mockito",pom.name);
|
assert_eq!(Some("Mockito".to_string()),pom.name);
|
||||||
assert_eq!(Some("org.mockito".to_string()),pom.group_id);
|
assert_eq!(Some("org.mockito".to_string()),pom.group_id);
|
||||||
assert_eq!("mockito-core",pom.artifact_id);
|
assert_eq!("mockito-core",pom.artifact_id);
|
||||||
assert_eq!(Some("1.9.5".to_string()),pom.version);
|
assert_eq!(Some("1.9.5".to_string()),pom.version);
|
||||||
|
|
|
||||||
3
tests/maven/project_parser_test.rs
Normal file
3
tests/maven/project_parser_test.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
use std::path::Path;
|
||||||
|
use undeepend::maven::project_parser::parse_project;
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue