Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#413: use maven's PluginParameterExpressionEvaluator to allow replacements with values that contain unresolved variables #444

Merged
merged 2 commits into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -386,9 +387,6 @@ public class GitCommitIdMojo extends AbstractMojo {
@Nonnull
private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log);

@Nonnull
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log);

@Override
public void execute() throws MojoExecutionException {
try {
Expand Down Expand Up @@ -490,6 +488,7 @@ public void execute() throws MojoExecutionException {

loadGitData(properties);
loadBuildData(properties);
PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log, new PluginParameterExpressionEvaluator(session));
propertiesReplacer.performReplacement(properties, replacementProperties);
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
Expand Down
79 changes: 48 additions & 31 deletions maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,76 @@

package pl.project13.maven.git;

import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import pl.project13.core.log.LoggerBridge;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.*;
import java.util.regex.Pattern;

public class PropertiesReplacer {
private final LoggerBridge log;
private final PluginParameterExpressionEvaluator expressionEvaluator;

public PropertiesReplacer(LoggerBridge log) {
public PropertiesReplacer(LoggerBridge log, PluginParameterExpressionEvaluator expressionEvaluator) {
this.log = log;
this.expressionEvaluator = expressionEvaluator;
}

/**
* @param properties all properties that are being generated by the plugin
* @param replacementProperties list of all replacement actions to perform
*/
public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
if ((replacementProperties != null) && (properties != null)) {
for (ReplacementProperty replacementProperty: replacementProperties) {
String propertyKey = replacementProperty.getProperty();
if (propertyKey == null) {
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String)entry.getKey();
String content = (String)entry.getValue();
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
propertiesToBeAdded.put(newPropertyKey, result);
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
entry.setValue(result);
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
}
}
properties.putAll(propertiesToBeAdded);
performReplacementOnAllGeneratedProperties(properties, replacementProperty);
} else {
String content = properties.getProperty(propertyKey);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
properties.setProperty(propertyKey, result);
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
}
performReplacementOnSingleProperty(properties, replacementProperty, propertyKey);
}
}
}
}

private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) {
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String)entry.getKey();
String content = (String)entry.getValue();
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
propertiesToBeAdded.put(newPropertyKey, result);
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
entry.setValue(result);
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
}
}
properties.putAll(propertiesToBeAdded);
}

private void performReplacementOnSingleProperty(Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
String content = properties.getProperty(propertyKey);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info("apply replace on property " + propertyKey + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
properties.setProperty(propertyKey, result);
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
}
}

private String performReplacement(ReplacementProperty replacementProperty, String content) {
String result = content;
String result = "";
try {
result = Optional.ofNullable(expressionEvaluator.evaluate(content)).map(x -> x.toString()).orElse("");
} catch (Exception e) {
log.error("Something went wrong performing the replacement.", e);
}
if (replacementProperty != null) {
result = performTransformationRules(replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
if (replacementProperty.isRegex()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -35,7 +36,10 @@
import java.util.Properties;

import static java.util.Arrays.asList;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(JUnitParamsRunner.class)
public class PropertiesReplacerTest {
Expand All @@ -46,8 +50,10 @@ public static Collection<?> useRegexReplacement() {
private PropertiesReplacer propertiesReplacer;

@Before
public void setUp() {
this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class));
public void setUp() throws Throwable {
PluginParameterExpressionEvaluator pluginParameterExpressionEvaluator = mock(PluginParameterExpressionEvaluator.class);
when(pluginParameterExpressionEvaluator.evaluate(anyString())).then(returnsFirstArg());
this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class), pluginParameterExpressionEvaluator);
}

@Test
Expand Down