Skip to content

Commit b8784a5

Browse files
committed
O3-3433: SDK Command: Add dependencies to distro.properties
1 parent 393b7ba commit b8784a5

File tree

6 files changed

+303
-0
lines changed

6 files changed

+303
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package org.openmrs.maven.plugins;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.openmrs.maven.plugins.model.DistroProperties;
6+
import org.openmrs.maven.plugins.utility.DistroHelper;
7+
8+
import java.io.File;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class AddDependencyTest extends AbstractSdkIntegrationTest {
15+
16+
private String distroFile;
17+
18+
@Before
19+
public void setUp() {
20+
distroFile = testDirectory + File.separator + "add-dependency" + File.separator + "openmrs-distro.properties";
21+
}
22+
23+
@Test
24+
public void shouldAddOmodDependency() throws Exception {
25+
addTaskParam("distro", distroFile);
26+
addTaskParam("type", "OMOD");
27+
addTaskParam("groupId", "org.openmrs.module");
28+
addTaskParam("artifactId", "webservices.rest");
29+
addTaskParam("version", "2.30.0");
30+
31+
executeTask("add-dependency");
32+
assertSuccess();
33+
34+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
35+
assertNotNull(distroProperties);
36+
assertTrue(distroProperties.getAllKeys().contains("omod.webservices.rest"));
37+
assertEquals(distroProperties.getParam("omod.webservices.rest"), "2.30.0");
38+
}
39+
40+
@Test
41+
public void shouldAddSpaDependency() throws Exception {
42+
addTaskParam("distro", distroFile);
43+
addTaskParam("type", "SPA");
44+
addTaskParam("moduleName", "esm-system-admin-app");
45+
addTaskParam("version", "4.0.3");
46+
47+
executeTask("add-dependency");
48+
assertSuccess();
49+
50+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
51+
assertNotNull(distroProperties);
52+
53+
assertTrue(distroProperties.getAllKeys().contains("spa.frontendModules.@openmrs/esm-system-admin-app"));
54+
assertEquals(distroProperties.getParam("spa.frontendModules.@openmrs/esm-system-admin-app"), "4.0.3");
55+
}
56+
57+
@Test
58+
public void shouldAddOwaDependency() throws Exception {
59+
addTaskParam("distro", distroFile);
60+
addTaskParam("type", "OWA");
61+
addTaskParam("artifactId", "sysadmin");
62+
addTaskParam("version", "1.2.0");
63+
64+
executeTask("add-dependency");
65+
assertSuccess();
66+
67+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
68+
assertNotNull(distroProperties);
69+
70+
assertTrue(distroProperties.getAllKeys().contains("owa.sysadmin"));
71+
assertEquals(distroProperties.getParam("owa.sysadmin"), "1.2.0");
72+
}
73+
74+
@Test
75+
public void shouldAddWarDependency() throws Exception {
76+
addTaskParam("distro", distroFile);
77+
addTaskParam("type", "WAR");
78+
addTaskParam("version", "2.6.1");
79+
80+
executeTask("add-dependency");
81+
assertSuccess();
82+
83+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
84+
assertNotNull(distroProperties);
85+
86+
assertTrue(distroProperties.getAllKeys().contains("war.openmrs"));
87+
assertEquals(distroProperties.getParam("war.openmrs"), "2.6.1");
88+
}
89+
90+
@Test
91+
public void shouldOverrideIfPropertyAlreadyExists() throws Exception {
92+
DistroProperties distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
93+
assertNotNull(distroProperties);
94+
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));
95+
96+
addTaskParam("distro", distroFile);
97+
addTaskParam("type", "OMOD");
98+
addTaskParam("groupId", "org.openmrs.module");
99+
addTaskParam("artifactId", "uiframework");
100+
addTaskParam("version", "2.30.0");
101+
102+
executeTask("add-dependency");
103+
assertSuccess();
104+
105+
distroProperties = DistroHelper.getDistroPropertiesFromFile(new File(distroFile));
106+
assertNotNull(distroProperties);
107+
108+
assertTrue(distroProperties.getAllKeys().contains("omod.uiframework"));
109+
assertEquals(distroProperties.getParam("omod.uiframework"), "2.30.0");
110+
}
111+
112+
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Mon Aug 19 16:25:48 IST 2024
2+
name=Ref 3.x distro
3+
omod.uiframework=3.6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.openmrs.maven.plugins;
2+
3+
import org.apache.commons.lang.StringUtils;
4+
import org.apache.maven.plugin.MojoExecutionException;
5+
import org.apache.maven.plugin.MojoFailureException;
6+
import org.apache.maven.plugins.annotations.Mojo;
7+
import org.apache.maven.plugins.annotations.Parameter;
8+
import org.openmrs.maven.plugins.model.Artifact;
9+
import org.openmrs.maven.plugins.model.DistroProperties;
10+
11+
import org.openmrs.maven.plugins.utility.NPMHelper;
12+
import org.openmrs.maven.plugins.utility.SDKConstants;
13+
14+
import java.io.File;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
import java.util.Properties;
19+
20+
@Mojo(name = "add-dependency", requiresProject = false)
21+
public class AddDependency extends AbstractTask {
22+
23+
/**
24+
* Path to the openmrs-distro.properties file to modify
25+
*/
26+
@Parameter(property = "distro")
27+
private String distro;
28+
29+
/**
30+
* Type of the dependency to add
31+
*/
32+
@Parameter(property = "type")
33+
private String type;
34+
35+
/**
36+
* Maven group id of the dependency
37+
*/
38+
@Parameter(property = "groupId")
39+
private String groupId;
40+
41+
/**
42+
* Maven artifact id of the dependency
43+
*/
44+
@Parameter(property = "artifactId")
45+
private String artifactId;
46+
47+
/**
48+
* Version of the dependency
49+
*/
50+
@Parameter(property = "version")
51+
private String version;
52+
53+
/**
54+
* Name of the frontend module
55+
*/
56+
@Parameter(property = "moduleName")
57+
private String moduleName;
58+
59+
60+
private final String DEPENDENCY_TYPE_PROMPT = "Enter the type of dependency you need to add";
61+
62+
63+
@Override
64+
public void executeTask() throws MojoExecutionException, MojoFailureException {
65+
if (distro == null) {
66+
File userDir = new File(System.getProperty("user.dir"));
67+
File distroFile = new File(userDir, DistroProperties.DISTRO_FILE_NAME);
68+
if (distroFile.exists()) {
69+
distro = distroFile.getAbsolutePath();
70+
}
71+
}
72+
73+
if(StringUtils.isBlank(type)) {
74+
List<String> dependencyTypes = new ArrayList<>(Arrays.asList("OMOD", "SPA", "OWA", "WAR"));
75+
type = wizard.promptForMissingValueWithOptions(DEPENDENCY_TYPE_PROMPT, null, null, dependencyTypes);
76+
}
77+
Properties properties = new Properties();
78+
79+
if(StringUtils.isNotBlank(distro)) {
80+
properties = distroHelper.resolveDistroPropertiesForStringSpecifier(distro, versionsHelper).getProperties();
81+
}
82+
83+
switch (type) {
84+
case "OMOD":
85+
groupId = wizard.promptForValueIfMissingWithDefault(null, groupId, "groupId", Artifact.GROUP_MODULE);
86+
artifactId = wizard.promptForValueIfMissing(artifactId, "artifactId");
87+
if (StringUtils.isBlank(version)) {
88+
Artifact artifact = new Artifact(artifactId, "1.0", groupId);
89+
List<String> versions = versionsHelper.getSuggestedVersions(artifact, 5);
90+
version = wizard.promptForMissingValueWithOptions("Enter the version", null, null, versions,
91+
"Please specify the module version", null);
92+
}
93+
properties.setProperty("omod." + artifactId, version);
94+
break;
95+
case "SPA":
96+
moduleName = wizard.promptForValueIfMissing(moduleName, "frontend module name");
97+
if (StringUtils.isBlank(version)) {
98+
List<String> versions = new NPMHelper().getPackageVersions(moduleName, 6);
99+
version = wizard.promptForMissingValueWithOptions("Enter the module version", null, null, versions,
100+
"Please specify the SPA version", null);
101+
}
102+
properties.setProperty("spa.frontendModules.@openmrs/" + moduleName, version);
103+
break;
104+
case "OWA":
105+
artifactId = wizard.promptForValueIfMissing(artifactId, "OWA name");
106+
Artifact artifact = new Artifact(artifactId, "1.0", Artifact.GROUP_OWA);
107+
if(StringUtils.isBlank(version)) {
108+
List<String> suggestedVersions = versionsHelper.getSuggestedVersions(artifact, 6);
109+
version = wizard
110+
.promptForMissingValueWithOptions("Which version would you like to deploy?%s", null, "", suggestedVersions,
111+
"Please specify OWA version", null);
112+
}
113+
properties.setProperty("owa." + artifactId, version);
114+
break;
115+
case "WAR":
116+
Artifact platformArtifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID,
117+
SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO);
118+
if(StringUtils.isBlank(version)) {
119+
version = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(platformArtifact, 5));
120+
}
121+
properties.setProperty("war.openmrs", version);
122+
}
123+
124+
DistroProperties distroProperties = new DistroProperties(properties);
125+
126+
if (StringUtils.isBlank(distro)) {
127+
wizard.showMessage("No distro.properpties file is provided. Generating a new openmrs-distro.properties file.");
128+
distro = new File(System.getProperty("user.dir"), DistroProperties.DISTRO_FILE_NAME).getAbsolutePath();
129+
}
130+
131+
distroProperties.saveTo(new File(distro).getParentFile());
132+
}
133+
134+
}

sdk-commons/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@
8181
<artifactId>mockito-core</artifactId>
8282
<scope>test</scope>
8383
</dependency>
84+
<dependency>
85+
<groupId>com.google.code.gson</groupId>
86+
<artifactId>gson</artifactId>
87+
<version>2.10.1</version>
88+
<scope>compile</scope>
89+
</dependency>
8490
</dependencies>
8591

8692
<build>

sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java

+4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ public Set<Object> getAllKeys() {
263263
return properties.keySet();
264264
}
265265

266+
public Properties getProperties() {
267+
return properties;
268+
}
269+
266270
public List<String> getExclusions() {
267271
String exclusions = getParam("exclusions");
268272
if(exclusions == null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.openmrs.maven.plugins.utility;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import org.apache.http.client.methods.HttpGet;
7+
import org.apache.http.impl.client.CloseableHttpClient;
8+
import org.apache.http.impl.client.HttpClients;
9+
import org.apache.http.util.EntityUtils;
10+
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.Comparator;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.stream.Collectors;
17+
18+
public class NPMHelper {
19+
public NPMHelper() {
20+
}
21+
22+
public List<String> getPackageVersions(String packageName, int limit) {
23+
return getPackageVersions(packageName).stream().sorted(Comparator.reverseOrder()).limit(limit).collect(Collectors.toList());
24+
}
25+
26+
public List<String> getPackageVersions(String packageName) {
27+
String url = "https://registry.npmjs.org/@openmrs/" + packageName;
28+
List<String> versionSet = new ArrayList<>();
29+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
30+
HttpGet request = new HttpGet(url);
31+
String jsonResponse = EntityUtils.toString(httpClient.execute(request).getEntity());
32+
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
33+
JsonObject versions = jsonObject.getAsJsonObject("versions");
34+
35+
for (Map.Entry<String, JsonElement> entry : versions.entrySet()) {
36+
versionSet.add(entry.getKey());
37+
}
38+
return versionSet;
39+
} catch (IOException e) {
40+
throw new RuntimeException(e);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)