<?xml version="1.0" encoding="UTF-8"?>
<project name="DirectDelivery" default="build"
    xmlns:if="ant:if" xmlns:unless="ant:unless">

    <property name="exclusionlist" value="${basedir}/vendor"/>
    <property name="exclusionlistwithslashes" value="${basedir}/vendor/"/>
    <property name="sourcedir" value="${basedir}/src"/>
    <property name="testsdir" value="${basedir}/tests"/>
    <property name="execdir" value="${basedir}/vendor/bin"/>

    <property name="testFilter" value=""/>
    <property name="skipLint" value=""/>
    <property name="skipCoverage" value=""/>
    <property name="updateSnapshots" value="false"/>

    <target name="build">
        <antcall target="yamllint" unless:true="${skipLint}"/>
        <antcall target="phpstan" unless:true="${skipLint}"/>
        <antcall target="lint-parallel" unless:true="${skipLint}"/>
        <antcall target="phpcs" unless:true="${skipLint}"/>
        <antcall target="php-cs-fixer-lint" unless:true="${skipLint}"/>
        <antcall target="phpunit"/>
    </target>

    <target name="yamllint" description="Lint YAML files using yamllint">
        <exec executable="yamllint" failonerror="true">
            <arg value="-s"/>
            <arg value="${basedir}/docker-compose.yaml"/>
            <arg value="${basedir}/docker-compose.unit-lint.yaml"/>
        </exec>
    </target>

    <target name="lint-parallel">
        <exec executable="${execdir}/parallel-lint" failonerror="true">
            <arg value="${basedir}/"/>
            <arg value="-e"/>
            <arg value="php"/>
            <arg value="--short"/>
            <arg value="--exclude"/>
            <arg value="${exclusionlistwithslashes}"/>
        </exec>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
        <exec executable="${execdir}/phpunit" dir="${testsdir}" failonerror="true">
            <arg value="--configuration" />
            <arg value="${testsdir}/phpunit.xml" />
            <arg value="--testsuite" />
            <arg value="unit" />
            <arg value="--coverage-html" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/coverage" unless:true="${skipCoverage}"/>
            <arg value="--coverage-clover" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/logs/phpunit.coverage.xml" unless:true="${skipCoverage}"/>
            <arg value="--log-junit" unless:true="${skipCoverage}"/>
            <arg value="${basedir}/build/logs/phpunit.xml" unless:true="${skipCoverage}"/>
            <arg value="--filter" unless:blank="${testFilter}"/>
            <arg value="${testFilter}" unless:blank="${testFilter}"/>
            <arg value="-d --snapshot-update" if:true="${updateSnapshots}"/>
            <arg value="--verbose"/>
        </exec>
    </target>

    <target name="integration" description="Run integration tests with PHPUnit">
        <exec executable="${execdir}/phpunit" dir="${testsdir}" failonerror="true">
            <arg value="--configuration" />
            <arg value="${testsdir}/phpunit.xml" />
            <arg value="--testsuite" />
            <arg value="integration" />
            <arg value="--filter" unless:blank="${testFilter}"/>
            <arg value="${testFilter}" unless:blank="${testFilter}"/>
        </exec>
    </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="--report=full" />
            <arg value="--standard=PSR12" />
            <arg value="--ignore=${exclusionlist}" />
            <arg path="${sourcedir}" />
            <arg path="${testsdir}" />
            <arg path="${basedir}/encode_tracks.php" />
            <arg path="${basedir}/deliver_tracks.php" />
            <arg path="${basedir}/scripts/obtain_encoding_orders.php" />
        </exec>
    </target>

    <target name="phpstan" description="Static code analysis using PHPStan">
        <exec executable="${execdir}/phpstan" failonerror="true">
            <arg value="analyze"/>
            <arg value="-c"/>
            <arg value="${basedir}/build/phpstan.neon.dist"/>
        </exec>
    </target>

    <target name="phpcbf" description="Format code using PHP_CodeSniffer">
        <exec executable="${execdir}/phpcbf" failonerror="false">
            <arg value="-p"/>
            <arg value="--extensions=php,inc"/>
            <arg value="--standard=PSR12"/>
            <arg value="--ignore=${exclusionlist}"/>
            <arg path="${sourcedir}" />
            <arg path="${testsdir}" />
            <arg path="${basedir}/encode_tracks.php" />
            <arg path="${basedir}/deliver_tracks.php" />
            <arg path="${basedir}/scripts/obtain_encoding_orders.php" />
        </exec>
    </target>

    <target name="php-cs-fixer-lint" description="Check code format using php-cs-fixer">
        <exec executable="${execdir}/php-cs-fixer" failonerror="true">
            <arg value="check"/>
            <arg value="--diff"/>
            <arg value="--config=${basedir}/build/.php-cs-fixer.dist.php"/>
            <arg value="--allow-unsupported-php-version=no"/>
            <arg value="--show-progress=dots"/>
        </exec>
    </target>

    <target name="php-cs-fixer" description="Format code using php-cs-fixer">
        <exec executable="${execdir}/php-cs-fixer" failonerror="false">
            <arg value="fix"/>
            <arg value="--config=${basedir}/build/.php-cs-fixer.dist.php"/>
            <arg value="--allow-unsupported-php-version=no"/>
            <arg value="--show-progress=dots"/>
        </exec>
    </target>
</project>
