From 523764502ae1c4f778931cdc3302701e62739b5f Mon Sep 17 00:00:00 2001 From: Wikum Weerakutti Date: Tue, 27 Aug 2024 22:35:57 +0530 Subject: [PATCH] O3-3434: SDK Command: Remove dependencies from distro.properties --- integration-tests/pom.xml | 4 ++ .../maven/plugins/RemoveDependencyTest.java | 51 ++++++++++++++++ .../maven/plugins/RemoveDependency.java | 61 +++++++++++++++++++ .../maven/plugins/model/DistroProperties.java | 4 ++ 4 files changed, 120 insertions(+) create mode 100644 integration-tests/src/test/java/org/openmrs/maven/plugins/RemoveDependencyTest.java create mode 100644 maven-plugin/src/main/java/org/openmrs/maven/plugins/RemoveDependency.java diff --git a/integration-tests/pom.xml b/integration-tests/pom.xml index 7c7859921..07986a2b8 100644 --- a/integration-tests/pom.xml +++ b/integration-tests/pom.xml @@ -42,5 +42,9 @@ hamcrest-library test + + org.mockito + mockito-core + diff --git a/integration-tests/src/test/java/org/openmrs/maven/plugins/RemoveDependencyTest.java b/integration-tests/src/test/java/org/openmrs/maven/plugins/RemoveDependencyTest.java new file mode 100644 index 000000000..a5b93df3b --- /dev/null +++ b/integration-tests/src/test/java/org/openmrs/maven/plugins/RemoveDependencyTest.java @@ -0,0 +1,51 @@ +package org.openmrs.maven.plugins; + +import org.apache.maven.plugin.MojoExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.maven.plugins.model.DistroProperties; +import org.openmrs.maven.plugins.utility.DistroHelper; + +import java.io.File; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class RemoveDependencyTest extends AbstractSdkIntegrationTest { + + private String distroFile; + + + private DistroProperties originalProperties; + + @Before + public void setUp() { + distroFile = testDirectory + File.separator + "openmrs-distro.properties"; + originalProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); + } + + @Test + public void shouldRemoveExistingDependency() throws Exception { + DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); + assertNotNull(distroProperties); + + assertTrue(distroProperties.getAllKeys().contains("omod.uicommons")); + + addTaskParam("distro", distroFile); + addTaskParam("property", "omod.uicommons"); + + executeTask("rm-dependency"); + assertSuccess(); + + distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); + assertFalse(distroProperties.getAllKeys().contains("omod.uicommons")); + } + + @After + public void reset() throws MojoExecutionException { + originalProperties.saveTo(new File(distroFile).getParentFile()); + } + +} diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/RemoveDependency.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/RemoveDependency.java new file mode 100644 index 000000000..56388218f --- /dev/null +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/RemoveDependency.java @@ -0,0 +1,61 @@ +package org.openmrs.maven.plugins; + +import org.apache.commons.lang.StringUtils; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.openmrs.maven.plugins.model.DistroProperties; + +import java.io.File; +import java.util.Properties; +import java.util.stream.Collectors; + +@Mojo(name = "rm-dependency", requiresProject = false) +public class RemoveDependency extends AbstractTask { + + /** + * Path to the openmrs-distro.properties file to modify + */ + @Parameter(property = "distro") + private String distro; + + /** + * Name of the property you want to remove + */ + @Parameter(property = "property") + private String property; + + @Override + public void executeTask() throws MojoExecutionException, MojoFailureException { + + if (distro == null) { + File userDir = new File(System.getProperty("user.dir")); + File distroFile = new File(userDir, DistroProperties.DISTRO_FILE_NAME); + if (distroFile.exists()) { + distro = distroFile.getAbsolutePath(); + } + } + + Properties properties = new Properties(); + + if(StringUtils.isNotBlank(distro)) { + properties = distroHelper.resolveDistroPropertiesForStringSpecifier(distro, versionsHelper).getProperties(); + + } + + if(StringUtils.isBlank(property)) { + property = wizard.promptForMissingValueWithOptions("Enter the property you want to remove", + null, null, properties.keySet().stream().map(prop -> (String) prop).collect(Collectors.toList())); + } + + if (StringUtils.isNotBlank(property)) { + if(!properties.containsKey(property)) { + throw new MojoExecutionException("The property " + property + " was not found in the distro"); + } + properties.remove(property); + DistroProperties distroProperties = new DistroProperties(properties); + distroProperties.saveTo(new File(distro).getParentFile()); + } + } +} diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java index 09f1e1e53..d57ddad1e 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java @@ -271,6 +271,10 @@ public List getExclusions() { return Arrays.asList(exclusions.split(",")); } + public Properties getProperties() { + return properties; + } + private String getPlaceholderKey(String string){ int startIndex = string.indexOf("${")+2; int endIndex = string.indexOf("}", startIndex);