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

<project name="Orchard-Features" default="build">
    <property name="exclusionlist"
              value="vendor"/>
    <property name="exclusionlistwithslashes"
              value="vendor/"/>

    <target name="build"
            depends="prepare,composer"/>

    <target name="build-pr"
            depends="build,parallelTasks-pr"/>

    <target name="build-jenkins"
            depends="build,parallelTasks"/>

    <target name="clean" description="Cleanup build artifacts">
        <delete dir="${basedir}/api"/>
        <delete dir="${basedir}/code-browser"/>
        <delete dir="${basedir}/coverage"/>
        <delete dir="${basedir}/logs"/>
        <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="${basedir}/coverage"/>
        <mkdir dir="${basedir}/logs"/>
        <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="/usr/local/bin/php" failonerror="true">
            <arg value="-l"/>

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

        </apply>
    </target>

    <target name="lint-delta">
        <apply executable="/usr/local/bin/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="${basedir}/../vendor/bin/phpunit" dir="${basedir}/../tests" failonerror="true">
            <arg value="--coverage-html"/>
            <arg value="${basedir}/coverage"/>
            <arg value="--coverage-clover"/>
            <arg value="${basedir}/logs/phpunit.coverage.xml"/>
            <arg value="--log-junit"/>
            <arg value="${basedir}/logs/phpunit.xml"/>
        </exec>
    </target>

    <target name="parallelTasks-pr"
            description="Run code analysis tasks in parallel. This is for PR builds only">
        <parallel threadCount="3">
            <antcall target="lint-delta"/>
            <antcall target="phpcs-delta"/>
            <antcall target="phpunit"/>
        </parallel>
    </target>

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

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

    <target name="phpmd"
            description="Perform project mess detection using PHPMD">
        <exec executable="${basedir}/../vendor/bin/phpmd" dir="${basedir}">
            <arg value="."/>
            <arg value="xml"/>
            <arg value="codesize,design,unusedcode,naming"/>
            <arg value="--reportfile"/>
            <arg value="${basedir}/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}/logs/pmd-cpd.xml"/>
            <arg value="--exclude"/>
            <arg value="vendor"/>
            <arg path="${basedir}"/>
        </exec>
    </target>

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

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

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

    <target name="phpcs-delta" description="Find coding standard violations using PHP_CodeSniffer">
        <apply executable="${basedir}/../vendor/bin/phpcs" failonerror="true">
            <arg value="--extensions=php,inc,lib"/>
            <arg value="-v"/>
            <arg value="-n"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>

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