This is an automated email from the ASF dual-hosted git repository.
davidb pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git
The following commit(s) were added to refs/heads/master by this push:
new 31b5d26 SLING-7860 Enhance slingfeature-maven-plugin to aggregate
multiple features into a single feature
31b5d26 is described below
commit 31b5d26e8073b033a917f4b43fb24d9315563401
Author: David Bosschaert <[email protected]>
AuthorDate: Wed Aug 29 11:13:31 2018 +0100
SLING-7860 Enhance slingfeature-maven-plugin to aggregate multiple features
into a single feature
Add support for includes and excludes to the directory source.
---
.../feature/maven/mojos/AggregateFeatures.java | 26 +++++++++
.../feature/maven/mojos/AggregateFeaturesTest.java | 67 +++++++++++++++++++++-
.../resources/aggregate-features/dir/test_v.json | 6 ++
.../resources/aggregate-features/dir/test_w.json | 6 ++
.../aggregate-features/dir/test_x.feature | 8 +++
.../resources/aggregate-features/dir2/test_w.json | 6 ++
.../resources/aggregate-features/dir2/test_y.json | 9 +++
.../resources/aggregate-features/dir2/test_z.json | 12 ++++
8 files changed, 139 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
index 59e86a8..29ea4ae 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/AggregateFeatures.java
@@ -140,11 +140,37 @@ public class AggregateFeatures extends
AbstractFeatureMojo {
}
private void readFeaturesFromDirectory(FeatureConfig fc, Map<ArtifactId,
Feature> featureMap) throws IOException {
+ nextFile:
for (File f : new File(fc.location).listFiles()) {
+ boolean matchesIncludes = fc.includes.size() == 0;
+ for (String inc : fc.includes) {
+ inc = convertGlobToRegex(inc);
+ if (f.getName().matches(inc)) {
+ matchesIncludes = true;
+ break;
+ }
+ }
+
+ if (!matchesIncludes)
+ continue nextFile;
+
+ for (String exc : fc.excludes) {
+ exc = convertGlobToRegex(exc);
+ if (f.getName().matches(exc)) {
+ continue nextFile;
+ }
+ }
+
readFeatureFromFile(f, featureMap);
}
}
+ private String convertGlobToRegex(String glob) {
+ glob = glob.replace(".", "[.]");
+ glob = glob.replace("*", ".*");
+ return glob;
+ }
+
private void readFeatureFromFile(File f, Map<ArtifactId, Feature>
featureMap) throws IOException {
String content = new String(Files.readAllBytes(f.toPath()));
content = Substitution.replaceMavenVars(project, content);
diff --git
a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
index 3b04ede..e344f3d 100644
---
a/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
+++
b/src/test/java/org/apache/sling/feature/maven/mojos/AggregateFeaturesTest.java
@@ -49,6 +49,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
+import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@@ -108,10 +109,74 @@ public class AggregateFeaturesTest {
@Test
public void testAggregateFeaturesFromDirectory() throws Exception {
File featuresDir = new File(
+ getClass().getResource("/aggregate-features/dir2").getFile());
+
+ FeatureConfig fc = new FeatureConfig();
+ fc.setLocation(featuresDir.getAbsolutePath());
+
+ Build mockBuild = Mockito.mock(Build.class);
+ Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
+
+ MavenProject mockProj = Mockito.mock(MavenProject.class);
+ Mockito.when(mockProj.getBuild()).thenReturn(mockBuild);
+ Mockito.when(mockProj.getGroupId()).thenReturn("org.foo");
+ Mockito.when(mockProj.getArtifactId()).thenReturn("org.foo.bar");
+ Mockito.when(mockProj.getVersion()).thenReturn("1.2.3-SNAPSHOT");
+
+ AggregateFeatures af = new AggregateFeatures();
+ af.classifier = "aggregated";
+ af.features = Collections.singletonList(fc);
+ af.project = mockProj;
+
+ af.execute();
+
+ File expectedFile = new File(tempDir.toFile(),
FeatureConstants.FEATURE_PROCESSED_LOCATION + "/aggregated.json");
+ try (Reader fr = new FileReader(expectedFile)) {
+ Feature genFeat = FeatureJSONReader.read(fr, null,
FeatureJSONReader.SubstituteVariables.NONE);
+ ArtifactId id = genFeat.getId();
+
+ assertEquals("org.foo", id.getGroupId());
+ assertEquals("org.foo.bar", id.getArtifactId());
+ assertEquals("1.2.3-SNAPSHOT", id.getVersion());
+ assertEquals("slingfeature", id.getType());
+ assertEquals("aggregated", id.getClassifier());
+
+ Set<ArtifactId> expectedBundles = new HashSet<>();
+ expectedBundles.add(
+ new ArtifactId("org.apache.aries",
"org.apache.aries.util", "1.1.3", null, null));
+ expectedBundles.add(
+ new ArtifactId("org.apache.sling", "someotherbundle", "1",
null, null));
+ Set<ArtifactId> actualBundles = new HashSet<>();
+ for (org.apache.sling.feature.Artifact art : genFeat.getBundles())
{
+ actualBundles.add(art.getId());
+ }
+ assertEquals(expectedBundles, actualBundles);
+
+ Map<String, Dictionary<String, Object>> expectedConfigs = new
HashMap<>();
+ expectedConfigs.put("some.pid", new
Hashtable<>(Collections.singletonMap("x", "y")));
+ Dictionary<String, Object> dict = new Hashtable<>();
+ dict.put("foo", 123L);
+ dict.put("bar", Boolean.TRUE);
+ expectedConfigs.put("another.pid", dict);
+
+ Map<String, Dictionary<String, Object>> actualConfigs = new
HashMap<>();
+ for (org.apache.sling.feature.Configuration conf :
genFeat.getConfigurations()) {
+ actualConfigs.put(conf.getPid(), conf.getProperties());
+ }
+ assertEquals(expectedConfigs, actualConfigs);
+ }
+ }
+
+ @Test
+ public void testAggregateFeaturesFromDirectoryWithIncludesExcludes()
throws Exception {
+ File featuresDir = new File(
getClass().getResource("/aggregate-features/dir").getFile());
FeatureConfig fc = new FeatureConfig();
- fc.location = featuresDir.getAbsolutePath();
+ fc.setLocation(featuresDir.getAbsolutePath());
+ fc.setIncludes("*.json");
+ fc.setExcludes("*_v*");
+ fc.setExcludes("test_w.json");
Build mockBuild = Mockito.mock(Build.class);
Mockito.when(mockBuild.getDirectory()).thenReturn(tempDir.toString());
diff --git a/src/test/resources/aggregate-features/dir/test_v.json
b/src/test/resources/aggregate-features/dir/test_v.json
new file mode 100644
index 0000000..4b09d5d
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir/test_v.json
@@ -0,0 +1,6 @@
+{
+ "id":"test:v:9.9.9",
+ "bundles":[
+ "org.apache.sling:someotherbundle:0.9"
+ ]
+}
diff --git a/src/test/resources/aggregate-features/dir/test_w.json
b/src/test/resources/aggregate-features/dir/test_w.json
new file mode 100644
index 0000000..3089f0b
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir/test_w.json
@@ -0,0 +1,6 @@
+{
+ "id":"test:w:9.9.9",
+ "bundles":[
+ "org.apache.sling:someotherbundle:1"
+ ]
+}
diff --git a/src/test/resources/aggregate-features/dir/test_x.feature
b/src/test/resources/aggregate-features/dir/test_x.feature
new file mode 100644
index 0000000..cd606da
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir/test_x.feature
@@ -0,0 +1,8 @@
+{
+ "id":"test:x:9.9.9",
+ "bundles":[
+ {
+ "id":"org.apache.sling:somebundle:1.2.3"
+ }
+ ]
+}
diff --git a/src/test/resources/aggregate-features/dir2/test_w.json
b/src/test/resources/aggregate-features/dir2/test_w.json
new file mode 100644
index 0000000..3089f0b
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir2/test_w.json
@@ -0,0 +1,6 @@
+{
+ "id":"test:w:9.9.9",
+ "bundles":[
+ "org.apache.sling:someotherbundle:1"
+ ]
+}
diff --git a/src/test/resources/aggregate-features/dir2/test_y.json
b/src/test/resources/aggregate-features/dir2/test_y.json
new file mode 100644
index 0000000..08c5416
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir2/test_y.json
@@ -0,0 +1,9 @@
+{
+ "id":"test:y:9.9.9",
+ "bundles":[
+ {
+ "id":"org.apache.aries:org.apache.aries.util:1.1.3",
+ "start-level":"20"
+ }
+ ]
+}
diff --git a/src/test/resources/aggregate-features/dir2/test_z.json
b/src/test/resources/aggregate-features/dir2/test_z.json
new file mode 100644
index 0000000..9ad87e5
--- /dev/null
+++ b/src/test/resources/aggregate-features/dir2/test_z.json
@@ -0,0 +1,12 @@
+{
+ "id":"test:z:1.0.1",
+ "configurations": {
+ "some.pid": {
+ "x": "y"
+ },
+ "another.pid": {
+ "foo": 123,
+ "bar": true
+ }
+ }
+}