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

<project name="Cloudalytics" default="build">
    <property name="exclusionlist"
              value="vendor,configs"/>
    <property name="exclusionlistwithslashes"
              value="vendor/,configs/"/>

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

    <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="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="php" failonerror="true">
            <arg value="composer.phar"/>
            <arg value="self-update"/>
        </exec>
        <exec executable="php" failonerror="true">
            <arg value="composer.phar"/>
            <arg value="install"/>
            <arg value="--dev"/>
            <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/**"/>
                <exclude name="configs/**"/>
            </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/**"/>
                <exclude name="configs/**"/>
                <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"/>
    </target>

    <target name="parallelTasksForPRBuild"
            description="Run code analysis tasks in parallel. This is for PR builds only">
        <parallel threadCount="2">
            <antcall target="lint-delta"/>
            <antcall target="phpcs"/>
        </parallel>
    </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,phtml"/>
            <arg value="-p"/>
            <arg value="--report=checkstyle"/>
            <arg value="--report-file=${basedir}/build/logs/checkstyle.xml"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${basedir}"/>
        </exec>
    </target>

</project>