|
haxeEmbed
Executes haxe as script within an ant buildfile.
The haxeEmbed TaskThis task allows you to embed haXe as script within your Ant build file. The code will be run within the neko environment (supplied with haXe). Ant properties can be embedded into the haXe code from Ant; and can be set from within the haXe code and passed back to Ant. It's a silly little hack, really, but is useful when Ant can't quite cut it as a scripting language - processing XML, doing calculations, mashing up strings. Feedback welcomed! As I said, it's a silly little hack, but I find it useful. For example: <target name="doSomeHaXe">
<haxeEmbed>
neko.Lib.println("Hello World!");
</haxeEmbed>
</target>will print "Hello World!" to the console when the build target doSomeHaxe is called. Note that you shouldn't try to create a haXe class or a function. What is between the haxeEmbed tags is essentially the body of a single function. What's really going onThe code between the haxeEmbed tags is transplanted into a templated haXe class as the body of a function, compiled and then run. ImportsAlthough this is the body of a function, haxeEmbed still allows you to use imports: <target name="doSomeHaXe">
<haxeEmbed>
import neko.Sys;
neko.Lib.println(Sys.getEnv("PATH"));
</haxeEmbed>
</target>Special CharactersBecause characters like <, > and & turn up a lot in code - but will break your XML - it's probably a good idea to include your haxeEmbed scripts in a CDATA element like so: <target name="doSomeHaXe">
<haxeEmbed><![CDATA[
neko.Lib.println(20>10);
]]></haxeEmbed>
</target>Getting Ant PropertiesYou can include Ant properties into your script in exactly the same way as with any other Ant task, except for one point - because we run the risk of accidentally putting in extra backslashes (when we're dealing with filenames like C:\Bucket), all backslashes are double-escaped. This should work transparently. For example: <target name="doSomeHaXe">
<property name="number" value="27"/>
<property name="fileName" value="C:\Bucket"/>
<haxeEmbed><![CDATA[
neko.Lib.println(${number}>10);
neko.Lib.println("My file is:${fileName}");
]]></haxeEmbed>
</target>Setting Ant PropertiesUse the command setAntProperty to do this, like so: <target name="doSomeHaXe">
<haxeEmbed><![CDATA[
setAntProperty("myNewProp","SomeValue");
]]></haxeEmbed>
<echo>Prop is: ${myNewProp}</echo>
</target>Classpaths and LibshaxeEmbed takes the same classpath and lib options as all standard haxe tasks, but you will need to wrap the actual script content in a script tag like so: <target name="doSomeHaXe">
<haxeEmbed>
<classpath>
<pathelement location="test/src"/>
<classpath>
<lib name="swhx"/>
<script><![CDATA[
setAntProperty("myNewProp","SomeValue");
]]></script>
</haxeEmbed>
<echo>Prop is: ${myNewProp}</echo>
</target>Limitations
|