Skip to content

Instantly share code, notes, and snippets.

@jasonhofer
Created March 1, 2012 18:07
Show Gist options
  • Save jasonhofer/1951747 to your computer and use it in GitHub Desktop.
Save jasonhofer/1951747 to your computer and use it in GitHub Desktop.
Apache Ant "php" macrodef for executing PHP code
<!--
@todo new attributes "include" and/or "require" which take csv file lists. useful for presetdefs
@todo new attribute "namespace" (useful?)
@todo use "return" instead of "echo", or allow both. maybe use attributes "returnProperty" and "outputProperty" instead of just "property"
@todo could (optionally) predefine some variables, such as $basedir
@todo could (optionally) use "echoProperties" to dump properties to a temp file and load them into a predefined $ant, $properties, or $p array
-->
<macrodef name="php" description="Executes PHP code">
<attribute name="code" default="" />
<attribute name="executable" default="php" />
<attribute name="property" default="" />
<attribute name="failOnError" default="false" />
<text name="text" optional="true" />
<sequential>
<local name="tmp.file" />
<local name="php.code" />
<local name="php.exec" />
<tempfile property="tmp.file" destDir="${basedir}" deleteOnExit="true" prefix=".ant-" suffix=".php" />
<condition property="php.code" value="@{code}" else="@{text}">
<equals arg1="" arg2="@{text}" trim="true" />
</condition>
<echo file="${tmp.file}" message="&lt;?php ${php.code};" />
<property name="php.exec" value="@{executable}" />
<script language="javascript"><![CDATA[
importClass(java.io.File);
var file = new File(project.getProperty("tmp.file"));
var foe = "@{failOnError}";
var prop = "@{property}";
var php = project.createTask("exec");
php.setTaskName("php");
php.setExecutable(project.getProperty("php.exec"));
if (foe.length > 0) php.setFailonerror(project.toBoolean(foe));
if (prop.length > 0) php.setOutputproperty(prop);
php.createArg().setValue("-f");
php.createArg().setFile(file);
php.execute();
]]></script>
</sequential>
</macrodef>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment