use anyhow::Result;
use std::path::Path;
pub struct MavenMigrator;
impl MavenMigrator {
pub fn migrate(pom_path: &Path) -> Result<MigrationResult> {
if !pom_path.exists() {
return Err(anyhow::anyhow!(
"POM file not found: {}",
pom_path.display()
));
}
let pom_content = std::fs::read_to_string(pom_path)?;
let _document: roxmltree::Document = roxmltree::Document::parse(&pom_content)?;
let config = Self::generate_jbuild_config(&pom_content)?;
Ok(MigrationResult {
config,
warnings: vec![],
notes: vec![
"Review and adjust dependencies as needed".to_string(),
"Update plugin configurations for jbuild".to_string(),
],
})
}
fn generate_jbuild_config(_pom: &str) -> Result<String> {
let mut config = String::from("# Generated by jbuild Maven migrator\n\n");
config.push_str("[package]\n");
config.push_str("name = \"project-name\"\n");
config.push_str("version = \"1.0.0\"\n\n");
config.push_str("[dependencies]\n");
config.push_str("# Add dependencies here\n\n");
config.push_str("[dev-dependencies]\n");
config.push_str("# Add dev dependencies here\n");
Ok(config)
}
pub fn convert_dependency(gav: &str) -> String {
let parts: Vec<&str> = gav.split(':').collect();
match parts.as_slice() {
[group, artifact, version] => {
format!(
r#"{{ group = "{}", artifact = "{}", version = "{}" }}"#,
group, artifact, version
)
}
_ => format!("# Invalid dependency: {}", gav),
}
}
}
#[derive(Debug, Clone)]
pub struct MigrationResult {
pub config: String,
pub warnings: Vec<String>,
pub notes: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_convert_dependency() {
let result = MavenMigrator::convert_dependency("org.junit.jupiter:junit-jupiter:5.10.0");
assert!(result.contains("org.junit.jupiter"));
assert!(result.contains("junit-jupiter"));
assert!(result.contains("5.10.0"));
}
}