<?xml version="1.0" encoding="UTF-8"?>

<project name="Orchard" default="build">

    <property name="exclusionlist"
              value="vendor,build,config,db,"/>
    <property name="exclusionlistwithslashes"
              value="library/Zend"/>

    <target name="build-dev"
            depends="prepare,composer,lint,phpunit"/>

    <target name="build-jenkins"
            depends="prepare,config-jenkins,composer,lint,phpunit"/>

    <target name="clean" description="Cleanup build artifacts">
        <delete dir="${basedir}/build/api"/>
        <delete dir="${basedir}/build/code-browser"/>
        <delete dir="${basedir}/build/coverage"/>
        <delete dir="${basedir}/build/logs"/>
        <delete dir="${basedir}/build/pdepend"/>
        <delete file="${basedir}/composer.lock"/>
    </target>

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

    <target name="config-jenkins" depends="prepare" description="Configure build to run on dev boxes">
        <scp remotefile="jenkins@ciconfigs:/mnt/configs/redis-lib/config.json"
             todir="${basedir}/config" trust="true" keyfile="${user.home}/.ssh/id_rsa" password=" "/>
    </target>

    <target name="composer" description="Install dependencies">
        <exec executable="wget" failonerror="true">
            <arg value="-nc"/>
            <arg value="http://getcomposer.org/composer.phar"/>
        </exec>
        <exec executable="php" failonerror="true">
            <arg value="composer.phar"/>
            <arg value="--dev"/>
            <arg value="install"/>
        </exec>
    </target>

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

            <fileset dir="${basedir}/lib">
                <include name="*.php"/>
            </fileset>

        </apply>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
        <exec executable="${basedir}/vendor/bin/phpunit" dir="${basedir}/test" failonerror="false"/>
    </target>

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

    <target name="pdepend"
            description="Calculate software metrics using PHP_Depend">
        <exec executable="${basedir}/vendor/bin/pdepend">
            <arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml"/>
            <arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg"/>
            <arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg"/>
            <arg path="${basedir}/lib"/>
        </exec>
    </target>

    <target name="phpmd"
            description="Perform project mess detection using PHPMD">
        <exec executable="${basedir}/vendor/bin/phpmd" dir="${basedir}">
            <arg value="./lib"/>
            <arg value="xml"/>
            <arg value="codesize,design,unusedcode,naming"/>
            <arg value="--reportfile"/>
            <arg value="${basedir}/build/logs/pmd.xml"/>
            <arg value="--exclude"/>
            <arg value="${exclusionlist}"/>
        </exec>
    </target>

    <target name="phpcpd" description="Find duplicate code using PHPCPD">
        <exec executable="${basedir}/vendor/bin/phpcpd" dir="${basedir}" logerror="on">
            <arg value="--log-pmd"/>
            <arg value="${basedir}/build/logs/pmd-cpd.xml"/>
            <arg value="--exclude"/>
            <arg value="vendor"/>
            <arg value="--exclude"/>
            <arg value="config"/>
            <arg value="--exclude"/>
            <arg value="db"/>
            <arg value="--exclude"/>
            <arg value="test"/>
            <arg path="${basedir}/lib"/>
        </exec>
    </target>

    <target name="phploc" description="Measure project size using PHPLOC">
        <exec executable="${basedir}/vendor/bin/phploc">
            <arg value="--log-csv"/>
            <arg value="${basedir}/build/logs/phploc.csv"/>
            <arg value="--exclude"/>
            <arg value="vendor"/>
            <arg value="--exclude"/>
            <arg value="config"/>
            <arg value="--exclude"/>
            <arg value="db"/>
            <arg value="--exclude"/>
            <arg value="test"/>
            <arg path="${basedir}"/>
        </exec>
    </target>

    <target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer">
        <exec executable="${basedir}/vendor/bin/phpcs">
            <arg value="--extensions=php,inc,lib,phtml"/>
            <arg value="--tab-width=4"/>
            <arg value="--report=checkstyle"/>
            <arg value="--report-file=${basedir}/build/logs/checkstyle.xml"/>
            <arg value="--standard=${basedir}/build/phpcs.xml"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${basedir}/lib"/>
        </exec>
    </target>

    <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
        <exec executable="${basedir}/vendor/bin/phpcb">
            <arg path="${basedir}/lib"/>
            <arg value="--log"/>
            <arg path="${basedir}/build/logs"/>
            <arg value="--source"/>
            <arg path="${basedir}"/>
            <arg value="--output"/>
            <arg path="${basedir}/build/code-browser"/>
            <arg value="--ignore"/>
            <arg value="${exclusionlist}"/>
        </exec>
    </target>
</project>