<?xml version="1.0" encoding="UTF-8"?>
<project name="Orchard bulkupload service" default="build">
    <target name="build" depends="prepare,buildTask"/>
    <property name="exclusionlist" value="${basedir}/../vendor"/>
    <property name="unittestsdir" value="${basedir}/../tests"/>
    <property name="execdir" location="${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="buildTask" description="Run code analysis tasks">
        <antcall target="lint"/>
        <antcall target="phpcs"/>
        <antcall target="phpstan"/>
        <antcall target="phpunit"/>
    </target>

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

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

    <target name="phpcs" description="Find coding standard violations using PHP_CodeSniffer">
        <exec executable="${execdir}/phpcs" failonerror="true">
            <arg value="--extensions=php"/>
            <arg value="-p"/>
            <arg value="--standard=PSR2"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${basedir}/../src"/>
            <arg path="${basedir}/../tests"/>
            <arg path="${basedir}/../htmlupload.php"/>
        </exec>
    </target>

    <target name="phpstan" description="Run PHPStan static analysis">
        <exec executable="${execdir}/phpstan" failonerror="true">
            <arg value="analyse"/>
            <arg value="--level=1"/>
            <arg value="${basedir}/../src"/>
            <arg value="${basedir}/../htmlupload.php"/>
        </exec>
    </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>
</project>
