Skip to content

Commit

Permalink
extract logic into propertiesUtils file
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Aug 7, 2024
1 parent 0584011 commit 51cef2d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.utility.PropertiesUtils;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,8 +50,6 @@ public class BuildDistro extends AbstractTask {

private static final String OPENMRS_DISTRO_PROPERTIES = "openmrs-distro.properties";

private static final String CONTENT_PROPERTIES = "content.properties";

private static final String DOCKER_COMPOSE_PATH = "build-distro/docker-compose.yml";

private static final String DOCKER_COMPOSE_OVERRIDE_PATH = "build-distro/docker-compose.override.yml";
Expand Down Expand Up @@ -198,7 +197,7 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
throw new MojoExecutionException("The distro you specified, '" + distro + "' could not be retrieved");
}

parseContentProperties(userDir, distroProperties);
PropertiesUtils.parseContentProperties(userDir, distroProperties);
String distroName = buildDistro(buildDirectory, distroArtifact, distroProperties);

wizard.showMessage(
Expand Down Expand Up @@ -608,90 +607,4 @@ public boolean accept(File dir, String name) {
}
}
}

/**
* Reads the content.properties file.
* <p> For dependencies defined in both content.properties and distro.properties, this ensures the version specified in distro.properties
* matches the range defined in content.properties; otherwise, we output an error to the user to fix this.</p>
* For dependencies not defined in distro.properties but defined in content.properties, the SDK should locate the latest matching version
* and add that to the OMODs and ESMs used by the distro.
*
* @param userDir The directory where the distro.properties or content.properties file is located. This is typically the user's working directory.
* @param distroProperties An object representing the properties defined in distro.properties. This includes versions of various dependencies
* currently used in the distribution.
* @throws MojoExecutionException if there is an error reading the content.properties file or if a version conflict is detected.
*/
private void parseContentProperties(File userDir, DistroProperties distroProperties) throws MojoExecutionException {
File contentFile = new File(userDir, CONTENT_PROPERTIES);
if (!contentFile.exists()) {
log.info("No content.properties file found in the user directory.");
return;
}

Properties contentProperties = new Properties();
String contentPackageName = null;
String contentPackageVersion = null;

try (BufferedReader reader = new BufferedReader(new FileReader(contentFile))) {
contentProperties.load(reader);
contentPackageName = contentProperties.getProperty("name");
contentPackageVersion = contentProperties.getProperty("version");

if (contentPackageName == null || contentPackageVersion == null) {
throw new MojoExecutionException("Content package name or version not specified in content.properties");
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read content.properties file", e);
}

for (String dependency : contentProperties.stringPropertyNames()) {
if (dependency.startsWith("omod.") || dependency.startsWith("owa.") || dependency.startsWith("war")
|| dependency.startsWith("spa.frontendModule") || dependency.startsWith("content.")) {
String versionRange = contentProperties.getProperty(dependency);
String distroVersion = distroProperties.get(dependency);

if (distroVersion == null) {
String latestVersion = findLatestMatchingVersion(dependency);
if (latestVersion == null) {
throw new MojoExecutionException("No matching version found for dependency " + dependency);
}
distroProperties.add(dependency, latestVersion);
} else {
checkVersionInRange(dependency, versionRange, distroVersion, contentPackageName);
}
}
}
}


private String findLatestMatchingVersion(String dependency) {
return distroHelper.findLatestMatchingVersion(dependency);
}

/**
* Checks if the version from distro.properties satisfies the range specified in content.properties.
* Throws an exception if there is a mismatch.
*
* @param contentDependencyKey The key of the content dependency.
* @param contentDependencyVersionRange The version range specified in content.properties.
* @param distroPropertyVersion The version specified in distro.properties.
* @param contentPackageName The name of the content package.
* @throws MojoExecutionException If the version does not fall within the specified range or if the range format is invalid.
*/
private void checkVersionInRange(String contentDependencyKey, String contentDependencyVersionRange, String distroPropertyVersion, String contentPackageName) throws MojoExecutionException {
com.github.zafarkhaja.semver.Version semverVersion = com.github.zafarkhaja.semver.Version.parse(distroPropertyVersion);

try {
boolean inRange = semverVersion.satisfies(contentDependencyVersionRange.trim());
if (!inRange) {
throw new MojoExecutionException(
"Incompatible version for " + contentDependencyKey + " in content package " + contentPackageName + ". Specified range: " + contentDependencyVersionRange
+ ", found in distribution: " + distroPropertyVersion);
}
} catch (ParseException e) {
throw new MojoExecutionException("Invalid version range format for " + contentDependencyKey + " in content package " + contentPackageName + ": " + contentDependencyVersionRange, e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.DBConnector;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.PropertiesUtils;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.openmrs.maven.plugins.utility.ServerHelper;

Expand Down Expand Up @@ -276,6 +277,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
// `setServerVersionsFromDistroProperties`, and `server.setValuesFromDistroPropertiesModules`.
distroHelper.savePropertiesToServer(distroProperties, server);
setServerVersionsFromDistroProperties(server, distroProperties);
PropertiesUtils.parseContentProperties(server.getServerDirectory(), distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper);
setConfigFolder(server, distroProperties);
if (spaInstaller != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -19,6 +21,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.zafarkhaja.semver.ParseException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -28,6 +31,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,6 +40,10 @@

public class PropertiesUtils {

private static final String CONTENT_PROPERTIES = "content.properties";

static DistroHelper distroHelper;

private static final Logger log = LoggerFactory.getLogger(PropertiesUtils.class);

/**
Expand Down Expand Up @@ -301,4 +309,87 @@ private static Document parseXMLFromURL(String url) throws ParserConfigurationEx
document.getDocumentElement().normalize();
return document;
}

/**
* Reads the content.properties file.
* <p> For dependencies defined in both content.properties and distro.properties, this ensures the version specified in distro.properties
* matches the range defined in content.properties; otherwise, we output an error to the user to fix this.</p>
* For dependencies not defined in distro.properties but defined in content.properties, the SDK should locate the latest matching version
* and add that to the OMODs and ESMs used by the distro.
*
* @param userDir The directory where the distro.properties or content.properties file is located. This is typically the user's working directory.
* @param distroProperties An object representing the properties defined in distro.properties. This includes versions of various dependencies
* currently used in the distribution.
* @throws MojoExecutionException if there is an error reading the content.properties file or if a version conflict is detected.
*/
public static void parseContentProperties(File userDir, DistroProperties distroProperties) throws MojoExecutionException {
File contentFile = new File(userDir, CONTENT_PROPERTIES);
if (!contentFile.exists()) {
log.info("No content.properties file found in the user directory.");
return;
}

Properties contentProperties = new Properties();
String contentPackageName = null;
String contentPackageVersion = null;

try (BufferedReader reader = new BufferedReader(new FileReader(contentFile))) {
contentProperties.load(reader);
contentPackageName = contentProperties.getProperty("name");
contentPackageVersion = contentProperties.getProperty("version");

if (contentPackageName == null || contentPackageVersion == null) {
throw new MojoExecutionException("Content package name or version not specified in content.properties");
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read content.properties file", e);
}

for (String dependency : contentProperties.stringPropertyNames()) {
if (dependency.startsWith("omod.") || dependency.startsWith("owa.") || dependency.startsWith("war")
|| dependency.startsWith("spa.frontendModule") || dependency.startsWith("content.")) {
String versionRange = contentProperties.getProperty(dependency);
String distroVersion = distroProperties.get(dependency);

if (distroVersion == null) {
String latestVersion = findLatestMatchingVersion(dependency);
if (latestVersion == null) {
throw new MojoExecutionException("No matching version found for dependency " + dependency);
}
distroProperties.add(dependency, latestVersion);
} else {
checkVersionInRange(dependency, versionRange, distroVersion, contentPackageName);
}
}
}
}

private static String findLatestMatchingVersion(String dependency) {
return distroHelper.findLatestMatchingVersion(dependency);
}

/**
* Checks if the version from distro.properties satisfies the range specified in content.properties.
* Throws an exception if there is a mismatch.
*
* @param contentDependencyKey The key of the content dependency.
* @param contentDependencyVersionRange The version range specified in content.properties.
* @param distroPropertyVersion The version specified in distro.properties.
* @param contentPackageName The name of the content package.
* @throws MojoExecutionException If the version does not fall within the specified range or if the range format is invalid.
*/
private static void checkVersionInRange(String contentDependencyKey, String contentDependencyVersionRange, String distroPropertyVersion, String contentPackageName) throws MojoExecutionException {
com.github.zafarkhaja.semver.Version semverVersion = com.github.zafarkhaja.semver.Version.parse(distroPropertyVersion);

try {
boolean inRange = semverVersion.satisfies(contentDependencyVersionRange.trim());
if (!inRange) {
throw new MojoExecutionException(
"Incompatible version for " + contentDependencyKey + " in content package " + contentPackageName + ". Specified range: " + contentDependencyVersionRange
+ ", found in distribution: " + distroPropertyVersion);
}
} catch (ParseException e) {
throw new MojoExecutionException("Invalid version range format for " + contentDependencyKey + " in content package " + contentPackageName + ": " + contentDependencyVersionRange, e);
}
}
}

0 comments on commit 51cef2d

Please sign in to comment.