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-org-apache-sling-feature-modelconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new 63c5e4a  Include original file name in feature ID if different from 
feature name
63c5e4a is described below

commit 63c5e4a84904fe23512638c53a99a11607518e47
Author: David Bosschaert <[email protected]>
AuthorDate: Fri May 4 09:19:39 2018 +0100

    Include original file name in feature ID if different from feature name
---
 .../modelconverter/ProvisioningToFeature.java      | 35 ++++++++++++++--------
 .../feature/modelconverter/ModelConverterTest.java | 10 +++++++
 src/test/resources/simple2.json                    | 12 ++++++++
 src/test/resources/simple2.txt                     | 24 +++++++++++++++
 4 files changed, 69 insertions(+), 12 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/modelconverter/ProvisioningToFeature.java
 
b/src/main/java/org/apache/sling/feature/modelconverter/ProvisioningToFeature.java
index fd2766d..8f404e5 100644
--- 
a/src/main/java/org/apache/sling/feature/modelconverter/ProvisioningToFeature.java
+++ 
b/src/main/java/org/apache/sling/feature/modelconverter/ProvisioningToFeature.java
@@ -70,17 +70,13 @@ public class ProvisioningToFeature {
 
     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, options);
 
-        String bareFileName = file.getName();
-        int idx = bareFileName.lastIndexOf('.');
-        if (idx > 0) {
-            bareFileName = bareFileName.substring(0, idx);
-        }
+        String bareFileName = getBareFileName(file);
+        final List<org.apache.sling.feature.Feature> features = 
buildFeatures(model, bareFileName, options);
 
         List<File> files = new ArrayList<>();
         for (org.apache.sling.feature.Feature f : features) {
-            String id = f.getVariables().get("provisioning.model.name");
+            String id = 
f.getVariables().get(FeatureToProvisioning.PROVISIONING_MODEL_NAME_VARIABLE);
             if (id == null) {
                 id = f.getId().getArtifactId();
             }
@@ -108,7 +104,7 @@ public class ProvisioningToFeature {
 
             writeApplication(app, outputFile);
         } else {
-            final List<org.apache.sling.feature.Feature> features = 
buildFeatures(model, Collections.emptyMap());
+            final List<org.apache.sling.feature.Feature> features = 
buildFeatures(model, null, Collections.emptyMap());
             int index = 1;
             for(final org.apache.sling.feature.Feature feature : features) {
                 writeFeature(feature, outputFile, features.size() > 1 ? index 
: 0);
@@ -331,9 +327,6 @@ public class ProvisioningToFeature {
             Entry<String, String> entry = it.next();
             variables.put(entry.getKey(), entry.getValue());
         }
-        if (feature.getName().startsWith(":")) {
-            
variables.put(FeatureToProvisioning.PROVISIONING_MODEL_NAME_VARIABLE, 
feature.getName());
-        }
 
         Extension cpExtension = 
extensions.getByName(FeatureConstants.EXTENSION_NAME_CONTENT_PACKAGES);
         for(final RunMode runMode : feature.getRunModes() ) {
@@ -448,7 +441,7 @@ public class ProvisioningToFeature {
     }
 
 
-    private static List<org.apache.sling.feature.Feature> buildFeatures(Model 
model, Map<String, Object> options) {
+    private static List<org.apache.sling.feature.Feature> buildFeatures(Model 
model, String bareFileName, Map<String, Object> options) {
         final List<org.apache.sling.feature.Feature> features = new 
ArrayList<>();
 
         String groupId = getOption(options, "groupId", "generated");
@@ -459,6 +452,11 @@ public class ProvisioningToFeature {
             String name = feature.getName();
             if ( name != null ) {
                 name = name.replaceAll("[:]", "");
+
+                if (!name.equals(bareFileName)) {
+                    name = bareFileName + "_" + name;
+                }
+
                 if ( feature.getVersion() != null ) {
                     idString = groupId + "/" + name + "/" + 
feature.getVersion();
                 } else {
@@ -471,11 +469,24 @@ public class ProvisioningToFeature {
             features.add(f);
 
             buildFromFeature(feature, f.getVariables(), f.getBundles(), 
f.getConfigurations(), f.getExtensions(), f.getFrameworkProperties());
+
+            if (!f.getId().getArtifactId().equals(feature.getName())) {
+                
f.getVariables().put(FeatureToProvisioning.PROVISIONING_MODEL_NAME_VARIABLE, 
feature.getName());
+            }
         }
 
         return features;
     }
 
+    private static String getBareFileName(File file) {
+        String bareFileName = file.getName();
+        int idx = bareFileName.lastIndexOf('.');
+        if (idx > 0) {
+            bareFileName = bareFileName.substring(0, idx);
+        }
+        return bareFileName;
+    }
+
     @SuppressWarnings("unchecked")
     private static <T> T getOption(Map<String, Object> options, String name, T 
defaultValue) {
         if (options.containsKey(name)) {
diff --git 
a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java 
b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
index 68a9848..7938f23 100644
--- 
a/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
+++ 
b/src/test/java/org/apache/sling/feature/modelconverter/ModelConverterTest.java
@@ -146,6 +146,16 @@ public class ModelConverterTest {
     }
 
     @Test
+    public void testSimple2ToProvModel() throws Exception {
+        testConvertToProvisioningModel("/simple2.json", "/simple2.txt");
+    }
+
+    @Test
+    public void testSimple2ToFeature() throws Exception {
+        testConvertToFeature("/simple2.txt", "/simple2.json");
+    }
+
+    @Test
     public void testProvModelRoundtripFolder() throws Exception {
         String dir = System.getProperty("test.prov.files.dir");
         File filesDir;
diff --git a/src/test/resources/simple2.json b/src/test/resources/simple2.json
new file mode 100644
index 0000000..d324a3f
--- /dev/null
+++ b/src/test/resources/simple2.json
@@ -0,0 +1,12 @@
+{
+  "id":"generated:simple2_something:1.0.0",
+  "variables":{
+    "provisioning.model.name":"something"
+  },
+  "bundles":[
+    {
+      "id":"org.apache.aries:org.apache.aries.util:1.1.3",
+      "start-level":"20"
+    }
+  ]
+}
diff --git a/src/test/resources/simple2.txt b/src/test/resources/simple2.txt
new file mode 100644
index 0000000..be7771e
--- /dev/null
+++ b/src/test/resources/simple2.txt
@@ -0,0 +1,24 @@
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+# The :boot feature contains all things to bootstrap the installation.
+#
+[feature name=something]
+
+[artifacts]
+    org.apache.aries/org.apache.aries.util/1.1.3

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to