diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index e000b1665..738534f77 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -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; @@ -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. */ @@ -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://"; @@ -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()); @@ -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; @@ -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); } } @@ -402,10 +416,7 @@ private void downloadConfigs(DistroProperties distroProperties, File configDir) List 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()); } } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/NodeHelper.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/NodeHelper.java index e1ac70413..53500b04f 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/NodeHelper.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/NodeHelper.java @@ -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 configuration = new ArrayList<>(3); diff --git a/pom.xml b/pom.xml index 52df4fd4c..d533e8fd6 100644 --- a/pom.xml +++ b/pom.xml @@ -399,7 +399,7 @@ com.github.zafarkhaja java-semver - 0.9.0 + 0.10.2 diff --git a/sdk-commons/pom.xml b/sdk-commons/pom.xml index 8171b8332..5be24a8d3 100644 --- a/sdk-commons/pom.xml +++ b/sdk-commons/pom.xml @@ -29,6 +29,11 @@ commons-io + + com.github.zafarkhaja + java-semver + + org.apache.httpcomponents diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java index 1600dc7ae..a843e1337 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java @@ -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"; @@ -48,7 +50,6 @@ public Properties getModuleAndWarProperties(List warArtifacts, List warArtifacts, List optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL, - O3_ARTIFACT_TMPL); + Map optionsMap = getO3VersionsOptionsMap(versionsHelper, REFAPP_OPTION_TMPL); return promptForO3Version(optionsMap, customMessage); } @@ -782,16 +779,31 @@ private Map getDistroVersionsOptionsMap(Set 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 getO3VersionsOptionsMap(VersionsHelper versionsHelper, - String optionTemplate, String artifactTemplate) { + String optionTemplate) { Map 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; } diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java index 81e59378a..4a2c539aa 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DistroHelper.java @@ -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; @@ -212,6 +214,7 @@ public File downloadDistro(File path, Artifact artifact, String fileName) throws ), executionEnvironment(mavenProject, mavenSession, pluginManager) ); + return new File(path, artifact.getDestFileName()); } @@ -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 newList = new ArrayList<>( distroProperties.getWarArtifacts(distroHelper, server.getServerDirectory())); newList.addAll(distroProperties.getModuleArtifacts(distroHelper, server.getServerDirectory())); @@ -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 entries = zipFile.entries(); while (entries.hasMoreElements()) { @@ -461,6 +473,7 @@ public Properties getFrontendProperties(Server server) throws MojoExecutionExcep try (InputStream inputStream = zipFile.getInputStream(zipEntry)){ frontendProperties = PropertiesUtils.getFrontendPropertiesFromJson(inputStream); } + break; } } } @@ -468,7 +481,9 @@ public Properties getFrontendProperties(Server server) throws MojoExecutionExcep throw new MojoExecutionException("Could not read \"" + frontendDistroFile.getAbsolutePath() + "\" " + e.getMessage(), e); } finally { - frontendDistroFile.delete(); + if (frontendDistroFile != null && frontendDistroFile.exists()) { + frontendDistroFile.delete(); + } } return frontendProperties; } diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/PropertiesUtils.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/PropertiesUtils.java index a9106d65a..9bf8955e7 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/PropertiesUtils.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/PropertiesUtils.java @@ -24,6 +24,7 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.maven.plugin.MojoExecutionException; import org.openmrs.maven.plugins.model.Artifact; @@ -147,12 +148,16 @@ public static void loadPropertiesFromInputStream(InputStream in, Properties prop * @param url The URL to retrieve the frontend properties from. * @return A Properties object containing the retrieved frontend properties. */ - public static Properties getFrontendPropertiesFromSpaConfigUrl(String url) { + public static Properties getFrontendPropertiesFromSpaConfigUrl(String url) throws MojoExecutionException { Properties properties = new Properties(); - try { - HttpClient httpClient = HttpClientBuilder.create().build(); + try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { HttpGet request = new HttpGet(url); HttpResponse response = httpClient.execute(request); + + if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 299) { + throw new MojoExecutionException("Could not load frontend properties from: " + url); + } + HttpEntity entity = response.getEntity(); try (InputStream inputStream = entity.getContent()) { properties = getFrontendPropertiesFromJson(inputStream); diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java index f7d7cbdb5..83b7798bc 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/SDKConstants.java @@ -117,12 +117,6 @@ public static Artifact getReferenceModule(String version) { return artifact; } - public static Artifact getO3Distro(String version) { - Artifact artifact = new Artifact("referenceapplication-distro", version, Artifact.GROUP_DISTRO, Artifact.TYPE_ZIP); - artifact.setClassifier("distro"); - return artifact; - } - public static Artifact getDistroModule(String groupId, String artifactId, String version) { return new Artifact(artifactId, version, groupId); }