Phing: skeleton files

25/01/2010

I use various build scripts and environments while developing, but many parts of the project stay the same.

One thing that always changes on a new build though, is configuration.

The easisest way to manage configuration is having all of the properties of an application saved in a file, and then have your build script apply those settings to any skeleton file found within the project. You might call this in typo3 terms, marker replacement.

Since my ExportProperties task has been included in Phing 2.4, it’s now a lot easier to do this using Phing, ExportProperties, Copy task and the ExpandProperties filter

<?xml version="1.0" encoding="UTF-8"?>
<project name="example" basedir="." default="skel">
    <!-- This is where we store the configured properties -->
    <property name="paths.config.phingProperties" value="${paths.config}/phing.properties" />
 
    <!-- Check if we already have properties -->
    <available file="${paths.config.phingProperties}" property="hasPhingProperties" />
 
    <target name="config">
        <if>
         <equals arg1="${hasPhingProperties}" arg2="true" />
         <then>
           <echo message="Reconfigure application..." />
         </then>
         <else>
           <echo message="Configure application ..." />
         </else>
        </if>
 
        <propertyprompt propertyName="db.host" defaultValue="localhost" promptText="Mysql Server Host" />
        <propertyprompt propertyName="db.username" promptText="Mysql Username" />
        <propertyprompt propertyName="db.password" promptText="Mysql Password" />
        <propertyprompt propertyName="db.name" defaultValue="${db.username}" promptText="Mysql Database name" />
 
        <!-- Export the properties -->
        <exportproperties targetFile="${paths.config.phingProperties}" />
    </target>
 
    <target name="loadProperties">
        <if>
         <equals arg1="${hasPhingProperties}" arg2="true" />
         <then>
 
         </then>
         <else>
            <echo message="Missing config file ..." />
            <phingcall target="config" />
         </else>
        </if>
 
        <property file="${paths.config.phingProperties}"  />
    </target>
 
    <!-- Copies every file ending in .skel found in the current directory and all subdirectories to the same filename excluding .skel
    Also replaces property markers with actual values -->
    <target name="skel" depends="loadProperties">
        <echo msg="Skel files..." />
 
        <copy todir="" overwrite="true">
            <mapper type="glob" from="*.skel" to="*"/>
            <filterchain>
                <expandproperties />
            </filterchain>
 
            <fileset dir=".">
                <include name="**/*.skel" />
            </fileset>
        </copy>
    </target>
</project>

And then in config/env.ini.skel:

[production]
; Doctrine settings
resources.doctrine.connection_string = "mysql://${db.username}:${db.password}@${db.host}/${db.name};unix_socket=/var/run/mysqld/mysqld.sock"

There are 3 comments in this article:

  1. 7/02/2010Cristian say:

    I get ” [wrapped: Could not instantiate class ExportPropertiesTask, even though a class was specified.” even though i’m using phing 2.4
    Any idea if i’m missing something?
    Also, i’ve trying to read the docs and some tutorials on how to use phing but the docs are a bit scarce and articles such as yours, although helpful, assume I’m familiar with some stuff, which I’m not :)
    What should i do ? Would it help to read the docs for Ant?
    For example
    The strings in name and value, where do they come from? What is paths? Or config?
    What do I need to put in my config files in order for phing to be able to replace with the values it prompts for?
    Thank you.

  2. 7/02/2010Cristian say:

    The tag i posted above gets filtered (figures :) )
    [property name="paths.config.phingProperties" value="${paths.config}/phing.properties" /]

  3. 9/02/2010Andrei say:

    Even though the ExportProperties task was included in 2.4, it was included with a naming problem (the class was called ExportProperties instead of ExportPropertiesTask).

    You can fix this by either manually changing the class name to ExportPropertiesTask (it’s normally under PEAR/phing/tasks/ext/ExportPropertiesTask.php), or checking out Phing from SVN.

    Regarding the strings like “paths.config.phingProperties”, they are just a naming convention I use, they are not defined anywhere else apart from the build file (look at line 4 in the above build file).

Write a comment: