Skip to content

Commit

Permalink
O3-3640: SDK to use any content packages specified in distribution pr…
Browse files Browse the repository at this point in the history
…operties (#288)
  • Loading branch information
nravilla authored Sep 24, 2024
1 parent b09b2ca commit 954cce5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.slf4j.Logger;
Expand Down Expand Up @@ -294,7 +295,6 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
File owasDir = new File(tempDir, "bundledOwas");
owasDir.mkdir();
downloadOWAs(targetDirectory, distroProperties, owasDir);

spaInstaller.installFromDistroProperties(tempDir, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
File frontendDir = new File(tempDir, "frontend");
if(frontendDir.exists()) {
Expand Down Expand Up @@ -325,8 +325,9 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
configDir.mkdir();
setConfigFolder(configDir, distroProperties, distroArtifact);

spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
ContentHelper.downloadAndMoveContentBackendConfig(web, distroProperties, moduleInstaller, wizard);

spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);

File owasDir = new File(web, "owa");
owasDir.mkdir();
Expand Down Expand Up @@ -375,7 +376,6 @@ private void setConfigFolder(File configDir, DistroProperties distroProperties,
return;
}


downloadConfigs(distroProperties, configDir);

File refappConfigFile = new File(configDir, distroArtifact.getArtifactId() + "-" + distroArtifact.getVersion() + ".zip");
Expand Down Expand Up @@ -514,7 +514,7 @@ private void writeTemplatedFile(File targetDirectory, String version, String pat
}

private String adjustImageName(String part) {
return part.replaceAll("\\s+", "").toLowerCase();
return part != null ? part.replaceAll("\\s+", "").toLowerCase() : "";
}

private void copyDbDump(File targetDirectory, InputStream stream) throws MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DBConnector;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
Expand Down Expand Up @@ -278,11 +279,16 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
setServerVersionsFromDistroProperties(server, distroProperties);
distroHelper.parseContentProperties(distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper);
setConfigFolder(server, distroProperties);

ContentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties, moduleInstaller, wizard);

if (spaInstaller != null) {
spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
}

installOWAs(server, distroProperties);

setConfigFolder(server, distroProperties);
} else {
moduleInstaller.installDefaultModules(server);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.openmrs.maven.plugins.utility;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;

/**
* This class is downloads and moves content backend config to respective configuration folders
*/
public class ContentHelper {

public static final String FRONTEND_CONFIG_FOLDER = Paths.get("configs", "frontend_config").toString();
public static final String BACKEND_CONFIG_FOLDER = Paths.get("configs", "backend_config").toString();

public static void downloadContent(Artifact contentArtifact, ModuleInstaller moduleInstaller, File targetDir) throws MojoExecutionException {
String artifactId = contentArtifact.getArtifactId();
// create a temporary artifact folder
File sourceDir;
try {
sourceDir = Files.createTempDirectory("openmrs-sdk-" + artifactId + "-").toFile();
} catch (IOException e) {
throw new MojoExecutionException("Exception while trying to create temporary directory", e);
}

moduleInstaller.installAndUnpackModule(contentArtifact, sourceDir.getAbsolutePath());
moveBackendConfig(artifactId, sourceDir, targetDir);

FileUtils.deleteQuietly(sourceDir);
}

private static void moveBackendConfig(String artifactId, File sourceDir, File targetDir) throws MojoExecutionException {
try {
File backendConfigFiles = sourceDir.toPath().resolve(BACKEND_CONFIG_FOLDER).toFile();
Path targetPath = targetDir.toPath().toAbsolutePath();

if (backendConfigFiles.exists()) {
File[] configDirectories = backendConfigFiles.listFiles(File::isDirectory);
if (configDirectories != null) {
for (File config : configDirectories) {
Path destDir = targetPath.resolve(config.getName()).resolve(artifactId);
Files.createDirectories(destDir);

//copy config files to the matching configuration folder
FileUtils.copyDirectory(config, destDir.toFile());
}
}
}
}
catch (IOException e) {
throw new MojoExecutionException("Error copying backend configuration: " + e.getMessage(), e);
}
}

public static void downloadAndMoveContentBackendConfig(File serverDirectory, DistroProperties distroProperties, ModuleInstaller moduleInstaller, Wizard wizard) throws MojoExecutionException {
if (distroProperties != null) {
File targetDir = new File(serverDirectory, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
List<Artifact> contents = distroProperties.getContentArtifacts();

if (contents != null) {
for (Artifact content : contents) {
wizard.showMessage("Downloading Content: " + content + "\n");
ContentHelper.downloadContent(content, moduleInstaller, targetDir);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmrs.maven.plugins.utility;

import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -10,7 +11,11 @@
import org.twdata.maven.mojoexecutor.MojoExecutor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.Element;
Expand All @@ -29,6 +34,8 @@
*/
public class ModuleInstaller {

private static final String GOAL_COPY = "copy";

private static final String GOAL_UNPACK = "unpack";

final MavenProject mavenProject;
Expand Down Expand Up @@ -74,18 +81,31 @@ public void installModulesForDistro(Server server, DistroProperties properties,
}

public void installModules(List<Artifact> artifacts, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(artifacts.toArray(new Artifact[0]), outputDir, goal);
prepareModules(artifacts.toArray(new Artifact[0]), outputDir, GOAL_COPY);
}

public void installModules(Artifact[] artifacts, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(artifacts, outputDir, goal);
prepareModules(artifacts, outputDir, GOAL_COPY);
}

public void installModule(Artifact artifact, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(new Artifact[] { artifact }, outputDir, goal);
prepareModules(new Artifact[] { artifact }, outputDir, GOAL_COPY);
}

public void installAndUnpackModule(Artifact artifact, String outputDir) throws MojoExecutionException {
Path markersDirectory;
try {
markersDirectory = Files.createTempDirectory("openmrs-sdk-markers");
} catch (IOException e) {
throw new MojoExecutionException("Error creating markers directory", e);
}

prepareModules(new Artifact[] { artifact }, outputDir, GOAL_UNPACK,
element("overWriteSnapshots", "true"),
element("overWriteReleases", "true"),
element("markersDirectory", markersDirectory.toAbsolutePath().toString()));

FileUtils.deleteQuietly(markersDirectory.toFile());
}

/**
Expand All @@ -95,18 +115,17 @@ public void installModule(Artifact artifact, String outputDir) throws MojoExecut
* @param goal
* @throws MojoExecutionException
*/
private void prepareModules(Artifact[] artifacts, String outputDir, String goal) throws MojoExecutionException {
private void prepareModules(Artifact[] artifacts, String outputDir, String goal, MojoExecutor.Element... additionalConfiguration) throws MojoExecutionException {
MojoExecutor.Element[] artifactItems = new MojoExecutor.Element[artifacts.length];
for (int index = 0; index < artifacts.length; index++) {
artifactItems[index] = artifacts[index].toElement(outputDir);
}

List<MojoExecutor.Element> configuration = new ArrayList<>();
configuration.add(element("artifactItems", artifactItems));
if (goal.equals(GOAL_UNPACK)) {
configuration.add(element("overWriteSnapshots", "true"));
configuration.add(element("overWriteReleases", "true"));
}

configuration.addAll(Arrays.asList(additionalConfiguration));

executeMojo(
plugin(
groupId(SDKConstants.DEPENDENCY_PLUGIN_GROUP_ID),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setVersion(String version){
}

public String getName(){
return getParam("name");
return getParam("name", "openmrs");
}

public void setName(String name){
Expand Down Expand Up @@ -150,6 +150,18 @@ public List<Artifact> getConfigArtifacts() {
return artifactList;
}

public List<Artifact> getContentArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
for (Object keyObject : getAllKeys()) {
String key = keyObject.toString();
if (key.startsWith(TYPE_CONTENT + ".")) {
artifacts.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key),
checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE)));
}
}
return artifacts;
}

protected Set<Object> getAllKeys() {
return properties.keySet();
}
Expand Down Expand Up @@ -237,7 +249,18 @@ else if (key.equals("distro.referenceapplication")) {
* @param key
* @return
*/
public String getParam(String key) {return properties.getProperty(key); }
public String getParam(String key) {
return properties.getProperty(key);
}

/**
* get param from properties
* @param key
* @return
*/
public String getParam(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}

public Artifact getModuleArtifact(String artifactId){
String key = TYPE_OMOD + "." + artifactId;
Expand Down

0 comments on commit 954cce5

Please sign in to comment.