Skip to content

Instantly share code, notes, and snippets.

@robertlemke
Created September 25, 2014 12:28
Show Gist options
  • Save robertlemke/bf99059381a155fb624a to your computer and use it in GitHub Desktop.
Save robertlemke/bf99059381a155fb624a to your computer and use it in GitHub Desktop.
Examples for inline JavaScript in ant
<?xml version="1.0"?>
<project name="Foo_Functions">
<!--
toLowerCase
Converts the given subject into a lower case string and puts the result into the specified user property.
Attributes:
subject The string to convert
targetPropertyName Name of the property to store the result in
-->
<scriptdef name="toLowerCase" language="javascript">
<attribute name="subject"/>
<attribute name="targetPropertyName"/>
var subject = attributes.get('subject');
project.setUserProperty(attributes.get('targetpropertyname'), subject.toLowerCase());
</scriptdef>
<!--
replace
Replaces occurrences of "pattern" in the given subject by "replacement" and puts the result into the specified
user property.
Attributes:
subject The original string
pattern The string to find, may be a regular expression pattern
replacement The replacement for occurrences of pattern
targetPropertyName Name of the property to store the result in
-->
<scriptdef name="replace" language="javascript">
<attribute name="subject"/>
<attribute name="pattern"/>
<attribute name="replacement"/>
<attribute name="targetPropertyName"/>
var subject = attributes.get('subject');
project.setUserProperty(attributes.get('targetpropertyname'), subject.replace(attributes.get('pattern'), attributes.get('replacement')));
</scriptdef>
<!--
jsonParse
Parses the given JSON string and puts the result into properties
Attributes:
json The JSON string
targetPropertyPrefix A prefix for the resulting target properties, eg. "foo."
-->
<scriptdef name="jsonParse" language="javascript">
<attribute name="json"/>
<attribute name="targetPropertyPrefix"/>
var obj = eval("(function(){return " + attributes.get('json') + ";})()");
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
project.setUserProperty(attributes.get('targetpropertyprefix') + key, obj[key]);
}
}
</scriptdef>
</project>
@robertlemke
Copy link
Author

Just call your functions likes this:

<toLowerCase subject="RobertLemke.com" targetPropertyName="foo"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment