<?xml version="1.0" encoding="UTF-8"?>
<project name="Orchard" default="build">
    <target name="build" depends="prepare,composer,parallelTasks,phpcb"/>
    <target name="lint_test" depends="lint,phpunit"/>
    <property name="exclusionlist" value="${basedir}/../vendor"/>
    <property name="exclusionlistwithslashes" value="${basedir}/../vendor/"/>
    <property name="sourcedir" value="${basedir}/../src"/>
    <property name="unittestsdir" value="${basedir}/../tests"/>
    <property name="execdir" value="${basedir}/../vendor/bin"/>
    <property environment="env"/>
    <property name="env" value="${env.PHP_ENV}"/>
    <property name="builddir" value="${basedir}"/>
    <property name="logdir" value="${builddir}/logs/${env}"/>
    <property name="coveragedir" value="${builddir}/coverage/${env}"/>

    <target name="clean" description="Cleanup build artifacts">
        <delete dir="${basedir}/api"/>
        <delete dir="${basedir}/code-browser"/>
        <delete dir="${coveragedir}"/>
        <delete dir="${logdir}"/>
        <delete dir="${basedir}/pdepend"/>
    </target>

    <target name="prepare" depends="clean" description="Prepare for build">
        <mkdir dir="${basedir}/api"/>
        <mkdir dir="${basedir}/code-browser"/>
        <mkdir dir="${coveragedir}"/>
        <mkdir dir="${logdir}"/>
        <mkdir dir="${basedir}/pdepend"/>
    </target>

    <target name="composer" description="Install dependencies">
        <exec executable="wget" failonerror="true">
            <arg value="-O"/>
            <arg value="composer.phar"/>
            <arg value="http://getcomposer.org/composer.phar" />
        </exec>
        <exec executable="/usr/local/bin/php" failonerror="true">
            <arg value="composer.phar" />
            <arg value="update" />
            <arg value="--prefer-dist" />
            <arg value="--working-dir=${basedir}/../" />
        </exec>
    </target>

    <target name="lint">
        <apply executable="php" failonerror="true">
            <arg value="-l" />

            <fileset dir="${basedir}/../">
                <include name="**/*.php" />
				<exclude name="vendor/**"/>
                <modified update="true"
                    seldirs="true"
                    algorithm="digest"
                    comparator="equal">
                </modified>
            </fileset>

        </apply>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
        <exec executable="${execdir}/phpunit" dir="${unittestsdir}"  failonerror="true">
            <arg value="--coverage-html"/>
            <arg value="${coveragedir}"/>
            <arg value="--coverage-clover"/>
            <arg value="${logdir}/phpunit.coverage.xml"/>
            <arg value="--log-junit"/>
            <arg value="${logdir}/phpunit.xml"/>
        </exec>
    </target>

    <target name="parallelTasks" description="Run code analysis tasks in parallel">
        <parallel threadCount="3">
            <antcall target="lint"/>
            <antcall target="phpunit"/>
            <antcall target="phpcs"/>
            <sequential>
                <antcall target="pdepend"/>
                <antcall target="phpmd"/>
            </sequential>
            <antcall target="phpcpd"/>
            <antcall target="phploc"/>
        </parallel>
    </target>

    <target name="pdepend" description="Calculate software metrics using PHP_Depend">
        <exec executable="${execdir}/pdepend">
            <arg value="--jdepend-xml=${logdir}/jdepend.xml" />
            <arg value="--jdepend-chart=${basedir}/pdepend/dependencies.svg" />
            <arg value="--overview-pyramid=${basedir}/pdepend/overview-pyramid.svg" />
            <arg value="--ignore=${exclusionlist}" />
            <arg path="${sourcedir}" />
        </exec>
    </target>

    <target name="phpmd" description="Perform project mess detection using PHPMD">
        <exec executable="${execdir}/phpmd" dir="${sourcedir}">
            <arg value="." />
            <arg value="xml" />
            <arg value="codesize,design,unusedcode,naming" />
            <arg value="--reportfile" />
            <arg value="${logdir}/pmd.xml" />
            <arg value="--exclude"/>
            <arg value="${exclusionlist}" />
        </exec>
    </target>

    <target name="phpcpd" description="Find duplicate code using PHPCPD">
        <exec executable="${execdir}/phpcpd" dir="${sourcedir}" logerror="on" >
            <arg value="--log-pmd" />
            <arg value="${logdir}/pmd-cpd.xml"/>
            <arg value="--exclude" />
            <arg value="${exclusionlist}" />
            <arg path="${sourcedir}" />
        </exec>
    </target>

    <target name="phploc" description="Measure project size using PHPLOC">
        <exec executable="${execdir}/phploc">
            <arg value="--log-csv" />
            <arg value="${logdir}/phploc.csv" />
            <arg value="--exclude=${exclusionlist}" />
            <arg path="${sourcedir}" />
        </exec>
    </target>

    <target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer">
        <exec executable="${execdir}/phpcs">
            <arg value="--extensions=php"/>
            <arg value="-p" />
            <arg value="--report=checkstyle" />
            <arg value="--report-file=${logdir}/checkstyle.xml"/>
            <arg value="--standard=PSR2" />
            <arg value="--ignore=${exclusionlist}" />
            <arg path="${sourcedir}" />
            <arg path="${unittestsdir}" />
        </exec>
    </target>

    <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
        <exec executable="${execdir}/phpcb">
            <arg value="--log" />
            <arg path="${logdir}" />
            <arg value="--source" />
            <arg path="${sourcedir}" />
            <arg value="--output" />
            <arg path="${basedir}/code-browser" />
            <arg value="--ignore" />
            <arg path="${exclusionlist}" />
        </exec>
    </target>
</project>
