Getting Antsy with Phing
Earlier today, I set to work on an open source project...pulled down a copy from github, and started reading through the code.
And there I found a build.xml file.
At first I didn't think anything of it - plenty of my projects have ended up using Ant for setup, builds, CI and other myriad tasks.
However, when I tried to run ant, it complained: <echo msg="foo" /> is invalid syntax, it should be <echo message="foo" />. It took a while before I eventually twigged that this wasn't an ant build file, it was a Phing file.
Phing is a port of apache ant to work with PHP instead of Java. It's mostly pretty similar, with a few minor differences here and there - such as msg attributes instead of message.
I decided to simplify setup of the project by adding database build tasks. The project included an initial database export file, and a series of patch files which needed to be run sequentially (LornaJane has a great post on this kind of database patching strategy).
So my initial build code looked like this:
<target name="initialiseDB">
<echo msg="DB: Remove database..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};"
userid="${db.username}"
password="${db.password}">
DROP DATABASE IF EXISTS ${db.database};
</pdo>
<echo msg="DB: Create empty database..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};"
userid="${db.username}"
password="${db.password}">
CREATE DATABASE ${db.database};
</pdo>
<!-- import the init_db file -->
<echo msg="DB: Import database..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};dbname=${db.database}"
userid="${db.username}"
password="${db.password}">
<filelist dir="./doc/db" files="init_db.sql" />
</pdo>
<!-- process patch files -->
<echo msg="DB: Process patch files..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};dbname=${db.database}"
userid="${db.username}"
password="${db.password}">
<fileset dir="./doc/db">
<include name="patch*.sql" />
</fileset>
</pdo>
</target> <!-- process patch files -->
<echo msg="DB: Process patch files..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};dbname=${db.database}"
userid="${db.username}"
password="${db.password}">
<fileset dir="./doc/db">
<include name="patch*.sql" />
</fileset>
</pdo> <!-- process patch files -->
<echo msg="DB: Process patch files..." />
<pdo
url="${db.dbdriver}:host=${db.hostname};dbname=${db.database}"
userid="${db.username}"
password="${db.password}">
<filelist dir="doc/db" files="patch1.sql,patch2.sql,patch3.sql" />
</pdo><!-- set a Phing property called 'foo' to the value 'bar' -->
<php expression="'bar'" returnProperty="foo" />
<!-- set a Phing property called 'foo' to the value '7' -->
<php expression="3+4" returnProperty="foo" />
<!-- set a Phing property called 'foo' to the value 'bar,baz' -->
<php expression="implode(',', array('bar', 'baz'))" returnProperty="foo" /><php expression="implode(',', glob('doc/db/patch*.sql'))" returnProperty="db.patchfiles" />$list = glob('doc/db/patch*.sql'); natsort $list; $list = implode(',', $list);
<php expression="eval('$list=glob("doc/db/patch*.sql"); natsort ($list); return implode(",", $list);');" returnProperty="db.patchfiles" />

Comments
Jeremy (not verified)
Fri, 02/18/2011 - 16:41
Permalink
This post gave me some insights on how to solve a similar problem.
Thanks!
Cédric D (not verified)
Tue, 03/27/2012 - 08:21
Permalink
Great post, thanks dude!
Add new comment