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-whiteboard.git
The following commit(s) were added to refs/heads/master by this push: new 29c77c1 Add the ability to set groupId and version for generated features 29c77c1 is described below commit 29c77c147ae871cd9dcdb6cc463a952fb9aa2c4e Author: David Bosschaert <david.bosscha...@gmail.com> AuthorDate: Tue Apr 10 11:31:00 2018 +0100 Add the ability to set groupId and version for generated features --- .../modelconverter/impl/ProvisioningToFeature.java | 31 +++++++++++++++------- .../modelconverter/impl/ModelConverterTest.java | 25 +++++++++++++++-- .../src/test/resources/boot.json | 2 +- .../test/resources/{boot.json => boot_gav.json} | 2 +- .../src/test/resources/launchpad.json | 2 +- .../src/test/resources/oak.json | 2 +- .../src/test/resources/repoinit.json | 2 +- 7 files changed, 50 insertions(+), 16 deletions(-) diff --git a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java index 0db9c2b..ce77d76 100644 --- a/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java +++ b/featuremodel/feature-modelconverter/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java @@ -68,9 +68,9 @@ import java.util.Set; public class ProvisioningToFeature { private static Logger LOGGER = LoggerFactory.getLogger(ProvisioningToFeature.class); - public static List<File> convert(File file, File outDir) { + public static List<File> convert(File file, File outDir, Map<String, Object> options) { Model model = createModel(Collections.singletonList(file), null, true, false); - final List<org.apache.sling.feature.Feature> features = buildFeatures(model); + final List<org.apache.sling.feature.Feature> features = buildFeatures(model, options); String bareFileName = file.getName(); int idx = bareFileName.lastIndexOf('.'); @@ -108,7 +108,7 @@ public class ProvisioningToFeature { writeApplication(app, outputFile); } else { - final List<org.apache.sling.feature.Feature> features = buildFeatures(model); + final List<org.apache.sling.feature.Feature> features = buildFeatures(model, Collections.emptyMap()); int index = 1; for(final org.apache.sling.feature.Feature feature : features) { writeFeature(feature, outputFile, features.size() > 1 ? index : 0); @@ -448,20 +448,24 @@ public class ProvisioningToFeature { } - private static List<org.apache.sling.feature.Feature> buildFeatures(final Model model) { + private static List<org.apache.sling.feature.Feature> buildFeatures(Model model, Map<String, Object> options) { final List<org.apache.sling.feature.Feature> features = new ArrayList<>(); + String groupId = getOption(options, "groupId", "generated"); + String version = getOption(options, "version", "1.0.0"); + for(final Feature feature : model.getFeatures() ) { final String idString; - // use a default name if not present or not usable as a Maven artifactId ( starts with ':') - if ( feature.getName() != null && !feature.isSpecial() ) { + String name = feature.getName(); + if ( name != null ) { + name = name.replaceAll("[:]", ""); if ( feature.getVersion() != null ) { - idString = "generated/" + feature.getName() + "/" + feature.getVersion(); + idString = groupId + "/" + name + "/" + feature.getVersion(); } else { - idString = "generated/" + feature.getName() + "/1.0.0"; + idString = groupId + "/" + name + "/" + version; } } else { - idString = "generated/feature/1.0.0"; + idString = groupId + "/feature/" + version; } final org.apache.sling.feature.Feature f = new org.apache.sling.feature.Feature(ArtifactId.parse(idString)); features.add(f); @@ -472,6 +476,15 @@ public class ProvisioningToFeature { return features; } + @SuppressWarnings("unchecked") + private static <T> T getOption(Map<String, Object> options, String name, T defaultValue) { + if (options.containsKey(name)) { + return (T) options.get(name); + } else { + return defaultValue; + } + } + private static void writeApplication(final Application app, final String out) { LOGGER.info("Writing application..."); final File file = new File(out); diff --git a/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java b/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java index df0eacf..1b5f4aa 100644 --- a/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java +++ b/featuremodel/feature-modelconverter/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java @@ -160,11 +160,31 @@ public class ModelConverterTest { } } + @Test + public void testModelGAV() throws Exception { + String originalProvModel = "/boot.txt"; + String expectedJSON = "/boot_gav.json"; + + File inFile = new File(getClass().getResource(originalProvModel).toURI()); + + Map<String, Object> options = new HashMap<>(); + options.put("groupId", "testing123"); + options.put("version", "4.5.6"); + List<File> files = ProvisioningToFeature.convert(inFile, tempDir.toFile(), options); + assertEquals("The testing code expects a single output file here", 1, files.size()); + File outFile = files.get(0); + + String expectedFile = new File(getClass().getResource(expectedJSON).toURI()).getAbsolutePath(); + org.apache.sling.feature.Feature expected = FeatureUtil.getFeature(expectedFile, artifactManager, SubstituteVariables.NONE); + org.apache.sling.feature.Feature actual = FeatureUtil.getFeature(outFile.getAbsolutePath(), artifactManager, SubstituteVariables.NONE); + assertFeaturesEqual(expected, actual); + } + public void testConvertFromProvModelRoundTrip(File orgProvModel) throws Exception { System.out.println("*** Roundtrip converting: " + orgProvModel.getName()); List<File> allGenerateProvisioningModelFiles = new ArrayList<>(); - List<File> generated = ProvisioningToFeature.convert(orgProvModel, tempDir.toFile()); + List<File> generated = ProvisioningToFeature.convert(orgProvModel, tempDir.toFile(), Collections.emptyMap()); for (File f : generated) { String baseName = f.getName().substring(0, f.getName().length() - ".json".length()); @@ -181,7 +201,7 @@ public class ModelConverterTest { public void testConvertToFeature(String originalProvModel, String expectedJSON) throws Exception { File inFile = new File(getClass().getResource(originalProvModel).toURI()); - List<File> files = ProvisioningToFeature.convert(inFile, tempDir.toFile()); + List<File> files = ProvisioningToFeature.convert(inFile, tempDir.toFile(), Collections.emptyMap()); assertEquals("The testing code expects a single output file here", 1, files.size()); File outFile = files.get(0); @@ -234,6 +254,7 @@ public class ModelConverterTest { } private void assertFeaturesEqual(org.apache.sling.feature.Feature expected, org.apache.sling.feature.Feature actual) { + assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getTitle(), actual.getTitle()); assertEquals(expected.getDescription(), actual.getDescription()); assertEquals(expected.getVendor(), actual.getVendor()); diff --git a/featuremodel/feature-modelconverter/src/test/resources/boot.json b/featuremodel/feature-modelconverter/src/test/resources/boot.json index e535f7b..313fec9 100644 --- a/featuremodel/feature-modelconverter/src/test/resources/boot.json +++ b/featuremodel/feature-modelconverter/src/test/resources/boot.json @@ -2,7 +2,7 @@ "#": "The model version defaults to 1 if not specified", "model-version": "1", - "id": "org.apache.sling.simple/boot/1.0.0", + "id": "generated/boot/1.0.0", "variables": { "slf4j.version": "1.7.25", diff --git a/featuremodel/feature-modelconverter/src/test/resources/boot.json b/featuremodel/feature-modelconverter/src/test/resources/boot_gav.json similarity index 98% copy from featuremodel/feature-modelconverter/src/test/resources/boot.json copy to featuremodel/feature-modelconverter/src/test/resources/boot_gav.json index e535f7b..1d997f8 100644 --- a/featuremodel/feature-modelconverter/src/test/resources/boot.json +++ b/featuremodel/feature-modelconverter/src/test/resources/boot_gav.json @@ -2,7 +2,7 @@ "#": "The model version defaults to 1 if not specified", "model-version": "1", - "id": "org.apache.sling.simple/boot/1.0.0", + "id": "testing123/boot/4.5.6", "variables": { "slf4j.version": "1.7.25", diff --git a/featuremodel/feature-modelconverter/src/test/resources/launchpad.json b/featuremodel/feature-modelconverter/src/test/resources/launchpad.json index 2fa94b3..0c103ba 100644 --- a/featuremodel/feature-modelconverter/src/test/resources/launchpad.json +++ b/featuremodel/feature-modelconverter/src/test/resources/launchpad.json @@ -1,5 +1,5 @@ { - "id":"generated:feature:1.0.0", + "id":"generated:launchpad:1.0.0", "variables":{ "provisioning.model.name":":launchpad" }, diff --git a/featuremodel/feature-modelconverter/src/test/resources/oak.json b/featuremodel/feature-modelconverter/src/test/resources/oak.json index 03f64c3..0db7923 100644 --- a/featuremodel/feature-modelconverter/src/test/resources/oak.json +++ b/featuremodel/feature-modelconverter/src/test/resources/oak.json @@ -1,5 +1,5 @@ { - "id": "org.apache.sling.simple/oak/1.0.0", + "id": "generated/oak/1.0.0", "variables": { "oak.version": "1.6.8" diff --git a/featuremodel/feature-modelconverter/src/test/resources/repoinit.json b/featuremodel/feature-modelconverter/src/test/resources/repoinit.json index 1fe41f3..06d179e 100644 --- a/featuremodel/feature-modelconverter/src/test/resources/repoinit.json +++ b/featuremodel/feature-modelconverter/src/test/resources/repoinit.json @@ -1,7 +1,7 @@ { "#": "this is a comment", - "id": "org.apache.sling.simple/repoinit/1.0.0", + "id": "generated/repoinit/1.0.0", "bundles": [ { "id": "org.apache.sling/org.apache.sling.repoinit.parser/1.2.0", -- To stop receiving notification emails like this one, please contact dav...@apache.org.