Skip to content

Commit

Permalink
Replacing SimpleTemplateEngine with basic string replace to improve p…
Browse files Browse the repository at this point in the history
…erformance in mergeProperties (#86)

* Replacing SimpleTemplateEngine with basic string replace to improve performance in mergeProperties

* Addressing PR comments by removing multiple replacements and throwing exceptions
pvelez12 authored and uschi2000 committed Aug 17, 2016
1 parent b2834fe commit 4902b28
Showing 1 changed file with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -27,23 +27,43 @@ import org.gradle.plugins.ide.eclipse.EclipsePlugin
class BaselineEclipse extends AbstractBaselinePlugin {

/**
* Copies all name/value pairs from {@code from} to {@code into}, replacing variable names (e.g., $var or ${var})
* occurring in values via a {@link SimpleTemplateEngine} and the given binding.
* Copies all name/value pairs from {@code from} to {@code into}, replacing variable names (e.g., $var or ${var}) occurring in values using basic string replacement
*/
static def mergeProperties(Properties from, Properties into, Map binding) {
from.stringPropertyNames().each { property ->
def propertyValue = from.getProperty(property)
try {
// Not all properties are well-formed; attempt to parse and fall-back to original value.
// Sadly, this is outrageously slow.
def template = new SimpleTemplateEngine().createTemplate(from.getProperty(property))
propertyValue = template.make(binding).toString()
} catch (Exception e) {
propertyValue = from.getProperty(property)
def propertyValue = replace(from.getProperty(property), binding)
into.setProperty(property, propertyValue)
}
catch(Exception e) {
e.printStackTrace()
}
}
}

static String replace(String str, Map binding) {
StringBuilder sb = new StringBuilder()
int start = 0

int begin = str.indexOf("\${", start)
if (begin < 0) {
sb.append(str.substring(start))
return sb.toString()
}

int end = str.indexOf('}', begin)
if (end < 0) {
throw new Exception("Missing '}' for property: " + str)
}

into.setProperty(property, propertyValue)
String replacement = binding.get(str.substring(begin + 2, end))
if (replacement != null) {
sb.append(str.substring(start, begin))
sb.append(replacement)
} else {
throw new Exception("Replacement missing from binding for property: " + str)
}
return sb.toString()
}

void apply(Project project) {

0 comments on commit 4902b28

Please sign in to comment.