add support for parsing Maven project information (using effective pom)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum ProjectType {
|
||||
@@ -35,9 +36,9 @@ impl Project {
|
||||
|
||||
pub fn info(&self) -> ProjectInfo {
|
||||
match self.project_type {
|
||||
ProjectType::Maven => ProjectInfo {
|
||||
name: "placeholder".into(),
|
||||
version: "placeholder".into()
|
||||
ProjectType::Maven => {
|
||||
let effective_pom = self.read_effective_pom();
|
||||
ProjectInfo::from_pom(&effective_pom)
|
||||
},
|
||||
ProjectType::Npm => ProjectInfo {
|
||||
name: "placeholder".into(),
|
||||
@@ -53,6 +54,21 @@ impl Project {
|
||||
.exec()
|
||||
.expect("metadata call failed")
|
||||
}
|
||||
|
||||
fn read_effective_pom(&self) -> sxd_document::Package {
|
||||
let full_output = std::str::from_utf8(&Command::new("mvn")
|
||||
.arg("help:effective-pom")
|
||||
.current_dir(&self.path)
|
||||
.output()
|
||||
.expect("failed to generate effective pom")
|
||||
.stdout).expect("failed to generate string from output").to_owned();
|
||||
let xml = full_output.get(
|
||||
full_output.find("<?xml").expect("failed to find start of xml")
|
||||
..
|
||||
full_output.find("</project>").expect("failed to find end of xml") + 10
|
||||
).expect("failed to extract xml body");
|
||||
sxd_document::parser::parse(xml).expect("failed to parse effective pom")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -77,6 +93,32 @@ impl From<cargo_metadata::Metadata> for ProjectInfo {
|
||||
}
|
||||
}
|
||||
|
||||
impl ProjectInfo {
|
||||
fn from_pom(pom: &sxd_document::Package) -> Self {
|
||||
let document = pom.as_document();
|
||||
|
||||
let mut context = sxd_xpath::context::Context::new();
|
||||
context.set_namespace("mvn", "http://maven.apache.org/POM/4.0.0");
|
||||
|
||||
let factory = sxd_xpath::Factory::new();
|
||||
let name_xpath = factory.build("/mvn:project/mvn:name")
|
||||
.expect("failed to compile name xpath")
|
||||
.expect("No XPath was compiled");
|
||||
let version_xpath = factory.build("/mvn:project/mvn:version")
|
||||
.expect("failed to compile name xpath")
|
||||
.expect("No XPath was compiled");
|
||||
|
||||
ProjectInfo {
|
||||
name: name_xpath.evaluate(&context, document.root())
|
||||
.expect("failed to evaluate name expression")
|
||||
.into_string(),
|
||||
version: version_xpath.evaluate(&context, document.root())
|
||||
.expect("failed to evaluate version expression")
|
||||
.into_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Scouter {
|
||||
search_path: PathBuf
|
||||
}
|
||||
@@ -129,4 +171,14 @@ mod test {
|
||||
assert_eq!(env!("CARGO_PKG_NAME"), project_info.name);
|
||||
assert_eq!(env!("CARGO_PKG_VERSION"), project_info.version);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_maven_project() {
|
||||
let mut scouter = Scouter::new(format!("{}/testdata/maven", env!("CARGO_MANIFEST_DIR")));
|
||||
let project = scouter.next().unwrap();
|
||||
let project_info = project.info();
|
||||
assert_eq!(ProjectType::Maven, project.project_type);
|
||||
assert_eq!("Shenlong Maven Test Project", project_info.name);
|
||||
assert_eq!("1.0.0-SNAPSHOT", project_info.version);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user