<?xml version="1.0" encoding="UTF-8"?>
<project name="Orchard helpcenter" default="build">
    <target name="build" depends="prepare,composer,prepareTests,parallelTasks"/>
    <target name="build-pr" depends="prepare,composer,prepareTests,parallelTasks-pr"/>
    <property name="unittestsdir" value="${basedir}/../vendor/wordpress/wordpress-develop"/>
    <property name="execdir" location="${basedir}/../vendor/bin"/>

    <target name="clean" description="Cleanup build artifacts">
        <delete dir="${basedir}/coverage"/>
        <delete dir="${basedir}/logs"/>
    </target>

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

    <target name="prepareTests" description="Prepare for tests">
        <copy todir="${basedir}/../vendor/wordpress/wordpress-develop/src/wp-content/themes/helpcenter-theme-child" >
            <fileset dir="${basedir}/../themes/helpcenter-theme-child" includes="**"/>
        </copy>
        <copy todir="${basedir}/../vendor/wordpress/wordpress-develop/src/wp-content/plugins" >
            <fileset dir="${basedir}/../plugins" includes="**"/>
        </copy>
        <copy todir="${basedir}/../vendor/wordpress/wordpress-develop/tests/phpunit/tests/helpcenter" >
            <fileset dir="${basedir}/../tests/helpcenter" includes="**"/>
        </copy>
        <copy file="${basedir}/../wp-tests-config.php" tofile="${basedir}/../vendor/wordpress/wordpress-develop/wp-tests-config.php"/>
        <copy file="${basedir}/../phpunit.xml" tofile="${basedir}/../vendor/wordpress/wordpress-develop/phpunit.xml"/>
    </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="themes/helpcenter-theme-child/functions.php"/>
            </fileset>
        </apply>
    </target>

    <target name="lint-delta">
        <apply executable="/usr/local/bin/php" failonerror="true">
            <arg value="-l"/>
            <fileset dir="${basedir}/../">
                <include name="themes/helpcenter-theme-child/functions.php"/>
                <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"/>
            <arg value="--verbose"/>
        </exec>
    </target>

    <target name="commonTasks" description="Tasks run by both PR and pull builds">
        <antcall target="phpunit"/>
    </target>

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

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