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

O3-3511: Content package to create zip file #1

Merged
merged 15 commits into from
Aug 15, 2024
Merged
26 changes: 26 additions & 0 deletions assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>null</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>content.properties</include>
</includes>
</fileSet>
<fileSet>
<directory>configuration</directory>
<outputDirectory>/configuration</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
6 changes: 3 additions & 3 deletions content.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#name=Ref 3.x distro
#version=3.0.0
#war.openmrs=2.6.1-SNAPSHOT
omod.fhir2=1.9.0
omod.fhir2=1.2.3
omod.webservices.rest=2.40.0-SNAPSHOT
spa.frontendModules.@ohri/openmrs-esm-ohri-hiv-care-treatment-app=latest
spa.frontendModules.@ohri/openmrs-esm-ohri-core-app=latest
spa.frontendModules.@ohri/openmrs-esm-ohri-hiv-care-treatment-app=2.40.0-SNAPSHOT
spa.frontendModules.@ohri/openmrs-esm-ohri-core-app=2.40.0-SNAPSHOT
#spa.frontendModules.@openmrs/esm-primary-navigation-app=next
114 changes: 107 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,109 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openmrs</groupId>
<artifactId>openmrs-content-hiv</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - openmrs-content-hiv</name>
<url>http://maven.apache.org</url>
<modelVersion>4.0.0</modelVersion>
<groupId>org.openmrs</groupId>
<artifactId>content.hiv</artifactId>
rbuisson marked this conversation as resolved.
Show resolved Hide resolved
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HIV Care and Treatment Content Package</name>
<description>Content package for HIV care and treatment module in OpenMRS</description>
<url>https://openmrs.org</url>
<developers>
<developer>
<name>OpenMRS</name>
<organization>OpenMRS</organization>
<organizationUrl>http://openmrs.org</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git@github.com:openmrs/openmrs-content-hiv.git</connection>
<developerConnection>scm:git:git@github.com:openmrs/openmrs-content-hiv.git</developerConnection>
<url>https://github.com/openmrs/openmrs-content-hiv.git</url>
<tag>HEAD</tag>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
ibacher marked this conversation as resolved.
Show resolved Hide resolved
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<java classname="org.openmrs.maven.plugins.model.ContentPropertiesValidator" fork="true" failonerror="true">
<arg value="${project.basedir}/content.properties"/>
<classpath>
<path refid="maven.runtime.classpath"/>
</classpath>
</java>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always use the latest version available, if possible.

Suggested change
<version>3.3.0</version>
<version>3.7.1</version>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<!-- Ensure this is set to false -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd likely be nice to have a why for this comment, but at least it should go with the right part.

Suggested change
<appendAssemblyId>false</appendAssemblyId>
<!-- Ensure this is set to false -->
<!-- Ensure this is set to false -->
<appendAssemblyId>false</appendAssemblyId>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added a comment, at the top instead of bottom.

<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>openmrs-repo-releases</id>
<name>OpenMRS Nexus Releases</name>
<url>https://mavenrepo.openmrs.org/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>openmrs-repo-snapshots</id>
<name>OpenMRS Nexus Snapshots</name>
<url>https://mavenrepo.openmrs.org/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.openmrs.maven.plugins.model;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.regex.Pattern;

/**
* This class is used by Maven to perform validation of property values based on a semantic versioning format using regular expressions.
* It validates whether the property values in a given properties file match the expected semantic versioning patterns.
* The validation includes support for range operators and excludes specific non-version strings like "latest".
*
*/
public class ContentPropertiesValidator {

// Enhanced regex pattern to include range operators and support pre-release and build metadata
private static final Pattern VERSION_PATTERN = Pattern.compile(
"^(\\s*[=><~^]*\\s*\\d+(\\.\\d+){0,2}(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?)" +
"(\\s*(-|\\|\\|)\\s*[=><~^]*\\s*\\d+(\\.\\d+){0,2}(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?)*$"
);

/**
* Validates the properties in the given file.
*
* @param filePath the path to the properties file
* @return true if all property values are valid version formats, false otherwise
* @throws IOException if an error occurs while reading the file
*/
public static boolean validateProperties(String filePath) throws IOException {
try (InputStream input = new FileInputStream(filePath)) {
Properties properties = new Properties();
properties.load(input);

for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
if (!isValidVersion(value)) {
System.out.println("Invalid version format for key: " + key + ", value: " + value);
return false;
}
}
return true;
} catch (IOException e) {
System.err.println("Error reading properties file: " + e.getMessage());
throw e;
}
}

/**
* Cleans the value by trimming whitespace.
*
* @param value the value to clean
* @return the cleaned value
*/
private static String cleanValue(String value) {
return value.trim();
}

/**
* Checks if the given value is a valid version format.
*
* @param value the value to check
* @return true if the value is a valid version format, false otherwise
*/
protected static boolean isValidVersion(String value) {
value = cleanValue(value);
// Exclude "latest" explicitly
if ("latest".equalsIgnoreCase(value)) {
return false;
}
return VERSION_PATTERN.matcher(value).matches();
}

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java org.openmrs.maven.plugins.model.ContentPropertiesValidator <properties-file-path>");
System.exit(1);
}

String filePath = args[0];
try {
boolean isValid = validateProperties(filePath);
if (isValid) {
System.out.println("All properties have valid version formats.");
System.exit(0); // Exit with zero status to indicate success
} else {
System.out.println("Some properties have invalid version formats.");
System.exit(1); // Exit with non-zero status to indicate failure
}
} catch (IOException e) {
System.err.println("Error reading properties file: " + e.getMessage());
System.exit(1); // Exit with non-zero status to indicate failure
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.openmrs.maven.plugins.model;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;

public class ContentPropertiesValidatorTest {

@Before
public void setUp() {
// Initialization if necessary
}

@Test
public void testValidVersions() {
assertTrue(ContentPropertiesValidator.isValidVersion("0.13.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("3.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("1.0.0"));
}

@Test
public void testValidVersionRanges() {
assertTrue(ContentPropertiesValidator.isValidVersion("^0.13.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("~0.13.0"));
assertTrue(ContentPropertiesValidator.isValidVersion(">0.13.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("<3.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion(">=3.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("<=3.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("=3.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("1.0.0 - 1.10.10"));
assertTrue(ContentPropertiesValidator.isValidVersion("<2.1.0 || >2.6.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("=3.0.0"));
}

@Test
public void testInvalidVersions() {
assertFalse(ContentPropertiesValidator.isValidVersion("abc"));
assertFalse(ContentPropertiesValidator.isValidVersion("1..0"));
assertFalse(ContentPropertiesValidator.isValidVersion("1.0.0.0"));
assertFalse(ContentPropertiesValidator.isValidVersion("latest")); // Ensure "latest" is not valid
}

@Test
public void testPreReleaseAndBuildMetadata() {
assertTrue(ContentPropertiesValidator.isValidVersion("1.0.0-alpha"));
assertTrue(ContentPropertiesValidator.isValidVersion("1.0.0+20130313144700"));
assertTrue(ContentPropertiesValidator.isValidVersion("1.0.0-beta+exp.sha.5114f85"));
}

@Test
public void testComplexRanges() {
assertFalse(ContentPropertiesValidator.isValidVersion(">=1.0.0 <2.0.0"));
assertTrue(ContentPropertiesValidator.isValidVersion("1.2.3 - 2.3.4"));
assertFalse(ContentPropertiesValidator.isValidVersion(">=1.2.3 <2.3.4 || >=3.0.0"));
}
}