forked from e-gineering/gitflow-helper-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request e-gineering#133 from DEsoftware/set-properties-set…
…s-git-info New goal set-git-properties
- Loading branch information
Showing
4 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/com/e_gineering/maven/gitflowhelper/PropertyMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.e_gineering.maven.gitflowhelper; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.shared.utils.StringUtils; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Helper for mapping the GIT branch name | ||
*/ | ||
public class PropertyMapper | ||
{ | ||
private String propertyName; | ||
private String language = "java"; | ||
private String mapper; | ||
|
||
public String getLanguage() | ||
{ | ||
return language; | ||
} | ||
|
||
public void setLanguage(final String language) | ||
{ | ||
this.language = language; | ||
} | ||
|
||
public String getPropertyName() | ||
{ | ||
return propertyName; | ||
} | ||
|
||
public void setPropertyName(final String propertyName) | ||
{ | ||
this.propertyName = propertyName; | ||
} | ||
|
||
public String getMapper() | ||
{ | ||
return mapper; | ||
} | ||
|
||
public void setMapper(final String mapper) | ||
{ | ||
this.mapper = mapper; | ||
} | ||
|
||
/** | ||
* Maps the Git branch name | ||
* @param gitBranchInfo the Git Info | ||
* @return the mapped branch name | ||
* @throws MojoExecutionException on error | ||
*/ | ||
String map(GitBranchInfo gitBranchInfo) throws MojoExecutionException | ||
{ | ||
if(StringUtils.isBlank(getPropertyName())) { | ||
throw new MojoExecutionException("Mapper has not propertyName"); | ||
} | ||
if(StringUtils.isBlank(getLanguage())) { | ||
throw new MojoExecutionException("Mapper for property [" + getPropertyName() + "] has no language"); | ||
} | ||
if(StringUtils.isBlank(getMapper())) { | ||
throw new MojoExecutionException("Mapper for property [" + getPropertyName() + "] has no mapper"); | ||
} | ||
if("java".equalsIgnoreCase(language)) { | ||
|
||
Class<?> mapperClass; | ||
try | ||
{ | ||
mapperClass = Class.forName(mapper.trim()); | ||
} | ||
catch(ClassNotFoundException e) | ||
{ | ||
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]: class " + mapper + " not found", e); | ||
} | ||
|
||
if(!BiFunction.class.isAssignableFrom(mapperClass)) { | ||
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]: class " + mapper + " does not implement " + BiFunction.class.getName()); | ||
} | ||
|
||
try | ||
{ | ||
@SuppressWarnings("unchecked") | ||
BiFunction<String,String,String> function = (BiFunction<String,String,String>)Class.forName(mapper).newInstance(); | ||
return function.apply(gitBranchInfo.getName(), gitBranchInfo.getType().name()); | ||
} | ||
catch(Exception e) | ||
{ | ||
throw new MojoExecutionException("Error in mapper for property [" + getPropertyName() + "]", e); | ||
} | ||
} | ||
ScriptEngineManager mgr = new ScriptEngineManager(); | ||
ScriptEngine engine = mgr.getEngineByName(language); | ||
if(engine == null) { | ||
throw new MojoExecutionException("Property mapper for property [" + getPropertyName() + "] has an unsupported language [" + language + "]"); | ||
} | ||
try | ||
{ | ||
engine.eval(mapper); | ||
|
||
Invocable inv = (Invocable) engine; | ||
|
||
Object ret = inv.invokeFunction("map", gitBranchInfo.getName(), gitBranchInfo.getType()); | ||
if(ret == null) { | ||
return null; | ||
} | ||
return String.valueOf(ret); | ||
} | ||
catch(Exception e) | ||
{ | ||
throw new MojoExecutionException("Error in property mapper for property [" + getPropertyName()+ "]", e); | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/e_gineering/maven/gitflowhelper/SetGitPropertiesMojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.e_gineering.maven.gitflowhelper; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.apache.maven.plugins.annotations.LifecyclePhase; | ||
import org.apache.maven.plugins.annotations.Mojo; | ||
import org.apache.maven.plugins.annotations.Parameter; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
||
/** | ||
* Stores the branch type and the git branch in Maven properties | ||
*/ | ||
@Mojo(name = "set-git-properties", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) | ||
public class SetGitPropertiesMojo extends AbstractGitflowBranchMojo | ||
{ | ||
/** | ||
* Defines the name of the property where the branch type is stored | ||
*/ | ||
@Parameter(property = "branchTypeProperty", defaultValue = "branchType") | ||
private String branchTypeProperty = "branchType"; | ||
|
||
/** | ||
* Defines the name of the property where the Git branch name is stored. | ||
*/ | ||
@Parameter(property = "branchNameProperty", defaultValue = "gitBranchName") | ||
private String branchNameProperty = "branchName"; | ||
|
||
/** | ||
* Der branchNamePropertyMapper allows to store the Git branch name | ||
* into additional properties.<br> | ||
* The branchName can be mapped by a java class, or an JSR223 scripting language | ||
*/ | ||
@Parameter(property = "branchNamePropertyMappers") | ||
private PropertyMapper[] branchNamePropertyMappers; | ||
|
||
@Override | ||
protected void execute(final GitBranchInfo gitBranchInfo) throws MojoExecutionException, MojoFailureException | ||
{ | ||
if(!StringUtils.isBlank(branchTypeProperty)) { | ||
project.getProperties().setProperty(branchTypeProperty, gitBranchInfo.getType().name()); | ||
} | ||
if(!StringUtils.isBlank(branchNameProperty)) { | ||
project.getProperties().setProperty(branchNameProperty, gitBranchInfo.getName()); | ||
} | ||
|
||
if(branchNamePropertyMappers != null) { | ||
for(PropertyMapper pm : branchNamePropertyMappers) | ||
{ | ||
String mappedValue = pm.map(gitBranchInfo); | ||
getLog().info("Mapper [" + pm.getPropertyName() + "] mapped Git branch name [" + gitBranchInfo.getName() + "] to [" + mappedValue + "]"); | ||
if(StringUtils.isBlank(mappedValue)) { | ||
getLog().warn("Mapper for property [" + pm.getPropertyName() + "] did not provide a value, ignoring it"); | ||
continue; | ||
} | ||
project.getProperties().setProperty(pm.getPropertyName(), mappedValue); | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/com/e_gineering/maven/gitflowhelper/TestPropertyMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.e_gineering.maven.gitflowhelper; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public class TestPropertyMapper | ||
{ | ||
/** | ||
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Java Mapper | ||
* @throws MojoExecutionException on error | ||
*/ | ||
@Test | ||
public void testPropertyMapperWithJava() throws MojoExecutionException | ||
{ | ||
PropertyMapper mapper = new PropertyMapper(); | ||
mapper.setPropertyName("prop"); | ||
mapper.setLanguage("java"); | ||
mapper.setMapper(ToLowerCaseMapper.class.getName()); | ||
|
||
GitBranchInfo info = new GitBranchInfo("FOO", GitBranchType.DEVELOPMENT, "bar"); | ||
|
||
Assert.assertEquals("foo", mapper.map(info)); | ||
} | ||
|
||
/** | ||
* Test the methode {@link PropertyMapper#map(GitBranchInfo)} with a Javascript Mapper | ||
* @throws MojoExecutionException on error | ||
*/ | ||
@Test | ||
public void testPropertyMapperWithJavascript() throws MojoExecutionException | ||
{ | ||
PropertyMapper mapper = new PropertyMapper(); | ||
mapper.setPropertyName("prop"); | ||
mapper.setLanguage("javascript"); | ||
mapper.setMapper("" | ||
+ "function map(branchName, branchType) {\n" | ||
+" return branchName.toLowerCase();\n" | ||
+ "}" | ||
); | ||
|
||
GitBranchInfo info = new GitBranchInfo("FOO", GitBranchType.DEVELOPMENT, "bar"); | ||
|
||
Assert.assertEquals("foo", mapper.map(info)); | ||
|
||
} | ||
|
||
public static class ToLowerCaseMapper implements BiFunction<String,String,String> | ||
{ | ||
@Override | ||
public String apply(String branchName, String branchType) | ||
{ | ||
return branchName.toLowerCase(); | ||
} | ||
} | ||
} |