<?xml version="1.0" encoding="UTF-8"?>
<project name="Orchard PHP Encryption Library" default="build">
    <target name="build" depends="prepare,composer,parallelTasks,phpcb"/>
    <target name="build-pr" depends="prepare,composer,parallelTasks-pr,phpcb"/>
    <property name="exclusionlist" value="${basedir}/../vendor"/>
    <property name="sourcedir" value="${basedir}/../src"/>
    <property name="unittestsdir" value="${basedir}/../tests"/>
    <property name="execdir" value="${basedir}/../vendor/bin"/>

    <target name="clean" description="Cleanup build artifacts">
        <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}/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="${execdir}/phpunit" dir="${unittestsdir}" 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="commonTasks" description="Tasks run by both PR and pull builds">
        <antcall target="phpunit"/>
        <sequential>
            <antcall target="pdepend"/>
            <antcall target="phpmd"/>
        </sequential>
        <antcall target="phpcpd"/>
        <antcall target="phploc"/>
    </target>

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

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

    <target name="pdepend" description="Calculate software metrics using PHP_Depend">
        <exec executable="${execdir}/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=${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="${basedir}/logs/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="${basedir}/logs/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="${basedir}/logs/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=${basedir}/logs/checkstyle.xml"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${sourcedir}"/>
            <arg path="${unittestsdir}"/>
        </exec>
    </target>

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

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

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