Skip to content

Commit 25ea663

Browse files
authored
SDK-331: Update SDK to work with 3.0.0 (#272)
1 parent b9c7da5 commit 25ea663

File tree

10 files changed

+103
-62
lines changed

10 files changed

+103
-62
lines changed

maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java

+38-27
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.maven.plugins.annotations.Mojo;
3333
import org.apache.maven.plugins.annotations.Parameter;
3434
import org.openmrs.maven.plugins.model.Artifact;
35+
import org.openmrs.maven.plugins.model.BaseSdkProperties;
3536
import org.openmrs.maven.plugins.model.DistroProperties;
3637
import org.openmrs.maven.plugins.model.Server;
3738
import org.openmrs.maven.plugins.model.Version;
@@ -41,6 +42,9 @@
4142
import org.openmrs.maven.plugins.utility.SDKConstants;
4243
import org.openmrs.maven.plugins.utility.ServerHelper;
4344

45+
import static org.openmrs.maven.plugins.model.BaseSdkProperties.PROPERTY_DISTRO_ARTIFACT_ID;
46+
import static org.openmrs.maven.plugins.model.BaseSdkProperties.PROPERTY_DISTRO_GROUP_ID;
47+
4448
/**
4549
* Set up a new instance of OpenMRS server. It can be used for setting up a platform or a distribution. It prompts for any missing, but required parameters.
4650
*/
@@ -55,11 +59,11 @@ public class Setup extends AbstractServerTask {
5559
"If you want to enable remote debugging by default when running the server, "
5660
+ "\nspecify the %s here (e.g. 1044). Leave blank to disable debugging. \n(Do not do this on a production server)";
5761

58-
private static final String O2_Distribution = "2.x Distribution";
62+
private static final String O2_DISTRIBUTION = "2.x Distribution";
5963

6064
private static final String PLATFORM = "Platform";
6165

62-
private static final String O3_Distribution = "O3 Distribution";
66+
private static final String O3_DISTRIBUTION = "O3 Distribution";
6367

6468
private static final String CLASSPATH_SCRIPT_PREFIX = "classpath://";
6569

@@ -189,16 +193,17 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
189193
if (distroProperties != null) {
190194
options.add(distroProperties.getName() + " " + distroProperties.getVersion() + " from current directory");
191195
}
192-
options.add(O2_Distribution);
196+
197+
options.add(O3_DISTRIBUTION);
198+
options.add(O2_DISTRIBUTION);
193199
options.add(PLATFORM);
194-
options.add(O3_Distribution);
195200
String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options);
196201

197202
switch (choice) {
198203
case PLATFORM:
199204
platformMode = true;
200205
break;
201-
case O2_Distribution:
206+
case O2_DISTRIBUTION:
202207
wizard.promptForRefAppVersionIfMissing(server, versionsHelper);
203208
if (DistroHelper.isRefapp2_3_1orLower(server.getDistroArtifactId(), server.getVersion())) {
204209
distroProperties = new DistroProperties(server.getVersion());
@@ -207,30 +212,34 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
207212
}
208213
platformMode = false;
209214
break;
210-
case O3_Distribution:
215+
case O3_DISTRIBUTION:
211216
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper);
212217
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(),
213218
server.getDistroGroupId(), "zip");
214219
Properties frontendProperties;
215220
if (new Version(server.getVersion()).higher(new Version("3.0.0-beta.16"))) {
216221
frontendProperties = distroHelper.getFrontendProperties(server);
217-
}
218-
else {
222+
} else {
219223
frontendProperties = PropertiesUtils.getFrontendPropertiesFromSpaConfigUrl(
220224
"https://raw.githubusercontent.com/openmrs/openmrs-distro-referenceapplication/"+ server.getVersion() +"/frontend/spa-build-config.json");
221225
}
226+
222227
Properties configurationProperties = PropertiesUtils.getConfigurationProperty(artifact);
223228
File file = distroHelper.downloadDistro(server.getServerDirectory(), artifact);
224229
Properties backendProperties = PropertiesUtils.getDistroProperties(file);
225230
Properties spaModuleProperty = PropertiesUtils.getModuleProperty("https://raw.githubusercontent.com/openmrs/openmrs-module-spa/master/pom.xml");
231+
226232
if(appShellVersion != null) {
227233
frontendProperties.setProperty("spa.core", appShellVersion);
228234
}
235+
229236
Properties allProperties = new Properties();
230237
allProperties.putAll(backendProperties);
231238
allProperties.putAll(spaModuleProperty);
232239
allProperties.putAll(frontendProperties);
233240
allProperties.putAll(configurationProperties);
241+
allProperties.put(PROPERTY_DISTRO_GROUP_ID, artifact.getGroupId());
242+
allProperties.put(PROPERTY_DISTRO_ARTIFACT_ID, artifact.getArtifactId());
234243
distroProperties = new DistroProperties(allProperties);
235244
platformMode = false;
236245
break;
@@ -368,26 +377,31 @@ private void setConfigFolder(Server server, DistroProperties distroProperties) t
368377
if(distroProperties.getConfigArtifacts().isEmpty()) {
369378
return;
370379
}
380+
371381
File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
372382
configDir.mkdir();
383+
373384
downloadConfigs(distroProperties, configDir);
374-
File referenceApplicationFile = new File(configDir, "referenceapplication-distro.owa");
375-
if (!referenceApplicationFile.exists()) {
376-
return;
377-
}
378-
try {
379-
ZipFile zipFile = new ZipFile(referenceApplicationFile);
380-
zipFile.extractAll(configDir.getPath());
381-
for (File file : Objects.requireNonNull(configDir.listFiles())) {
382-
if (file.getName().equals("openmrs_config")) {
383-
FileUtils.copyDirectory(file, configDir);
385+
386+
// Handle O2 configuration
387+
if (Artifact.GROUP_DISTRO.equals(server.getDistroGroupId()) && "referenceapplication-distro".equals(server.getDistroArtifactId())) {
388+
File referenceApplicationFile = new File(configDir, "referenceapplication-distro.owa");
389+
if (!referenceApplicationFile.exists()) {
390+
return;
391+
}
392+
try {
393+
ZipFile zipFile = new ZipFile(referenceApplicationFile);
394+
zipFile.extractAll(configDir.getPath());
395+
for (File file : Objects.requireNonNull(configDir.listFiles())) {
396+
if (file.getName().equals("openmrs_config")) {
397+
FileUtils.copyDirectory(file, configDir);
398+
}
399+
FileUtils.deleteQuietly(file);
384400
}
385-
FileUtils.deleteQuietly(file);
401+
FileUtils.deleteQuietly(referenceApplicationFile);
402+
} catch (ZipException | IOException e) {
403+
throw new RuntimeException(e);
386404
}
387-
FileUtils.deleteQuietly(referenceApplicationFile);
388-
}
389-
catch (ZipException | IOException e) {
390-
throw new RuntimeException(e);
391405
}
392406
}
393407

@@ -402,10 +416,7 @@ private void downloadConfigs(DistroProperties distroProperties, File configDir)
402416
List<Artifact> configs = distroProperties.getConfigArtifacts();
403417
wizard.showMessage("Downloading Configs...\n");
404418
if (!configs.isEmpty()) {
405-
for (Artifact config : configs) {
406-
wizard.showMessage("Downloading Config: " + config);
407-
owaHelper.downloadOwa(configDir, config, moduleInstaller);
408-
}
419+
moduleInstaller.installModules(configs, configDir.getAbsolutePath());
409420
}
410421
}
411422

maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/NodeHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ public void runNpx(String arguments) throws MojoExecutionException {
8484
runNpx(arguments, "");
8585
}
8686

87-
public void runNpx(String arguments, String legacyPeerDeps) throws MojoExecutionException {
88-
String npmExec = legacyPeerDeps + " exec -- " + arguments;
87+
public void runNpx(String arguments, String npmArguments) throws MojoExecutionException {
88+
String npmExec = npmArguments + " exec -- " + arguments;
8989

9090
// it's a little weird to use a custom NPM cache for this; however, it seems to be necessary to get things working on Bamboo
9191
// hack added in December 2021; it's use probably should be re-evaluated at some point
9292
// additional hack: we do not use --cache on macs due to https://github.com/npm/cli/issues/3256
9393
if (mavenProject != null && mavenProject.getBuild() != null && !SystemUtils.IS_OS_MAC_OSX) {
9494
npmExec =
95-
legacyPeerDeps + " --cache=" + tempDir.resolve("npm-cache").toAbsolutePath() + " exec -- " + arguments;
95+
npmArguments + " --cache=" + tempDir.resolve("npm-cache").toAbsolutePath() + " exec -- " + arguments;
9696
}
9797

9898
List<MojoExecutor.Element> configuration = new ArrayList<>(3);

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@
399399
<dependency>
400400
<groupId>com.github.zafarkhaja</groupId>
401401
<artifactId>java-semver</artifactId>
402-
<version>0.9.0</version>
402+
<version>0.10.2</version>
403403
</dependency>
404404

405405
<!-- console input and output -->

sdk-commons/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<artifactId>commons-io</artifactId>
3030
</dependency>
3131

32+
<dependency>
33+
<groupId>com.github.zafarkhaja</groupId>
34+
<artifactId>java-semver</artifactId>
35+
</dependency>
36+
3237
<!-- TODO: Get rid of this... just using for URLBuilder -->
3338
<dependency>
3439
<groupId>org.apache.httpcomponents</groupId>

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
public abstract class BaseSdkProperties {
1616

17+
public static final String PROPERTY_DISTRO_ARTIFACT_ID = "distro.artifactId";
18+
public static final String PROPERTY_DISTRO_GROUP_ID = "distro.groupId";
1719
protected static final String ARTIFACT_ID = "artifactId";
1820
protected static final String TYPE = "type";
1921
protected static final String GROUP_ID = "groupId";
@@ -48,7 +50,6 @@ public Properties getModuleAndWarProperties(List<Artifact> warArtifacts, List<Ar
4850
}
4951

5052
for (Artifact artifact : moduleArtifacts) {
51-
5253
stripArtifactId(artifact);
5354

5455
if (!artifact.getType().equals(TYPE_JAR)) {
@@ -59,7 +60,6 @@ public Properties getModuleAndWarProperties(List<Artifact> warArtifacts, List<Ar
5960
}
6061

6162
properties.setProperty(TYPE_OMOD + "." + artifact.getArtifactId(), artifact.getVersion());
62-
6363
}
6464
return properties;
6565
}
@@ -174,6 +174,7 @@ protected String checkIfOverwritten(String key, String param) {
174174
setting = setting.concat("-");
175175
setting = setting.concat("package");
176176
}
177+
177178
return setting;
178179
} else {
179180
switch (param) {
@@ -187,7 +188,7 @@ protected String checkIfOverwritten(String key, String param) {
187188
return Artifact.GROUP_MODULE;
188189
case TYPE_DISTRO:
189190
case TYPE_CONFIG:
190-
return Artifact.GROUP_DISTRO;
191+
return properties.getProperty(PROPERTY_DISTRO_GROUP_ID, Artifact.GROUP_DISTRO);
191192
default:
192193
return "";
193194
}
@@ -210,7 +211,7 @@ protected String checkIfOverwritten(String key, String param) {
210211
}
211212
}
212213

213-
private String extractArtifactId(String key){
214+
protected String extractArtifactId(String key){
214215
String type = getArtifactType(key);
215216
StringBuilder stringBuilder = new StringBuilder(key.substring(key.indexOf(".") + 1));
216217
if(type.equals(TYPE_OMOD)) {

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

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public class Server extends BaseSdkProperties {
5151
public static final String PROPERTY_USER_MODULES = "user_modules";
5252
public static final String PROPERTY_USER_OWAS = "user_owas";
5353
public static final String PROPERTY_DEMO_DATA = "add_demo_data";
54-
public static final String PROPERTY_DISTRO_ARTIFACT_ID = "distro.artifactId";
55-
public static final String PROPERTY_DISTRO_GROUP_ID = "distro.groupId";
5654
private static final String OLD_PROPERTIES_FILENAME = "backup.properties";
5755
public static final String OWA_DIRECTORY = "owa";
5856

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,14 @@ public class DefaultWizard implements Wizard {
8484

8585
private static final String REFERENCEAPPLICATION_2_4 = "org.openmrs.distro:referenceapplication-package:2.4";
8686

87-
private static final String REFERENCEAPPLICATION_O3 = "org.openmrs.distro:referenceapplication-distro:3.0.0";
87+
private static final String REFERENCEAPPLICATION_O3 = "org.openmrs:emr-distro-configuration:3.0.0";
8888

8989
private static final String SDK_PROPERTIES_FILE = "SDK Properties file";
9090

9191
private static final String REFAPP_OPTION_TMPL = "Reference Application %s";
9292

9393
private static final String REFAPP_ARTIFACT_TMPL = "org.openmrs.distro:referenceapplication-package:%s";
9494

95-
private static final String O3_ARTIFACT_TMPL = "org.openmrs.distro:referenceapplication-distro:%s";
96-
9795
private static final String JDK_ERROR_TMPL = "\nThe JDK %s is not compatible with OpenMRS Platform %s. " +
9896
"Please use %s to run this server.\n\nIf you are running " +
9997
"in a forked mode, correct the java.home property in %s\n";
@@ -727,8 +725,7 @@ public String promptForRefAppVersion(VersionsHelper versionsHelper, String custo
727725

728726
public String promptForO3RefAppVersion(VersionsHelper versionsHelper, String customMessage)
729727
throws MojoExecutionException {
730-
Map<String, String> optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL,
731-
O3_ARTIFACT_TMPL);
728+
Map<String, String> optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL);
732729
return promptForO3Version(optionsMap, customMessage);
733730
}
734731

@@ -782,16 +779,31 @@ private Map<String, String> getDistroVersionsOptionsMap(Set<String> versions, Ve
782779
*
783780
* @param versionsHelper The VersionsHelper object to retrieve the artifact versions from.
784781
* @param optionTemplate The template for generating option keys in the map.
785-
* @param artifactTemplate The template for generating artifact values in the map.
786782
* @return A LinkedHashMap containing the generated options map.
787783
*/
788784
private Map<String, String> getO3VersionsOptionsMap(VersionsHelper versionsHelper,
789-
String optionTemplate, String artifactTemplate) {
785+
String optionTemplate) {
790786
Map<String, String> optionsMap = new LinkedHashMap<>();
791-
Artifact artifact = new Artifact("referenceapplication-distro", "3.0.0-SNAPSHOT", "org.openmrs.distro", "zip");
792-
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
793-
optionsMap.put(String.format(optionTemplate, version.toString()), String.format(artifactTemplate, version));
787+
788+
{
789+
Artifact artifact = new Artifact("distro-emr-configuration", "3.0.0-SNAPSHOT", "org.openmrs", "zip");
790+
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
791+
optionsMap.put(String.format(optionTemplate, version.toString()), artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version);
792+
}
793+
}
794+
795+
if (optionsMap.size() == MAX_OPTIONS_SIZE) {
796+
return optionsMap;
794797
}
798+
799+
{
800+
Artifact artifact = new Artifact("referenceapplication-distro", "3.0.0-SNAPSHOT", "org.openmrs.distro", "zip");
801+
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
802+
optionsMap.put(String.format(optionTemplate, version.toString()), artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version);
803+
804+
}
805+
}
806+
795807
return optionsMap;
796808
}
797809

sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package org.openmrs.maven.plugins.utility;
22

33
import org.apache.commons.io.FileUtils;
4-
import org.apache.commons.io.IOUtils;
54
import org.apache.commons.lang.StringUtils;
65
import org.apache.maven.execution.MavenSession;
76
import org.apache.maven.plugin.BuildPluginManager;
87
import org.apache.maven.plugin.MojoExecutionException;
98
import org.apache.maven.project.MavenProject;
10-
import org.openmrs.maven.plugins.model.*;
9+
import org.openmrs.maven.plugins.model.Artifact;
10+
import org.openmrs.maven.plugins.model.DistroProperties;
11+
import org.openmrs.maven.plugins.model.Server;
12+
import org.openmrs.maven.plugins.model.UpgradeDifferential;
13+
import org.openmrs.maven.plugins.model.Version;
1114
import org.twdata.maven.mojoexecutor.MojoExecutor;
1215

1316
import java.io.File;
1417
import java.io.IOException;
1518
import java.io.InputStream;
16-
import java.nio.charset.StandardCharsets;
1719
import java.util.*;
1820
import java.util.zip.ZipEntry;
1921
import java.util.zip.ZipFile;
@@ -212,6 +214,7 @@ public File downloadDistro(File path, Artifact artifact, String fileName) throws
212214
),
213215
executionEnvironment(mavenProject, mavenSession, pluginManager)
214216
);
217+
215218
return new File(path, artifact.getDestFileName());
216219
}
217220

@@ -332,7 +335,7 @@ public void saveDistroPropertiesTo(File destination, String distro) throws MojoE
332335
* - add modules which are not installed on server yet
333336
*/
334337
public static UpgradeDifferential calculateUpdateDifferential(DistroHelper distroHelper, Server server,
335-
DistroProperties distroProperties) throws MojoExecutionException {
338+
DistroProperties distroProperties) throws MojoExecutionException {
336339
List<Artifact> newList = new ArrayList<>(
337340
distroProperties.getWarArtifacts(distroHelper, server.getServerDirectory()));
338341
newList.addAll(distroProperties.getModuleArtifacts(distroHelper, server.getServerDirectory()));
@@ -450,9 +453,18 @@ private static boolean artifactsToCompareAreInvalid(Artifact previous, Artifact
450453
}
451454

452455
public Properties getFrontendProperties(Server server) throws MojoExecutionException {
453-
Artifact artifact = new Artifact("referenceapplication-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
456+
com.github.zafarkhaja.semver.Version v = com.github.zafarkhaja.semver.Version.parse(server.getVersion());
457+
458+
Artifact artifact;
459+
if (v.satisfies(">=3.0.0")) {
460+
artifact = new Artifact("distro-emr-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
461+
} else {
462+
artifact = new Artifact("referenceapplication-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
463+
}
464+
454465
File frontendDistroFile = downloadDistro(server.getServerDirectory(), artifact, "frontend.zip");
455466
Properties frontendProperties = new Properties();
467+
456468
try (ZipFile zipFile = new ZipFile(frontendDistroFile)) {
457469
Enumeration<? extends ZipEntry> entries = zipFile.entries();
458470
while (entries.hasMoreElements()) {
@@ -461,14 +473,17 @@ public Properties getFrontendProperties(Server server) throws MojoExecutionExcep
461473
try (InputStream inputStream = zipFile.getInputStream(zipEntry)){
462474
frontendProperties = PropertiesUtils.getFrontendPropertiesFromJson(inputStream);
463475
}
476+
break;
464477
}
465478
}
466479
}
467480
catch (IOException e) {
468481
throw new MojoExecutionException("Could not read \"" + frontendDistroFile.getAbsolutePath() + "\" " + e.getMessage(), e);
469482
}
470483
finally {
471-
frontendDistroFile.delete();
484+
if (frontendDistroFile != null && frontendDistroFile.exists()) {
485+
frontendDistroFile.delete();
486+
}
472487
}
473488
return frontendProperties;
474489
}

0 commit comments

Comments
 (0)