Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #58 from tinyspeck/BUILD-3103_envInject_default
Browse files Browse the repository at this point in the history
BUILD-3103 add required properties with default values for EnvInjectBuildWrapper
  • Loading branch information
NatWoodson authored Dec 30, 2022
2 parents 1cb0b09 + a08c557 commit 58412f4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
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());
}
}
}
8 changes: 6 additions & 2 deletions src/main/resources/translator.properties
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ EnvInjectBuildWrapper = envInjectBuildWrapper
EnvInjectBuildWrapper.type = OBJECT

EnvInjectBuildWrapper.info = info
EnvInjectBuildWrapper.info.type = OBJECT
EnvInjectBuildWrapper.info.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLEnvInjectStrategy

propertiesContent = propertiesContent
EnvInjectPasswordWrapper = envInjectPasswordWrapper
Expand Down Expand Up @@ -858,6 +858,7 @@ secureGroovyScript = secureGroovyScript
secureGroovyScript.type = OBJECT

secureGroovyScript.script = script
secureGroovyScript.script.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLMandatoryStringStrategy
sandbox = sandbox

info = INNER
Expand Down Expand Up @@ -937,7 +938,10 @@ makeExecutable = makeExecutable

useWorkspaceAsHome = useWorkspaceAsHome

propertiesFilePath = propertiesFile
propertiesFilePath = propertiesFilePath
scriptFilePath = scriptFilePath
scriptContent = scriptContent


au.com.rayh.XCodeBuilder = it / 'builders' / 'au.com.rayh.XCodeBuilder'
au.com.rayh.XCodeBuilder.type = CONFIGURE
Expand Down

0 comments on commit 58412f4

Please sign in to comment.