You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/04 12:32:25 UTC
[sling-whiteboard] branch master updated: Use model name for
feature file name when converting to feature.
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 31d725c Use model name for feature file name when converting to feature.
31d725c is described below
commit 31d725cba007366c9bf82a7dad335bc78c15b614
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Wed Apr 4 13:30:55 2018 +0100
Use model name for feature file name when converting to feature.
---
.../modelconverter/impl/ProvisioningToFeature.java | 106 +++++----------------
.../modelconverter/impl/ModelConverterTest.java | 22 ++---
2 files changed, 30 insertions(+), 98 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 5f87e1f..4693ba8 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,16 +68,27 @@ import java.util.Set;
public class ProvisioningToFeature {
private static Logger LOGGER = LoggerFactory.getLogger(ProvisioningToFeature.class);
- public static void convert(File file, String output) {
+ public static List<File> convert(File file, File outDir) {
Model model = createModel(Collections.singletonList(file), null, true, false);
final List<org.apache.sling.feature.Feature> features = buildFeatures(model);
- if (features.size() != 1) {
- for (int i=0; i<features.size(); i++) {
- writeFeature(features.get(i), output, i+1);
+
+ List<File> files = new ArrayList<>();
+ for (org.apache.sling.feature.Feature f : features) {
+ String id = f.getVariables().get("provisioning.model.name");
+ if (id == null) {
+ id = f.getId().getArtifactId();
}
- } else {
- writeFeature(features.get(0), output, 0);
+
+ File outFile = new File(outDir, id + ".json");
+ int counter = 0;
+ while (outFile.exists()) {
+ outFile = new File(outDir, id + "_" + (++counter) + ".json");
+ }
+
+ files.add(outFile);
+ writeFeature(f, outFile.getAbsolutePath(), 0);
}
+ return files;
}
public static void convert(List<File> files, String outputFile, String runModes, boolean createApp,
@@ -148,8 +159,6 @@ public class ProvisioningToFeature {
modes = calculateRunModes(effectiveModel, runModes);
}
- // removeInactiveFeaturesAndRunModes(effectiveModel, modes);
-
return effectiveModel;
}
@@ -237,81 +246,6 @@ public class ProvisioningToFeature {
}
}
- // TODO is this needed for something?
- private static void removeInactiveFeaturesAndRunModes(final Model m,
- final Set<String> activeRunModes) {
- final String[] requiredFeatures = new String[] {ModelConstants.FEATURE_LAUNCHPAD, ModelConstants.FEATURE_BOOT};
- // first pass:
- // - remove special features except boot required ones
- // - remove special run modes and inactive run modes
- // - remove special configurations (TODO)
- final Iterator<Feature> i = m.getFeatures().iterator();
- while ( i.hasNext() ) {
- final Feature feature = i.next();
- if ( feature.isSpecial() ) {
- boolean remove = true;
- if ( requiredFeatures != null ) {
- for(final String name : requiredFeatures) {
- if ( feature.getName().equals(name) ) {
- remove = false;
- break;
- }
- }
- }
- if ( remove ) {
- i.remove();
- continue;
- }
- }
- feature.setComment(null);
- final Iterator<RunMode> rmI = feature.getRunModes().iterator();
- while ( rmI.hasNext() ) {
- final RunMode rm = rmI.next();
- if ( rm.isActive(activeRunModes) || rm.isRunMode(ModelConstants.RUN_MODE_STANDALONE) ) {
- final Iterator<Configuration> cI = rm.getConfigurations().iterator();
- while ( cI.hasNext() ) {
- final Configuration config = cI.next();
- if ( config.isSpecial() ) {
- cI.remove();
- continue;
- }
- config.setComment(null);
- }
- } else {
- rmI.remove();
- continue;
- }
- }
- }
-
- // second pass: aggregate the settings and add them to the first required feature
- final Feature requiredFeature = m.getFeature(requiredFeatures[0]);
- if ( requiredFeature != null ) {
- for(final Feature f : m.getFeatures()) {
- if ( f.getName().equals(requiredFeature.getName()) ) {
- continue;
- }
- copyAndClearSettings(requiredFeature, f.getRunMode(new String[] {ModelConstants.RUN_MODE_STANDALONE}));
- copyAndClearSettings(requiredFeature, f.getRunMode());
- }
- }
- }
-
- private static void copyAndClearSettings(final Feature requiredFeature, final RunMode rm) {
- if ( rm != null && !rm.getSettings().isEmpty() ) {
- final RunMode requiredRunMode = requiredFeature.getOrCreateRunMode(null);
- final Set<String> keys = new HashSet<>();
- for(final Map.Entry<String, String> entry : rm.getSettings()) {
- requiredRunMode.getSettings().put(entry.getKey(), entry.getValue());
- keys.add(entry.getKey());
- }
-
- for(final String key : keys) {
- rm.getSettings().remove(key);
- }
- }
- }
-
private static Set<String> calculateRunModes(final Model model, final String runModes) {
final Set<String> modesSet = new HashSet<>();
@@ -530,8 +464,12 @@ public class ProvisioningToFeature {
}
LOGGER.info("to file {}", out);
-
final File file = new File(out);
+ while (file.exists()) {
+ LOGGER.error("Output file already exists: {}", file.getAbsolutePath());
+ System.exit(1);
+ }
+
try ( final FileWriter writer = new FileWriter(file)) {
FeatureJSONWriter.write(writer, f, WriteOption.OLD_STYLE_FACTORY_CONFIGS);
} catch ( final IOException ioe) {
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 72e0c08..4231489 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
@@ -161,19 +161,13 @@ public class ModelConverterTest {
public void testConvertFromProvModelRoundTrip(File orgProvModel) throws Exception {
System.out.println("*** Roundtrip converting: " + orgProvModel.getName());
- String genJSONPrefix = orgProvModel.getName() + ".json";
- String genTxtPrefix = orgProvModel.getName() + ".txt";
- String genSuffix = ".generated";
- File outJSONFile = new File(tempDir.toFile(), genJSONPrefix + genSuffix);
List<File> allGenerateProvisioningModelFiles = new ArrayList<>();
- ProvisioningToFeature.convert(orgProvModel, outJSONFile.getAbsolutePath());
+ List<File> generated = ProvisioningToFeature.convert(orgProvModel, tempDir.toFile());
- for (File f : tempDir.toFile().listFiles((p, n) -> n.startsWith(genJSONPrefix))) {
- String infix = f.getName().substring(genJSONPrefix.length(),
- f.getName().length() - genSuffix.length());
-
- File genFile = new File(tempDir.toFile(), genTxtPrefix + infix + genSuffix);
+ for (File f : generated) {
+ String baseName = f.getName().substring(0, f.getName().length() - ".json".length());
+ File genFile = new File(tempDir.toFile(), baseName + ".txt");
allGenerateProvisioningModelFiles.add(genFile);
FeatureToProvisioning.convert(f, genFile.getAbsolutePath(), artifactManager);
}
@@ -185,14 +179,14 @@ public class ModelConverterTest {
public void testConvertToFeature(String originalProvModel, String expectedJSON) throws Exception {
File inFile = new File(getClass().getResource(originalProvModel).toURI());
- File outFile = new File(tempDir.toFile(), expectedJSON + ".generated");
- String outPath = outFile.getAbsolutePath();
- ProvisioningToFeature.convert(inFile, outPath);
+ List<File> files = ProvisioningToFeature.convert(inFile, tempDir.toFile());
+ 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(outPath, artifactManager, SubstituteVariables.NONE);
+ org.apache.sling.feature.Feature actual = FeatureUtil.getFeature(outFile.getAbsolutePath(), artifactManager, SubstituteVariables.NONE);
assertFeaturesEqual(expected, actual);
}
--
To stop receiving notification emails like this one, please contact
davidb@apache.org.