Skip to content

Commit

Permalink
SDK-306: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith committed Jul 6, 2023
1 parent 98cbf6d commit 8016d9b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.utility.ConfigurationInstaller;
import org.openmrs.maven.plugins.utility.DefaultJira;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.git.DefaultGitHelper;
Expand Down Expand Up @@ -134,6 +135,11 @@ public abstract class AbstractTask extends AbstractMojo {
*/
DockerHelper dockerHelper;

/**
* Installs configurations
*/
ConfigurationInstaller configurationInstaller;

public AbstractTask() {
}

Expand All @@ -156,6 +162,7 @@ public AbstractTask(AbstractTask other) {
this.testMode = other.testMode;
this.openMRSPath = other.openMRSPath;
this.stats = other.stats;
this.configurationInstaller = other.configurationInstaller;
initTask();
}

Expand Down Expand Up @@ -184,6 +191,9 @@ public void initTask() {
if (dockerHelper == null) {
dockerHelper = new DockerHelper(mavenProject, mavenSession, pluginManager, wizard);
}
if(configurationInstaller == null) {
configurationInstaller = new ConfigurationInstaller(wizard, moduleInstaller);
}
if (StringUtils.isNotBlank(openMRSPath)) {
Server.setServersPath(openMRSPath);
}
Expand Down
74 changes: 4 additions & 70 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper);
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(),
server.getDistroGroupId(), "zip");
distroProperties = distroHelper.downloadDistroProperties(server.getServerDirectory(), server, "zip");
distroProperties.addProperties(PropertiesUtils.getConfigurationProperty(artifact));
Properties frontendProperties;
if (server.getVersion().equals(versionsHelper.getLatestSnapshotVersion(artifact))) {
frontendProperties = PropertiesUtils.getFrontendPropertiesFromSpaConfigUrl(
Expand All @@ -210,19 +212,7 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
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(spaCoreVersion != null) {
frontendProperties.setProperty("spa.core", spaCoreVersion);
}
Properties allProperties = new Properties();
allProperties.putAll(backendProperties);
allProperties.putAll(spaModuleProperty);
allProperties.putAll(frontendProperties);
allProperties.putAll(configurationProperties);
distroProperties = new DistroProperties(allProperties);
distroProperties.addProperties(frontendProperties);
platformMode = false;
break;

Expand Down Expand Up @@ -283,7 +273,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
distroHelper.savePropertiesToServer(distroProperties, server);
setServerVersionsFromDistroProperties(server, distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper);
setConfigurationFolder(server, distroProperties);
configurationInstaller.setConfigurationFolder(server, distroProperties);
if (spaInstaller != null) {
spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties);
}
Expand Down Expand Up @@ -349,62 +339,6 @@ private void downloadOWAs(File targetDirectory, DistroProperties distroPropertie
}
}

/**
* Sets the configuration folder for the specified server using the provided distro properties.
*
* @param server The server for which to set the configuration folder.
* @param distroProperties The distro properties containing the configuration information.
*/
private void setConfigurationFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException {
if(distroProperties.getConfigArtifacts().isEmpty()) {
return;
}
File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configDir.mkdir();
downloadConfigurations(distroProperties, configDir);
for(Artifact artifact : distroProperties.getConfigArtifacts()) {
String configurationFileName = artifact.getArtifactId() + "-" + artifact.getVersion() + ".zip";
File referenceApplicationFile = new File(configDir, configurationFileName);
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(referenceApplicationFile);
}
catch (ZipException | IOException e) {
throw new RuntimeException(e);
}

}

}

/**
* Downloads the configuration artifact specified in the distro properties and saves them in the provided config directory.
*
* @param distroProperties The distro properties containing the configuration artifacts to download.
* @param configDir The directory where the configuration files will be saved.
* @throws MojoExecutionException If an error occurs while downloading the configuration files.
*/
private void downloadConfigurations(DistroProperties distroProperties, File configDir) throws MojoExecutionException {
List<Artifact> configs = distroProperties.getConfigArtifacts();
wizard.showMessage("Downloading Configs...\n");
if (!configs.isEmpty()) {
for (Artifact config : configs) {
wizard.showMessage("Downloading Config: " + config);
moduleInstaller.installModule(config, configDir.getPath());
}
}
}

private void wipeDatabase(Server server) throws MojoExecutionException {
String uri = getUriWithoutDb(server);
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.openmrs.maven.plugins.utility;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
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;
import org.openmrs.maven.plugins.model.Server;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

public class ConfigurationInstaller {

private final Wizard wizard;

private final ModuleInstaller moduleInstaller;

public ConfigurationInstaller(Wizard wizard, ModuleInstaller moduleInstaller) {
this.wizard = wizard;
this.moduleInstaller = moduleInstaller;
}

/**
* Sets the configuration folder for the specified server using the provided distro properties.
*
* @param server The server for which to set the configuration folder.
* @param distroProperties The distro properties containing the configuration information.
*/
public void setConfigurationFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException {
if(distroProperties.getConfigArtifacts().isEmpty()) {
return;
}
File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configDir.mkdir();
downloadConfigurations(distroProperties, configDir);
File distroJar = new File(server.getServerDirectory(),"openmrs-distro.jar");
if (!distroJar.exists()) {
return;
}
try {
ZipFile zipFile = new ZipFile(distroJar);
zipFile.extractAll(configDir.getPath());
for (File file : Objects.requireNonNull(configDir.listFiles())) {
if (file.getName().equals("openmrs_config")) {
FileUtils.copyDirectory(file, configDir);
}
file.delete();
}
}
catch (ZipException | IOException e) {
throw new RuntimeException(e);
}
finally {
distroJar.delete();
}
}

/**
* Downloads the configuration artifact specified in the distro properties and saves them in the provided config directory.
*
* @param distroProperties The distro properties containing the configuration artifacts to download.
* @param configDir The directory where the configuration files will be saved.
* @throws MojoExecutionException If an error occurs while downloading the configuration files.
*/
private void downloadConfigurations(DistroProperties distroProperties, File configDir) throws MojoExecutionException {
List<Artifact> configs = distroProperties.getConfigArtifacts();
wizard.showMessage("Downloading Configs...\n");
if (!configs.isEmpty()) {
for (Artifact config : configs) {
config.setDestFileName("openmrs-distro.jar");
wizard.showMessage("Downloading Config: " + config);
moduleInstaller.installModule(config, configDir.getPath());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public void resolvePlaceholders(Properties projectProperties) throws MojoExecuti
}
}

public void addProperties(Properties properties) {
this.properties.putAll(properties);
}

private String getPlaceholderKey(String string){
int startIndex = string.indexOf("${")+2;
int endIndex = string.indexOf("}", startIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,8 @@ 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
);
return promptForO3Version(optionsMap, customMessage);
}

Expand Down Expand Up @@ -771,17 +771,14 @@ private Map<String, String> getDistroVersionsOptionsMap(Set<String> versions, Ve
/**
* Returns a map of options based on the versions of O3
*
* @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.
* @param versionsHelper The VersionsHelper object to retrieve the artifact versions from.
* @return A LinkedHashMap containing the generated options map.
*/
private Map<String, String> getO3VersionsOptionsMap(VersionsHelper versionsHelper,
String optionTemplate, String artifactTemplate) {
private Map<String, String> getO3VersionsOptionsMap(VersionsHelper versionsHelper) {
Map<String, String> optionsMap = new LinkedHashMap<>();
Artifact artifact = new Artifact("referenceapplication-distro", null, "org.openmrs.distro", "zip");
Artifact artifact = new Artifact("referenceapplication-distro", "version", "org.openmrs.distro", "zip");
for (ArtifactVersion version : versionsHelper.getAllVersions(artifact, MAX_OPTIONS_SIZE)) {
optionsMap.put(String.format(optionTemplate, version.toString()), String.format(artifactTemplate, version));
optionsMap.put(String.format(DefaultWizard.REFAPP_OPTION_TMPL, version.toString()), String.format(DefaultWizard.O3_ARTIFACT_TMPL, version));
}
return optionsMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,29 @@ public DistroProperties downloadDistroProperties(File path, Artifact artifact) t
return distroProperties;
}

public DistroProperties downloadDistroProperties(File path, Artifact artifact, String distroPropertiesFileName) throws MojoExecutionException {
File file = downloadDistro(path, artifact);
DistroProperties distroProperties = null;
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (distroPropertiesFileName.equals(zipEntry.getName())) {
Properties properties = new Properties();
properties.load(zipFile.getInputStream(zipEntry));
distroProperties = new DistroProperties(properties);
}
}
}
catch (IOException e) {
throw new MojoExecutionException("Could not read \"" + file.getAbsolutePath() + "\" " + e.getMessage(), e);
}
finally {
file.delete();
}
return distroProperties;
}

public DistroProperties downloadDistroProperties(File serverPath, Server server) throws MojoExecutionException {
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(),
"jar");
Expand All @@ -270,6 +293,16 @@ public DistroProperties downloadDistroProperties(File serverPath, Server server)
}
}

public DistroProperties downloadDistroProperties(File serverPath, Server server, String fileType) throws MojoExecutionException {
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(),
fileType);
if (StringUtils.isNotBlank(artifact.getArtifactId())) {
return downloadDistroProperties(serverPath, artifact, "distro.properties");
} else {
return null;
}
}

/**
* Distro can be passed in two ways: either as maven artifact identifier or path to distro file
* Returns null if string is invalid as path or identifier
Expand Down

0 comments on commit 8016d9b

Please sign in to comment.