Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-331: Update SDK to work with 3.0.0 #272

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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.BaseSdkProperties;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
Expand All @@ -41,6 +42,9 @@
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.openmrs.maven.plugins.utility.ServerHelper;

import static org.openmrs.maven.plugins.model.BaseSdkProperties.PROPERTY_DISTRO_ARTIFACT_ID;
import static org.openmrs.maven.plugins.model.BaseSdkProperties.PROPERTY_DISTRO_GROUP_ID;

/**
* 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.
*/
Expand All @@ -55,11 +59,11 @@ public class Setup extends AbstractServerTask {
"If you want to enable remote debugging by default when running the server, "
+ "\nspecify the %s here (e.g. 1044). Leave blank to disable debugging. \n(Do not do this on a production server)";

private static final String O2_Distribution = "2.x Distribution";
private static final String O2_DISTRIBUTION = "2.x Distribution";

private static final String PLATFORM = "Platform";

private static final String O3_Distribution = "O3 Distribution";
private static final String O3_DISTRIBUTION = "O3 Distribution";

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

Expand Down Expand Up @@ -189,16 +193,17 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
if (distroProperties != null) {
options.add(distroProperties.getName() + " " + distroProperties.getVersion() + " from current directory");
}
options.add(O2_Distribution);

options.add(O3_DISTRIBUTION);
options.add(O2_DISTRIBUTION);
options.add(PLATFORM);
options.add(O3_Distribution);
String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options);

switch (choice) {
case PLATFORM:
platformMode = true;
break;
case O2_Distribution:
case O2_DISTRIBUTION:
wizard.promptForRefAppVersionIfMissing(server, versionsHelper);
if (DistroHelper.isRefapp2_3_1orLower(server.getDistroArtifactId(), server.getVersion())) {
distroProperties = new DistroProperties(server.getVersion());
Expand All @@ -207,30 +212,34 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
}
platformMode = false;
break;
case O3_Distribution:
case O3_DISTRIBUTION:
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper);
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(),
server.getDistroGroupId(), "zip");
Properties frontendProperties;
if (new Version(server.getVersion()).higher(new Version("3.0.0-beta.16"))) {
frontendProperties = distroHelper.getFrontendProperties(server);
}
else {
} else {
frontendProperties = PropertiesUtils.getFrontendPropertiesFromSpaConfigUrl(
"https://raw.githubusercontent.com/openmrs/openmrs-distro-referenceapplication/"+ server.getVersion() +"/frontend/spa-build-config.json");
}

Properties configurationProperties = PropertiesUtils.getConfigurationProperty(artifact);
File file = distroHelper.downloadDistro(server.getServerDirectory(), artifact);
Properties backendProperties = PropertiesUtils.getDistroProperties(file);
Properties spaModuleProperty = PropertiesUtils.getModuleProperty("https://raw.githubusercontent.com/openmrs/openmrs-module-spa/master/pom.xml");

if(appShellVersion != null) {
frontendProperties.setProperty("spa.core", appShellVersion);
}

Properties allProperties = new Properties();
allProperties.putAll(backendProperties);
allProperties.putAll(spaModuleProperty);
allProperties.putAll(frontendProperties);
allProperties.putAll(configurationProperties);
allProperties.put(PROPERTY_DISTRO_GROUP_ID, artifact.getGroupId());
allProperties.put(PROPERTY_DISTRO_ARTIFACT_ID, artifact.getArtifactId());
distroProperties = new DistroProperties(allProperties);
platformMode = false;
break;
Expand Down Expand Up @@ -368,26 +377,31 @@ private void setConfigFolder(Server server, DistroProperties distroProperties) t
if(distroProperties.getConfigArtifacts().isEmpty()) {
return;
}

File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configDir.mkdir();

downloadConfigs(distroProperties, configDir);
File referenceApplicationFile = new File(configDir, "referenceapplication-distro.owa");
if (!referenceApplicationFile.exists()) {
return;
}
try {
ZipFile zipFile = new ZipFile(referenceApplicationFile);
zipFile.extractAll(configDir.getPath());
for (File file : Objects.requireNonNull(configDir.listFiles())) {
if (file.getName().equals("openmrs_config")) {
FileUtils.copyDirectory(file, configDir);

// Handle O2 configuration
if (Artifact.GROUP_DISTRO.equals(server.getDistroGroupId()) && "referenceapplication-distro".equals(server.getDistroArtifactId())) {
File referenceApplicationFile = new File(configDir, "referenceapplication-distro.owa");
if (!referenceApplicationFile.exists()) {
return;
}
try {
ZipFile zipFile = new ZipFile(referenceApplicationFile);
zipFile.extractAll(configDir.getPath());
for (File file : Objects.requireNonNull(configDir.listFiles())) {
if (file.getName().equals("openmrs_config")) {
FileUtils.copyDirectory(file, configDir);
}
FileUtils.deleteQuietly(file);
}
FileUtils.deleteQuietly(file);
FileUtils.deleteQuietly(referenceApplicationFile);
} catch (ZipException | IOException e) {
throw new RuntimeException(e);
}
FileUtils.deleteQuietly(referenceApplicationFile);
}
catch (ZipException | IOException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -402,10 +416,7 @@ private void downloadConfigs(DistroProperties distroProperties, File configDir)
List<Artifact> configs = distroProperties.getConfigArtifacts();
wizard.showMessage("Downloading Configs...\n");
if (!configs.isEmpty()) {
for (Artifact config : configs) {
wizard.showMessage("Downloading Config: " + config);
owaHelper.downloadOwa(configDir, config, moduleInstaller);
}
moduleInstaller.installModules(configs, configDir.getAbsolutePath());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public void runNpx(String arguments) throws MojoExecutionException {
runNpx(arguments, "");
}

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

// 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
// hack added in December 2021; it's use probably should be re-evaluated at some point
// additional hack: we do not use --cache on macs due to https://github.com/npm/cli/issues/3256
if (mavenProject != null && mavenProject.getBuild() != null && !SystemUtils.IS_OS_MAC_OSX) {
npmExec =
legacyPeerDeps + " --cache=" + tempDir.resolve("npm-cache").toAbsolutePath() + " exec -- " + arguments;
npmArguments + " --cache=" + tempDir.resolve("npm-cache").toAbsolutePath() + " exec -- " + arguments;
}

List<MojoExecutor.Element> configuration = new ArrayList<>(3);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
<dependency>
<groupId>com.github.zafarkhaja</groupId>
<artifactId>java-semver</artifactId>
<version>0.9.0</version>
<version>0.10.2</version>
</dependency>

<!-- console input and output -->
Expand Down
5 changes: 5 additions & 0 deletions sdk-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>com.github.zafarkhaja</groupId>
<artifactId>java-semver</artifactId>
</dependency>

<!-- TODO: Get rid of this... just using for URLBuilder -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
public abstract class BaseSdkProperties {

public static final String PROPERTY_DISTRO_ARTIFACT_ID = "distro.artifactId";
public static final String PROPERTY_DISTRO_GROUP_ID = "distro.groupId";
protected static final String ARTIFACT_ID = "artifactId";
protected static final String TYPE = "type";
protected static final String GROUP_ID = "groupId";
Expand Down Expand Up @@ -48,7 +50,6 @@ public Properties getModuleAndWarProperties(List<Artifact> warArtifacts, List<Ar
}

for (Artifact artifact : moduleArtifacts) {

stripArtifactId(artifact);

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

properties.setProperty(TYPE_OMOD + "." + artifact.getArtifactId(), artifact.getVersion());

}
return properties;
}
Expand Down Expand Up @@ -174,6 +174,7 @@ protected String checkIfOverwritten(String key, String param) {
setting = setting.concat("-");
setting = setting.concat("package");
}

return setting;
} else {
switch (param) {
Expand All @@ -187,7 +188,7 @@ protected String checkIfOverwritten(String key, String param) {
return Artifact.GROUP_MODULE;
case TYPE_DISTRO:
case TYPE_CONFIG:
return Artifact.GROUP_DISTRO;
return properties.getProperty(PROPERTY_DISTRO_GROUP_ID, Artifact.GROUP_DISTRO);
default:
return "";
}
Expand All @@ -210,7 +211,7 @@ protected String checkIfOverwritten(String key, String param) {
}
}

private String extractArtifactId(String key){
protected String extractArtifactId(String key){
String type = getArtifactType(key);
StringBuilder stringBuilder = new StringBuilder(key.substring(key.indexOf(".") + 1));
if(type.equals(TYPE_OMOD)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public class Server extends BaseSdkProperties {
public static final String PROPERTY_USER_MODULES = "user_modules";
public static final String PROPERTY_USER_OWAS = "user_owas";
public static final String PROPERTY_DEMO_DATA = "add_demo_data";
public static final String PROPERTY_DISTRO_ARTIFACT_ID = "distro.artifactId";
public static final String PROPERTY_DISTRO_GROUP_ID = "distro.groupId";
private static final String OLD_PROPERTIES_FILENAME = "backup.properties";
public static final String OWA_DIRECTORY = "owa";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,14 @@ public class DefaultWizard implements Wizard {

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

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

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

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

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

private static final String O3_ARTIFACT_TMPL = "org.openmrs.distro:referenceapplication-distro:%s";

private static final String JDK_ERROR_TMPL = "\nThe JDK %s is not compatible with OpenMRS Platform %s. " +
"Please use %s to run this server.\n\nIf you are running " +
"in a forked mode, correct the java.home property in %s\n";
Expand Down Expand Up @@ -727,8 +725,7 @@ public String promptForRefAppVersion(VersionsHelper versionsHelper, String custo

public String promptForO3RefAppVersion(VersionsHelper versionsHelper, String customMessage)
throws MojoExecutionException {
Map<String, String> optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL,
O3_ARTIFACT_TMPL);
Map<String, String> optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL);
return promptForO3Version(optionsMap, customMessage);
}

Expand Down Expand Up @@ -782,16 +779,31 @@ private Map<String, String> getDistroVersionsOptionsMap(Set<String> versions, Ve
*
* @param versionsHelper The VersionsHelper object to retrieve the artifact versions from.
* @param optionTemplate The template for generating option keys in the map.
* @param artifactTemplate The template for generating artifact values in the map.
* @return A LinkedHashMap containing the generated options map.
*/
private Map<String, String> getO3VersionsOptionsMap(VersionsHelper versionsHelper,
String optionTemplate, String artifactTemplate) {
String optionTemplate) {
Map<String, String> optionsMap = new LinkedHashMap<>();
Artifact artifact = new Artifact("referenceapplication-distro", "3.0.0-SNAPSHOT", "org.openmrs.distro", "zip");
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
optionsMap.put(String.format(optionTemplate, version.toString()), String.format(artifactTemplate, version));

{
Artifact artifact = new Artifact("distro-emr-configuration", "3.0.0-SNAPSHOT", "org.openmrs", "zip");
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
optionsMap.put(String.format(optionTemplate, version.toString()), artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version);
}
}

if (optionsMap.size() == MAX_OPTIONS_SIZE) {
return optionsMap;
}

{
Artifact artifact = new Artifact("referenceapplication-distro", "3.0.0-SNAPSHOT", "org.openmrs.distro", "zip");
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
optionsMap.put(String.format(optionTemplate, version.toString()), artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + version);

}
}

return optionsMap;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.openmrs.maven.plugins.utility;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.openmrs.maven.plugins.model.*;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.UpgradeDifferential;
import org.openmrs.maven.plugins.model.Version;
import org.twdata.maven.mojoexecutor.MojoExecutor;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
Expand Down Expand Up @@ -212,6 +214,7 @@ public File downloadDistro(File path, Artifact artifact, String fileName) throws
),
executionEnvironment(mavenProject, mavenSession, pluginManager)
);

return new File(path, artifact.getDestFileName());
}

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

public Properties getFrontendProperties(Server server) throws MojoExecutionException {
Artifact artifact = new Artifact("referenceapplication-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
com.github.zafarkhaja.semver.Version v = com.github.zafarkhaja.semver.Version.parse(server.getVersion());

Artifact artifact;
if (v.satisfies(">=3.0.0")) {
artifact = new Artifact("distro-emr-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
} else {
artifact = new Artifact("referenceapplication-frontend", server.getVersion(), server.getDistroGroupId(), "zip");
}

File frontendDistroFile = downloadDistro(server.getServerDirectory(), artifact, "frontend.zip");
Properties frontendProperties = new Properties();

try (ZipFile zipFile = new ZipFile(frontendDistroFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
Expand All @@ -461,14 +473,17 @@ public Properties getFrontendProperties(Server server) throws MojoExecutionExcep
try (InputStream inputStream = zipFile.getInputStream(zipEntry)){
frontendProperties = PropertiesUtils.getFrontendPropertiesFromJson(inputStream);
}
break;
}
}
}
catch (IOException e) {
throw new MojoExecutionException("Could not read \"" + frontendDistroFile.getAbsolutePath() + "\" " + e.getMessage(), e);
}
finally {
frontendDistroFile.delete();
if (frontendDistroFile != null && frontendDistroFile.exists()) {
frontendDistroFile.delete();
}
}
return frontendProperties;
}
Expand Down
Loading
Loading