From 51cef2d4dee48bb4bdc204c370403fee55bffbab Mon Sep 17 00:00:00 2001 From: mherman22 Date: Fri, 2 Aug 2024 20:57:24 +0300 Subject: [PATCH] extract logic into propertiesUtils file --- .../openmrs/maven/plugins/BuildDistro.java | 91 +------------------ .../java/org/openmrs/maven/plugins/Setup.java | 2 + .../plugins/utility/PropertiesUtils.java | 91 +++++++++++++++++++ 3 files changed, 95 insertions(+), 89 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java index 0070f481..2ef46e1b 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java @@ -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; @@ -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"; @@ -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( @@ -608,90 +607,4 @@ public boolean accept(File dir, String name) { } } } - - /** - * Reads the content.properties file. - *

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.

- * 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); - } - } - - } 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 00b23e95..34a5ac27 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 @@ -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; @@ -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) { 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 9bf8955e..e07280e7 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 @@ -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; @@ -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; @@ -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; @@ -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); /** @@ -301,4 +309,87 @@ private static Document parseXMLFromURL(String url) throws ParserConfigurationEx document.getDocumentElement().normalize(); return document; } + + /** + * Reads the content.properties file. + *

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.

+ * 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); + } + } }