This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
forked from jenkinsci/xml-job-to-job-dsl-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #58 from tinyspeck/BUILD-3103_envInject_default
BUILD-3103 add required properties with default values for EnvInjectBuildWrapper
- Loading branch information
Showing
2 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/com/adq/jenkins/xmljobtodsl/dsl/strategies/custom/DSLEnvInjectStrategy.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,77 @@ | ||
package com.adq.jenkins.xmljobtodsl.dsl.strategies.custom; | ||
|
||
import com.adq.jenkins.xmljobtodsl.dsl.strategies.DSLObjectStrategy; | ||
import com.adq.jenkins.xmljobtodsl.parsers.PropertyDescriptor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class DSLEnvInjectStrategy extends DSLObjectStrategy { | ||
private final String name; | ||
|
||
private HashMap<String, String> expectedProperties = new HashMap<String, String>(){{ | ||
put("propertiesFilePath", ""); | ||
put("scriptFilePath", ""); | ||
put("scriptContent", ""); | ||
put("loadFilesFromMaster", "false"); | ||
put("secureGroovyScript", ""); | ||
}}; | ||
|
||
public DSLEnvInjectStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name) { | ||
this(tabs, propertyDescriptor, name, true); | ||
addMissingProperties(propertyDescriptor); | ||
} | ||
|
||
public DSLEnvInjectStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name, boolean shouldInitChildren) { | ||
super(tabs, propertyDescriptor, name, shouldInitChildren); | ||
this.name = name; | ||
addMissingProperties(propertyDescriptor); | ||
} | ||
|
||
private void addMissingProperties(PropertyDescriptor propertyDescriptor){ | ||
// Make a copy of the properties that we can iterate over and remove elements as we go. | ||
List<PropertyDescriptor> properties = new ArrayList<>(); | ||
properties.addAll(propertyDescriptor.getProperties()); | ||
|
||
for (HashMap.Entry<String, String> expectedProp : expectedProperties.entrySet()) { | ||
boolean foundProperty = false; | ||
int indexToRemove = 0; | ||
|
||
for(PropertyDescriptor existingProp : properties){ | ||
if(existingProp.getName() == expectedProp.getKey()){ | ||
foundProperty = true; | ||
indexToRemove = properties.indexOf(existingProp); | ||
break; | ||
} | ||
} | ||
|
||
if(foundProperty){ | ||
properties.remove(indexToRemove); | ||
} else { | ||
PropertyDescriptor newChild = new PropertyDescriptor( | ||
expectedProp.getKey(), | ||
propertyDescriptor.getParent(), | ||
expectedProp.getValue(), | ||
null | ||
); | ||
|
||
propertyDescriptor.getProperties().add(newChild); | ||
|
||
} | ||
|
||
initChildren(propertyDescriptor); | ||
} | ||
} | ||
|
||
@Override | ||
public String toDSL() { | ||
String childrenDSL = getChildrenDSL(); | ||
|
||
if (name != null) { | ||
return replaceTabs(String.format(getSyntax("syntax.object_with_name"), name, childrenDSL), getTabs()); | ||
} else { | ||
return replaceTabs(String.format(getSyntax("syntax.object"), childrenDSL), getTabs()); | ||
} | ||
} | ||
} |
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