|
17 | 17 |
|
18 | 18 | package pl.project13.maven.git; |
19 | 19 |
|
| 20 | +import org.apache.maven.plugin.PluginParameterExpressionEvaluator; |
20 | 21 | import pl.project13.core.log.LoggerBridge; |
21 | 22 |
|
22 | | -import java.util.HashMap; |
23 | | -import java.util.List; |
24 | | -import java.util.Map; |
25 | | -import java.util.Properties; |
| 23 | +import java.util.*; |
26 | 24 | import java.util.regex.Pattern; |
27 | 25 |
|
28 | 26 | public class PropertiesReplacer { |
29 | 27 | private final LoggerBridge log; |
| 28 | + private final PluginParameterExpressionEvaluator expressionEvaluator; |
30 | 29 |
|
31 | | - public PropertiesReplacer(LoggerBridge log) { |
| 30 | + public PropertiesReplacer(LoggerBridge log, PluginParameterExpressionEvaluator expressionEvaluator) { |
32 | 31 | this.log = log; |
| 32 | + this.expressionEvaluator = expressionEvaluator; |
33 | 33 | } |
34 | 34 |
|
| 35 | + /** |
| 36 | + * @param properties all properties that are being generated by the plugin |
| 37 | + * @param replacementProperties list of all replacement actions to perform |
| 38 | + */ |
35 | 39 | public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) { |
36 | 40 | if ((replacementProperties != null) && (properties != null)) { |
37 | 41 | for (ReplacementProperty replacementProperty: replacementProperties) { |
38 | 42 | String propertyKey = replacementProperty.getProperty(); |
39 | 43 | if (propertyKey == null) { |
40 | | - Map<Object, Object> propertiesToBeAdded = new HashMap<>(); |
41 | | - for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
42 | | - String key = (String)entry.getKey(); |
43 | | - String content = (String)entry.getValue(); |
44 | | - String result = performReplacement(replacementProperty, content); |
45 | | - if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { |
46 | | - String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix(); |
47 | | - propertiesToBeAdded.put(newPropertyKey, result); |
48 | | - log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); |
49 | | - } else { |
50 | | - entry.setValue(result); |
51 | | - log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'"); |
52 | | - } |
53 | | - } |
54 | | - properties.putAll(propertiesToBeAdded); |
| 44 | + performReplacementOnAllGeneratedProperties(properties, replacementProperty); |
55 | 45 | } else { |
56 | | - String content = properties.getProperty(propertyKey); |
57 | | - String result = performReplacement(replacementProperty, content); |
58 | | - if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { |
59 | | - String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix(); |
60 | | - properties.setProperty(newPropertyKey, result); |
61 | | - log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); |
62 | | - } else { |
63 | | - properties.setProperty(propertyKey, result); |
64 | | - log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'"); |
65 | | - } |
| 46 | + performReplacementOnSingleProperty(properties, replacementProperty, propertyKey); |
66 | 47 | } |
67 | 48 | } |
68 | 49 | } |
69 | 50 | } |
70 | 51 |
|
| 52 | + private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) { |
| 53 | + Map<Object, Object> propertiesToBeAdded = new HashMap<>(); |
| 54 | + for (Map.Entry<Object, Object> entry : properties.entrySet()) { |
| 55 | + String key = (String)entry.getKey(); |
| 56 | + String content = (String)entry.getValue(); |
| 57 | + String result = performReplacement(replacementProperty, content); |
| 58 | + if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { |
| 59 | + String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix(); |
| 60 | + propertiesToBeAdded.put(newPropertyKey, result); |
| 61 | + log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); |
| 62 | + } else { |
| 63 | + entry.setValue(result); |
| 64 | + log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'"); |
| 65 | + } |
| 66 | + } |
| 67 | + properties.putAll(propertiesToBeAdded); |
| 68 | + } |
| 69 | + |
| 70 | + private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) { |
| 71 | + String content = properties.getProperty(propertyKey); |
| 72 | + String result = performReplacement(replacementProperty, content); |
| 73 | + if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) { |
| 74 | + String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix(); |
| 75 | + properties.setProperty(newPropertyKey, result); |
| 76 | + log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'"); |
| 77 | + } else { |
| 78 | + properties.setProperty(propertyKey, result); |
| 79 | + log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
71 | 83 | private String performReplacement(ReplacementProperty replacementProperty, String content) { |
72 | | - String result = content; |
| 84 | + String result = ""; |
| 85 | + try { |
| 86 | + result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse(""); |
| 87 | + } catch (Exception e) { |
| 88 | + log.error("Something went wrong performing the replacement.", e); |
| 89 | + } |
73 | 90 | if (replacementProperty != null) { |
74 | 91 | result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE); |
75 | 92 | if (replacementProperty.isRegex()) { |
|
0 commit comments