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-306: Integrated Micro Frontend Tooling Setup in OpenMRS SDK - Reactoring #224

Closed
wants to merge 5 commits into from
Closed
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
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
63 changes: 4 additions & 59 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public Setup(AbstractTask other) {
* file will be created after the database is initialized. Setup should proceed to install
* modules based on the OpenMRS WAR file for the given platform version.
* As of this writing, this function can return null only in platform mode.
*
openmrs-sdk [org.openmrs.maven.plugins:openmrs-sdk-maven-plugin:5.0.0-SNAPSHOT:setup]*
* @param server An initialized Server instance
* @return distro properties instantiated by DistroHelper
* @throws MojoExecutionException
Expand Down Expand Up @@ -205,6 +205,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 @@ -220,12 +222,6 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu
if(appShellVersion != null) {
frontendProperties.setProperty("spa.core", appShellVersion);
}
Properties allProperties = new Properties();
allProperties.putAll(backendProperties);
allProperties.putAll(spaModuleProperty);
allProperties.putAll(frontendProperties);
allProperties.putAll(configurationProperties);
distroProperties = new DistroProperties(allProperties);
platformMode = false;
break;

Expand Down Expand Up @@ -286,7 +282,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
distroHelper.savePropertiesToServer(distroProperties, server);
setServerVersionsFromDistroProperties(server, distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper);
setConfigFolder(server, distroProperties);
configurationInstaller.setConfigurationFolder(server, distroProperties);
if (spaInstaller != null) {
spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies);
}
Expand Down Expand Up @@ -352,57 +348,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 setConfigFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException {
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);
}
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 downloadConfigs(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);
owaHelper.downloadOwa(configDir, config, moduleInstaller);
}
}
}

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,14 @@ public void resolvePlaceholders(Properties projectProperties) throws MojoExecuti
}
}

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

public void addProperty(String name, String value) {
this.properties.setProperty(name, value);
}

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", "3.0.0-SNAPSHOT", "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