Skip to content

Commit

Permalink
support wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-confino committed Nov 15, 2018
1 parent 99c737c commit 7e0ebe0
Showing 1 changed file with 36 additions and 6 deletions.
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

Expand Down Expand Up @@ -135,7 +136,7 @@ public FeatureReplacementAction addFeatures(Set<String> addFeatures) {
*
* ...to be clear, this is not the opposite of addFeatures()
*
* @param removeFeatures the features to be removed
* @param removeFeatures the features to be removed. Wildcards are supported.
* @return this
*/
public FeatureReplacementAction removeFeatures(Set<String> removeFeatures) {
Expand All @@ -159,7 +160,7 @@ public FeatureReplacementAction addFeature(String addFeature) {
*
* ...to be clear, this is not the opposite of addFeature()
*
* @param removeFeature the feature to be removed
* @param removeFeature the feature to be removed. Wildcards are supported.
* @return this
*/
public FeatureReplacementAction removeFeature(String removeFeature) {
Expand Down Expand Up @@ -308,16 +309,31 @@ public void setup() throws Exception {
Log.info(c, m, "Original features: " + features);
if (forceAddFeatures) {
features.removeAll(removeFeatures);
//remove any wildcard features, before adding the new feature.
for (String removeFeature : removeFeatures) {
if (removeFeature.endsWith("*")) {
removeWildcardFeature(features, removeFeature);
}
}
features.addAll(addFeatures);
} else {
for (String removeFeature : removeFeatures)
if (features.remove(removeFeature)) {
// If we found a feature to remove that is actually present in config file, then
// remove it and replace it with the corresponding feature
for (String removeFeature : removeFeatures) {
boolean removed = false;
if (removeFeature.endsWith("*")) {
removed = removeWildcardFeature(features, removeFeature);
}
else if (features.remove(removeFeature)) {
removed = true;
}

// If we found a feature to remove that is actually present in config file, then
// replace it with the corresponding feature
if (removed) {
String toAdd = getReplacementFeature(removeFeature, addFeatures);
if (toAdd != null)
features.add(toAdd);
}
}
}
Log.info(c, m, "Resulting features: " + features);

Expand Down Expand Up @@ -347,6 +363,20 @@ private static String getReplacementFeature(String originalFeature, Set<String>
return null;
}

private static boolean removeWildcardFeature(Set<String> features, String removeFeature) {
String matcher = removeFeature.substring(0, removeFeature.length() - 1);
boolean removed = false;
Iterator<String> iterator = features.iterator();
while (iterator.hasNext()) {
String feature = iterator.next();
if (feature.startsWith(matcher)) {
iterator.remove();
removed = true;
}
}
return removed;
}

private static Set<File> findFile(File dir, String suffix) {
HashSet<File> set = new HashSet<File>();
File[] list = dir.listFiles();
Expand Down

0 comments on commit 7e0ebe0

Please sign in to comment.