-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
O3-3433: SDK Command: Add dependencies to distro.properties
- Loading branch information
1 parent
393b7ba
commit b8784a5
Showing
6 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
integration-tests/src/test/java/org/openmrs/maven/plugins/AddDependencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.openmrs.maven.plugins; | ||
|
||
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.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class AddDependencyTest extends AbstractSdkIntegrationTest { | ||
|
||
private String distroFile; | ||
|
||
@Before | ||
public void setUp() { | ||
distroFile = testDirectory + File.separator + "add-dependency" + File.separator + "openmrs-distro.properties"; | ||
} | ||
|
||
@Test | ||
public void shouldAddOmodDependency() throws Exception { | ||
addTaskParam("distro", distroFile); | ||
addTaskParam("type", "OMOD"); | ||
addTaskParam("groupId", "org.openmrs.module"); | ||
addTaskParam("artifactId", "webservices.rest"); | ||
addTaskParam("version", "2.30.0"); | ||
|
||
executeTask("add-dependency"); | ||
assertSuccess(); | ||
|
||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
assertTrue(distroProperties.getAllKeys().contains("omod.webservices.rest")); | ||
assertEquals(distroProperties.getParam("omod.webservices.rest"), "2.30.0"); | ||
} | ||
|
||
@Test | ||
public void shouldAddSpaDependency() throws Exception { | ||
addTaskParam("distro", distroFile); | ||
addTaskParam("type", "SPA"); | ||
addTaskParam("moduleName", "esm-system-admin-app"); | ||
addTaskParam("version", "4.0.3"); | ||
|
||
executeTask("add-dependency"); | ||
assertSuccess(); | ||
|
||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
|
||
assertTrue(distroProperties.getAllKeys().contains("spa.frontendModules.@openmrs/esm-system-admin-app")); | ||
assertEquals(distroProperties.getParam("spa.frontendModules.@openmrs/esm-system-admin-app"), "4.0.3"); | ||
} | ||
|
||
@Test | ||
public void shouldAddOwaDependency() throws Exception { | ||
addTaskParam("distro", distroFile); | ||
addTaskParam("type", "OWA"); | ||
addTaskParam("artifactId", "sysadmin"); | ||
addTaskParam("version", "1.2.0"); | ||
|
||
executeTask("add-dependency"); | ||
assertSuccess(); | ||
|
||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
|
||
assertTrue(distroProperties.getAllKeys().contains("owa.sysadmin")); | ||
assertEquals(distroProperties.getParam("owa.sysadmin"), "1.2.0"); | ||
} | ||
|
||
@Test | ||
public void shouldAddWarDependency() throws Exception { | ||
addTaskParam("distro", distroFile); | ||
addTaskParam("type", "WAR"); | ||
addTaskParam("version", "2.6.1"); | ||
|
||
executeTask("add-dependency"); | ||
assertSuccess(); | ||
|
||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
|
||
assertTrue(distroProperties.getAllKeys().contains("war.openmrs")); | ||
assertEquals(distroProperties.getParam("war.openmrs"), "2.6.1"); | ||
} | ||
|
||
@Test | ||
public void shouldOverrideIfPropertyAlreadyExists() throws Exception { | ||
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework")); | ||
|
||
addTaskParam("distro", distroFile); | ||
addTaskParam("type", "OMOD"); | ||
addTaskParam("groupId", "org.openmrs.module"); | ||
addTaskParam("artifactId", "uiframework"); | ||
addTaskParam("version", "2.30.0"); | ||
|
||
executeTask("add-dependency"); | ||
assertSuccess(); | ||
|
||
distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile)); | ||
assertNotNull(distroProperties); | ||
|
||
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework")); | ||
assertEquals(distroProperties.getParam("omod.uiframework"), "2.30.0"); | ||
} | ||
|
||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...ration-tests/src/test/resources/integration-test/add-dependency/openmrs-distro.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#Mon Aug 19 16:25:48 IST 2024 | ||
name=Ref 3.x distro | ||
omod.uiframework=3.6 |
134 changes: 134 additions & 0 deletions
134
maven-plugin/src/main/java/org/openmrs/maven/plugins/AddDependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
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.Artifact; | ||
import org.openmrs.maven.plugins.model.DistroProperties; | ||
|
||
import org.openmrs.maven.plugins.utility.NPMHelper; | ||
import org.openmrs.maven.plugins.utility.SDKConstants; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
@Mojo(name = "add-dependency", requiresProject = false) | ||
public class AddDependency extends AbstractTask { | ||
|
||
/** | ||
* Path to the openmrs-distro.properties file to modify | ||
*/ | ||
@Parameter(property = "distro") | ||
private String distro; | ||
|
||
/** | ||
* Type of the dependency to add | ||
*/ | ||
@Parameter(property = "type") | ||
private String type; | ||
|
||
/** | ||
* Maven group id of the dependency | ||
*/ | ||
@Parameter(property = "groupId") | ||
private String groupId; | ||
|
||
/** | ||
* Maven artifact id of the dependency | ||
*/ | ||
@Parameter(property = "artifactId") | ||
private String artifactId; | ||
|
||
/** | ||
* Version of the dependency | ||
*/ | ||
@Parameter(property = "version") | ||
private String version; | ||
|
||
/** | ||
* Name of the frontend module | ||
*/ | ||
@Parameter(property = "moduleName") | ||
private String moduleName; | ||
|
||
|
||
private final String DEPENDENCY_TYPE_PROMPT = "Enter the type of dependency you need to add"; | ||
|
||
|
||
@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(); | ||
} | ||
} | ||
|
||
if(StringUtils.isBlank(type)) { | ||
List<String> dependencyTypes = new ArrayList<>(Arrays.asList("OMOD", "SPA", "OWA", "WAR")); | ||
type = wizard.promptForMissingValueWithOptions(DEPENDENCY_TYPE_PROMPT, null, null, dependencyTypes); | ||
} | ||
Properties properties = new Properties(); | ||
|
||
if(StringUtils.isNotBlank(distro)) { | ||
properties = distroHelper.resolveDistroPropertiesForStringSpecifier(distro, versionsHelper).getProperties(); | ||
} | ||
|
||
switch (type) { | ||
case "OMOD": | ||
groupId = wizard.promptForValueIfMissingWithDefault(null, groupId, "groupId", Artifact.GROUP_MODULE); | ||
artifactId = wizard.promptForValueIfMissing(artifactId, "artifactId"); | ||
if (StringUtils.isBlank(version)) { | ||
Artifact artifact = new Artifact(artifactId, "1.0", groupId); | ||
List<String> versions = versionsHelper.getSuggestedVersions(artifact, 5); | ||
version = wizard.promptForMissingValueWithOptions("Enter the version", null, null, versions, | ||
"Please specify the module version", null); | ||
} | ||
properties.setProperty("omod." + artifactId, version); | ||
break; | ||
case "SPA": | ||
moduleName = wizard.promptForValueIfMissing(moduleName, "frontend module name"); | ||
if (StringUtils.isBlank(version)) { | ||
List<String> versions = new NPMHelper().getPackageVersions(moduleName, 6); | ||
version = wizard.promptForMissingValueWithOptions("Enter the module version", null, null, versions, | ||
"Please specify the SPA version", null); | ||
} | ||
properties.setProperty("spa.frontendModules.@openmrs/" + moduleName, version); | ||
break; | ||
case "OWA": | ||
artifactId = wizard.promptForValueIfMissing(artifactId, "OWA name"); | ||
Artifact artifact = new Artifact(artifactId, "1.0", Artifact.GROUP_OWA); | ||
if(StringUtils.isBlank(version)) { | ||
List<String> suggestedVersions = versionsHelper.getSuggestedVersions(artifact, 6); | ||
version = wizard | ||
.promptForMissingValueWithOptions("Which version would you like to deploy?%s", null, "", suggestedVersions, | ||
"Please specify OWA version", null); | ||
} | ||
properties.setProperty("owa." + artifactId, version); | ||
break; | ||
case "WAR": | ||
Artifact platformArtifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID, | ||
SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO); | ||
if(StringUtils.isBlank(version)) { | ||
version = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(platformArtifact, 5)); | ||
} | ||
properties.setProperty("war.openmrs", version); | ||
} | ||
|
||
DistroProperties distroProperties = new DistroProperties(properties); | ||
|
||
if (StringUtils.isBlank(distro)) { | ||
wizard.showMessage("No distro.properpties file is provided. Generating a new openmrs-distro.properties file."); | ||
distro = new File(System.getProperty("user.dir"), DistroProperties.DISTRO_FILE_NAME).getAbsolutePath(); | ||
} | ||
|
||
distroProperties.saveTo(new File(distro).getParentFile()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/NPMHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.openmrs.maven.plugins.utility; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class NPMHelper { | ||
public NPMHelper() { | ||
} | ||
|
||
public List<String> getPackageVersions(String packageName, int limit) { | ||
return getPackageVersions(packageName).stream().sorted(Comparator.reverseOrder()).limit(limit).collect(Collectors.toList()); | ||
} | ||
|
||
public List<String> getPackageVersions(String packageName) { | ||
String url = "https://registry.npmjs.org/@openmrs/" + packageName; | ||
List<String> versionSet = new ArrayList<>(); | ||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
HttpGet request = new HttpGet(url); | ||
String jsonResponse = EntityUtils.toString(httpClient.execute(request).getEntity()); | ||
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject(); | ||
JsonObject versions = jsonObject.getAsJsonObject("versions"); | ||
|
||
for (Map.Entry<String, JsonElement> entry : versions.entrySet()) { | ||
versionSet.add(entry.getKey()); | ||
} | ||
return versionSet; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |