<?xml version="1.0"?>

<!--
    Doctrine 2 build file.
-->

<project name="Doctrine2" default="build" basedir=".">
    <taskdef classname="phing.tasks.ext.d51PearPkg2Task" name="d51pearpkg2" />
    <taskdef classname="NativePhpunitTask" classpath="./tools/" name="nativephpunit" />

    <property file="build.properties" />
    
    <!-- 
        Fileset for artifacts shared across all distributed packages.
    -->
    <fileset id="shared-artifacts" dir=".">
        <include name="LICENSE"/>
        <include name="COPYRIGHT"/>
        <include name="CHANGELOG"/>
    </fileset>

    <!--
        Fileset for command line scripts
    -->
    <fileset id="bin-scripts" dir="./bin">
        <include name="doctrine"/>
        <include name="doctrine.php"/>
    </fileset>

    <!-- 
        Fileset for the sources of the Doctrine Common package.
    -->
    <fileset id="common-sources" dir="./lib">
        <include name="Doctrine/Common/**"/>
    </fileset>

    <!-- 
        Fileset for the sources of the Doctrine DBAL package.
    -->
    <fileset id="dbal-sources" dir="./lib">
        <include name="Doctrine/DBAL/**"/>
    </fileset>
    
    <!-- 
        Fileset for the sources of the Doctrine ORM package.
    -->
    <fileset id="orm-sources" dir="./lib">
        <include name="Doctrine/ORM/**"/>
    </fileset>
    
    <!-- 
        Fileset for the Doctrine ORM tools + sandbox.
    -->
    <fileset id="orm-tools" dir=".">
        <include name="tools/sandbox/Entities"/>
        <include name="tools/sandbox/xml"/>
        <include name="tools/sandbox/yaml"/>
        <include name="tools/sandbox/cli-config.php"/>
        <include name="tools/sandbox/config.php"/>
        <include name="tools/sandbox/doctrine"/>
        <include name="tools/sandbox/doctrine.php"/>
        <include name="tools/sandbox/index.php"/>
    </fileset>

    <target name="clean">
        <available file="./build.properties" property="build_properties_exist" value="true"/>
        <fail unless="build_properties_exist" message="The build.properties file is missing." />

        <delete dir="${build.dir}" includeemptydirs="true" />
        <delete dir="${dist.dir}" includeemptydirs="true" />
        <delete dir="${report.dir}" includeemptydirs="true" />
    </target>

    <target name="prepare" depends="clean">
        <echo msg="Creating build directory: ${build.dir}" />
        <mkdir dir="${build.dir}" />
        <echo msg="Creating distribution directory: ${dist.dir}" />
        <mkdir dir="${dist.dir}" />
        <echo msg="Creating report directory: ${report.dir}" />
        <mkdir dir="${report.dir}" />
        <mkdir dir="${build.dir}/logs"/>
        <mkdir dir="${report.dir}/tests"/>
    </target>

    <target name="build-common">
        <copy todir="${build.dir}/common">
            <fileset refid="shared-artifacts"/>
        </copy>
        <copy todir="${build.dir}/common/DoctrineCommon-${version}">
            <fileset refid="common-sources"/>
        </copy>
    </target>
    
    <target name="build-dbal">
        <copy todir="${build.dir}/dbal">
            <fileset refid="shared-artifacts"/>
        </copy>
        <copy todir="${build.dir}/dbal/DoctrineDBAL-${version}">
            <fileset refid="common-sources"/>
            <fileset refid="dbal-sources"/>
        </copy>
    </target>
    
    <!-- 
        Builds all packages, preparing them for distribution.
    -->
    <target name="build-orm" depends="test, build-common, build-dbal">
        <copy todir="${build.dir}/orm">
            <fileset refid="shared-artifacts"/>
            <fileset refid="orm-tools"/>
        </copy>
        <copy todir="${build.dir}/orm/DoctrineORM-${version}">
            <fileset refid="common-sources"/>
            <fileset refid="dbal-sources"/>
            <fileset refid="orm-sources"/>
        </copy>
        <copy todir="${build.dir}/orm/DoctrineORM-${version}/bin">
            <fileset refid="bin-scripts"/>
        </copy>
    </target>
    
    <target name="build" depends="test, build-orm"/>
    
    <!-- 
        Runs the full test suite.
    -->
    <target name="test" depends="prepare">
        <if><equals arg1="${test.phpunit_generate_coverage}" arg2="1" />
            <then>
                <property name="test.phpunit_coverage_file" value="${build.dir}/logs/clover.xml" />
            </then>
            <else>
                <property name="test.phpunit_coverage_file" value="false" />
            </else>
        </if>

        <nativephpunit
            testfile="./tests/Doctrine/Tests/AllTests.php" junitlogfile="${build.dir}/logs/testsuites.xml"
            testdirectory="./tests"  coverageclover="${test.phpunit_coverage_file}" configuration="${test.phpunit_configuration_file}"
        />
        <phpunitreport infile="${build.dir}/logs/testsuites.xml" format="frames" todir="${report.dir}/tests" />

        <nativephpunit testfile="./tests/Doctrine/Tests/ORM/Performance/AllTests.php" testdirectory="./tests" haltonfailure="false" haltonerror="false" />
        <tstamp/>
        <svnlastrevision svnpath="${svn.path}" workingcopy="." propertyname="svn.lastrevision"/>
        <copy file="${build.dir}/logs/testsuites.xml" tofile="${log.archive.dir}/${svn.lastrevision}/log.xml" overwrite="true"/>

        <if><equals arg1="${test.pmd_reports}" arg2="1" />
            <then>
                <exec command="${test.pdepend_exec} --jdepend-xml=${build.dir}/logs/jdepend.xml ./lib/Doctrine" />
                <exec command="${test.phpmd_exec} ./lib/Doctrine xml codesize --reportfile ${build.dir}/logs/phpmd.xml" />

                <copy file="${build.dir}/logs/jdepend.xml" tofile="${log.archive.dir}/${svn.lastrevision}/jdepend.xml" overwrite="true"/>
                <copy file="${build.dir}/logs/phpmd.xml" tofile="${log.archive.dir}/${svn.lastrevision}/phpmd.xml" overwrite="true"/>
            </then>
        </if>
    </target>

    <!-- 
        Builds distributable PEAR packages.
    -->
    <target name="build-packages" depends="build-orm">
        <d51pearpkg2 baseinstalldir="/" dir="${build.dir}/common/DoctrineCommon-${version}">
           <name>DoctrineCommon</name>
           <summary>Common Doctrine code</summary>
           <channel>pear.phpdoctrine.org</channel>
           <description>The Doctrine Common package contains shared code between the other packages.</description>
           <lead user="jwage" name="Jonathan H. Wage" email="jonwage@gmail.com" />
           <lead user="guilhermeblanco" name="Guilherme Blanco" email="guilhermeblanco@gmail.com" />
           <lead user="romanb" name="Roman Borschel" email="roman@code-factory.org" />
           <license>LGPL</license>
           <version release="${version}" api="${version}" />
           <stability release="${stability}" api="${stability}" />
           <notes>-</notes>
           <dependencies>
               <php minimum_version="5.3.0" />
               <pear minimum_version="1.6.0" recommended_version="1.6.1" />
           </dependencies>
        </d51pearpkg2>
        <tar destfile="${dist.dir}/DoctrineCommon-${version_name}.tgz" basedir="${build.dir}/common" compression="gzip" />

        <d51pearpkg2 baseinstalldir="/" dir="${build.dir}/dbal/DoctrineDBAL-${version}">
           <name>DoctrineDBAL</name>
           <summary>Doctrine Database Abstraction Layer</summary>
           <channel>pear.phpdoctrine.org</channel>
           <description>The Doctrine DBAL package is the database abstraction layer used to power the ORM package.</description>
           <lead user="jwage" name="Jonathan H. Wage" email="jonwage@gmail.com" />
           <lead user="guilhermeblanco" name="Guilherme Blanco" email="guilhermeblanco@gmail.com" />
           <lead user="romanb" name="Roman Borschel" email="roman@code-factory.org" />
           <license>LGPL</license>
           <version release="${version}" api="${version}" />
           <stability release="${stability}" api="${stability}" />
           <notes>-</notes>
           <dependencies>
               <php minimum_version="5.3.0" />
               <pear minimum_version="1.6.0" recommended_version="1.6.1" />
           </dependencies>
        </d51pearpkg2>
        <tar destfile="${dist.dir}/DoctrineDBAL-${version_name}.tgz" basedir="${build.dir}/dbal" compression="gzip" />

        <d51pearpkg2 baseinstalldir="/" dir="${build.dir}/orm/DoctrineORM-${version}">
           <name>DoctrineORM</name>
           <summary>Doctrine Object Relationl Mapper</summary>
           <channel>pear.phpdoctrine.org</channel>
           <description>The Doctrine ORM package is the primary package containing the object relational mapper.</description>
           <lead user="jwage" name="Jonathan H. Wage" email="jonwage@gmail.com" />
           <lead user="guilhermeblanco" name="Guilherme Blanco" email="guilhermeblanco@gmail.com" />
           <lead user="romanb" name="Roman Borschel" email="roman@code-factory.org" />
           <license>LGPL</license>
           <version release="${version}" api="${version}" />
           <stability release="${stability}" api="${stability}" />
           <notes>-</notes>
           <dependencies>
               <php minimum_version="5.3.0" />
               <pear minimum_version="1.6.0" recommended_version="1.6.1" />
           </dependencies>
           <dirroles key="bin">script</dirroles>
           <replacement path="bin/doctrine" type="pear-config" from="@php_bin@" to="php_bin" />
           <replacement path="bin/doctrine.php" type="pear-config" from="@php_bin@" to="php_bin" />
           <replacement path="bin/doctrine.php" type="pear-config" from="@bin_dir@" to="bin_dir" />
           <release>
                <install as="doctrine" name="bin/doctrine" />
                <install as="doctrine.php" name="bin/doctrine.php" />
           </release>
        </d51pearpkg2>
        <tar destfile="${dist.dir}/DoctrineORM-${version_name}.tgz" basedir="${build.dir}/orm" compression="gzip" />
    </target>
</project>