maven_rs/pom/editor/
build.rsuse edit_xml::Element;
use crate::{
editor::utils::{add_or_update_item, get_all_children_of_element},
editor::XMLEditorError,
pom::build::Plugin,
};
use super::PomEditor;
impl PomEditor {
pub fn get_or_create_build_element(&mut self) -> BuildEditor<'_> {
BuildEditor::new(self)
}
pub fn get_build_element_or_none(&mut self) -> Option<BuildEditor<'_>> {
if self.has_build() {
return Some(BuildEditor::new(self));
}
None
}
pub fn has_build(&self) -> bool {
let root = self.root();
root.find(&self.document, "build").is_some()
}
pub fn delete_build(&mut self) -> Result<bool, XMLEditorError> {
let root = self.root();
let element = root.find(&self.document, "build");
if let Some(element) = element {
element.detach(&mut self.document)?;
Ok(true)
} else {
Ok(false)
}
}
}
#[derive(Debug)]
pub struct BuildEditor<'a> {
parent: &'a mut PomEditor,
build_element: Element,
}
macro_rules! top_level_getter_setter {
(
$set:ident, $get:ident, $name:literal
) => {
pub fn $set(&mut self, value: &str) {
let element = crate::editor::utils::get_or_create_top_level_element(
$name,
&mut self.parent.document,
self.build_element,
);
element.set_text_content(&mut self.parent.document, value);
}
pub fn $get(&self) -> Option<String> {
let element = self.build_element.find(&self.parent.document, $name);
return element.map(|x| x.text_content(&self.parent.document));
}
};
}
impl<'a> BuildEditor<'a> {
top_level_getter_setter!(
set_source_directory,
get_source_directory,
"sourceDirectory"
);
top_level_getter_setter!(set_final_name, get_final_name, "finalName");
top_level_getter_setter!(set_directory, get_directory, "directory");
top_level_getter_setter!(set_default_goal, get_default_goal, "defaultGoal");
pub(super) fn new(parent: &'a mut PomEditor) -> Self {
let root = parent.root();
let build_element = crate::editor::utils::get_or_create_top_level_element(
"build",
&mut parent.document,
root,
);
Self {
parent,
build_element,
}
}
pub fn get_plugins(&self) -> Result<Vec<Plugin>, XMLEditorError> {
let Some(plugins) = self.build_element.find(&self.parent.document, "plugins") else {
return Ok(vec![]);
};
let result = get_all_children_of_element::<Plugin>(&self.parent.document, plugins)?;
Ok(result.into_iter().map(|(depend, _)| depend).collect())
}
pub fn add_or_update_plugin(
&mut self,
plugin: Plugin,
) -> Result<Option<Plugin>, XMLEditorError> {
let plugins = self.build_element.find(&self.parent.document, "plugins");
add_or_update_item(
&mut self.parent.document,
plugins,
self.build_element,
plugin,
)
}
}
#[cfg(test)]
mod tests {
use crate::{
pom::editor::{build::Plugin, PomEditor},
types::Property,
};
#[test]
pub fn test_plugins() -> anyhow::Result<()> {
let mut editor = PomEditor::new_with_group_and_artifact("dev.wyatt-herkamp", "test");
{
let mut build_editor = editor.get_or_create_build_element();
build_editor.set_source_directory("src/main/java");
build_editor.set_final_name("test");
let plugin = Plugin {
group_id: Some("org.apache.maven.plugins".to_string()),
artifact_id: "maven-compiler-plugin".to_string(),
version: Some(Property::Literal("3.8.1".to_string())),
};
build_editor.add_or_update_plugin(plugin.clone())?;
let plugins = build_editor.get_plugins()?;
assert_eq!(plugins.len(), 1);
assert_eq!(plugins[0], plugin);
}
let value = editor.write_to_str()?;
println!("{}", value);
Ok(())
}
}