<?xml version="1.0" encoding="UTF-8"?>
<project name="phpunit" default="setup">
    <target name="setup" depends="clean,install-dependencies"/>
    <target name="validate" depends="php-syntax-check,validate-composer-json,validate-phpunit-xsd"/>

    <target name="clean" unless="clean.done" description="Cleanup build artifacts">
        <delete dir="${basedir}/bin"/>
        <delete dir="${basedir}/vendor"/>
        <delete file="${basedir}/composer.lock"/>
        <delete dir="${basedir}/build/documentation"/>
        <delete dir="${basedir}/build/logfiles"/>
        <delete dir="${basedir}/build/phar"/>
        <delete>
            <fileset dir="${basedir}/build">
                <include name="**/phpunit*.phar"/>
                <include name="**/phpunit*.phar.asc"/>
            </fileset>
        </delete>

        <property name="clean.done" value="true"/>
    </target>

    <target name="prepare" unless="prepare.done" depends="clean" description="Prepare for build">
        <mkdir dir="${basedir}/build/documentation"/>
        <mkdir dir="${basedir}/build/logfiles"/>
        <property name="prepare.done" value="true"/>
    </target>

    <target name="validate-composer-json" depends="clean" unless="validate-composer-json.done" description="Validate composer.json">
        <exec executable="${basedir}/tools/composer" failonerror="true" taskname="composer">
            <arg value="validate"/>
            <arg value="--strict"/>
            <arg value="${basedir}/composer.json"/>
        </exec>

        <property name="validate-composer-json.done" value="true"/>
    </target>

    <target name="-dependencies-installed">
        <available file="${basedir}/composer.lock" property="dependencies-installed"/>
    </target>

    <target name="install-dependencies" unless="dependencies-installed" depends="-dependencies-installed,validate-composer-json" description="Install dependencies with Composer">
        <copy file="${basedir}/composer.json" tofile="${basedir}/composer.json.bak"/>

        <exec executable="${basedir}/tools/composer" taskname="composer">
            <arg value="require"/>
            <arg value="--no-update"/>
            <arg value="phpunit/php-invoker:^2.0"/>
        </exec>

        <exec executable="${basedir}/tools/composer" taskname="composer">
            <arg value="update"/>
            <arg value="--no-interaction"/>
            <arg value="--no-progress"/>
            <arg value="--no-ansi"/>
            <arg value="--no-suggest"/>
        </exec>

        <move file="${basedir}/composer.json.bak" tofile="${basedir}/composer.json"/>
    </target>

    <target name="check-dependencies" description="Performs check for outdated dependencies">
        <exec executable="${basedir}/tools/composer" taskname="composer">
            <arg value="show"/>
            <arg value="--minor-only"/>
            <arg value="--latest"/>
            <arg value="--direct"/>
            <arg value="--outdated"/>
            <arg value="--strict"/>
        </exec>
    </target>

    <target name="php-syntax-check" unless="php-syntax-check.done" description="Perform syntax check on PHP files">
        <apply executable="php" failonerror="true" taskname="lint">
            <arg value="-l"/>

            <fileset dir="${basedir}/src">
                <include name="**/*.php"/>
                <modified/>
            </fileset>

            <fileset dir="${basedir}/tests">
                <include name="**/*.php"/>
                <modified/>
            </fileset>
        </apply>

        <property name="php-syntax-check.done" value="true"/>
    </target>

    <target name="validate-phpunit-xsd" unless="validate-phpunit-xsd.done" description="Validate phpunit.xsd">
        <exec executable="xmllint" failonerror="true" taskname="xmllint">
            <arg value="--noout"/>
            <arg path="${basedir}/phpunit.xsd"/>
        </exec>

        <property name="validate-phpunit-xsd.done" value="true"/>
    </target>

    <target name="test" depends="validate,install-dependencies" description="Run tests">
        <exec executable="${basedir}/phpunit" taskname="phpunit"/>
    </target>

    <target name="signed-phar" depends="phar" description="Create signed PHAR archive of PHPUnit and all its dependencies">
        <exec executable="gpg" failonerror="true">
            <arg value="--local-user"/>
            <arg value="sb@sebastian-bergmann.de"/>
            <arg value="--armor"/>
            <arg value="--detach-sign"/>
            <arg path="${basedir}/build/phpunit-library-${version}.phar"/>
        </exec>

        <exec executable="gpg" failonerror="true">
            <arg value="--local-user"/>
            <arg value="sb@sebastian-bergmann.de"/>
            <arg value="--armor"/>
            <arg value="--detach-sign"/>
            <arg path="${basedir}/build/phpunit-${version}.phar"/>
        </exec>
    </target>

    <target name="phar" depends="-phar-prepare,-phar-determine-version" description="Create PHAR archive of PHPUnit and all its dependencies">
        <antcall target="-phar-build">
            <param name="type" value="release"/>
        </antcall>
    </target>

    <target name="phar-nightly" depends="-phar-prepare" description="Create PHAR archive of PHPUnit and all its dependencies (nightly)">
        <antcall target="-phar-build">
            <param name="type" value="nightly"/>
        </antcall>
    </target>

    <target name="-phar-prepare" depends="clean,install-dependencies">
        <mkdir dir="${basedir}/build/phar"/>

        <copy file="${basedir}/phpunit.xsd" tofile="${basedir}/build/phar/phpunit.xsd"/>

        <exec executable="${basedir}/build/phar-manifest.php" output="${basedir}/build/phar/manifest.txt" failonerror="true"/>

        <copy file="${basedir}/vendor/phpunit/php-code-coverage/LICENSE" tofile="${basedir}/build/phar/php-code-coverage/LICENSE"/>
        <copy todir="${basedir}/build/phar/php-code-coverage">
            <fileset dir="${basedir}/vendor/phpunit/php-code-coverage/src">
                <include name="**/*" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpunit/php-file-iterator/LICENSE" tofile="${basedir}/build/phar/php-file-iterator/LICENSE"/>
        <copy todir="${basedir}/build/phar/php-file-iterator">
            <fileset dir="${basedir}/vendor/phpunit/php-file-iterator/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy todir="${basedir}/build/phar/php-invoker">
            <fileset dir="${basedir}/vendor/phpunit/php-invoker/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpunit/php-text-template/LICENSE" tofile="${basedir}/build/phar/php-text-template/LICENSE"/>
        <copy todir="${basedir}/build/phar/php-text-template">
            <fileset dir="${basedir}/vendor/phpunit/php-text-template/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpunit/php-timer/LICENSE" tofile="${basedir}/build/phar/php-timer/LICENSE"/>
        <copy todir="${basedir}/build/phar/php-timer">
            <fileset dir="${basedir}/vendor/phpunit/php-timer/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpunit/php-token-stream/LICENSE" tofile="${basedir}/build/phar/php-token-stream/LICENSE"/>
        <copy todir="${basedir}/build/phar/php-token-stream">
            <fileset dir="${basedir}/vendor/phpunit/php-token-stream/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/code-unit-reverse-lookup/LICENSE" tofile="${basedir}/build/phar/sebastian-code-unit-reverse-lookup/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-code-unit-reverse-lookup">
            <fileset dir="${basedir}/vendor/sebastian/code-unit-reverse-lookup/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/comparator/LICENSE" tofile="${basedir}/build/phar/sebastian-comparator/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-comparator">
            <fileset dir="${basedir}/vendor/sebastian/comparator/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/diff/LICENSE" tofile="${basedir}/build/phar/sebastian-diff/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-diff">
            <fileset dir="${basedir}/vendor/sebastian/diff/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/environment/LICENSE" tofile="${basedir}/build/phar/sebastian-environment/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-environment">
            <fileset dir="${basedir}/vendor/sebastian/environment/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/exporter/LICENSE" tofile="${basedir}/build/phar/sebastian-exporter/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-exporter">
            <fileset dir="${basedir}/vendor/sebastian/exporter/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/global-state/LICENSE" tofile="${basedir}/build/phar/sebastian-global-state/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-global-state">
            <fileset dir="${basedir}/vendor/sebastian/global-state/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/object-enumerator/LICENSE" tofile="${basedir}/build/phar/object-enumerator/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-object-enumerator">
            <fileset dir="${basedir}/vendor/sebastian/object-enumerator/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/object-reflector/LICENSE" tofile="${basedir}/build/phar/object-reflector/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-object-reflector">
            <fileset dir="${basedir}/vendor/sebastian/object-reflector/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/recursion-context/LICENSE" tofile="${basedir}/build/phar/sebastian-recursion-context/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-recursion-context">
            <fileset dir="${basedir}/vendor/sebastian/recursion-context/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/resource-operations/LICENSE" tofile="${basedir}/build/phar/sebastian-resource-operations/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-resource-operations">
            <fileset dir="${basedir}/vendor/sebastian/resource-operations/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/sebastian/version/LICENSE" tofile="${basedir}/build/phar/sebastian-version/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-version">
            <fileset dir="${basedir}/vendor/sebastian/version/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/doctrine/instantiator/LICENSE" tofile="${basedir}/build/phar/doctrine-instantiator/LICENSE"/>
        <copy todir="${basedir}/build/phar/doctrine-instantiator">
            <fileset dir="${basedir}/vendor/doctrine/instantiator/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/myclabs/deep-copy/LICENSE" tofile="${basedir}/build/phar/myclabs-deep-copy/LICENSE"/>
        <copy todir="${basedir}/build/phar/myclabs-deep-copy">
            <fileset dir="${basedir}/vendor/myclabs/deep-copy/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phar-io/manifest/LICENSE" tofile="${basedir}/build/phar/phar-io-manifest/LICENSE"/>
        <copy todir="${basedir}/build/phar/phar-io-manifest">
            <fileset dir="${basedir}/vendor/phar-io/manifest/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phar-io/version/LICENSE" tofile="${basedir}/build/phar/phar-io-version/LICENSE"/>
        <copy todir="${basedir}/build/phar/phar-io-version">
            <fileset dir="${basedir}/vendor/phar-io/version/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpdocumentor/reflection-common/LICENSE" tofile="${basedir}/build/phar/phpdocumentor-reflection-common/LICENSE"/>
        <copy todir="${basedir}/build/phar/phpdocumentor-reflection-common">
            <fileset dir="${basedir}/vendor/phpdocumentor/reflection-common/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpdocumentor/reflection-docblock/LICENSE" tofile="${basedir}/build/phar/phpdocumentor-reflection-docblock/LICENSE"/>
        <copy todir="${basedir}/build/phar/phpdocumentor-reflection-docblock">
            <fileset dir="${basedir}/vendor/phpdocumentor/reflection-docblock/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpdocumentor/type-resolver/LICENSE" tofile="${basedir}/build/phar/phpdocumentor-type-resolver/LICENSE"/>
        <copy todir="${basedir}/build/phar/phpdocumentor-type-resolver">
            <fileset dir="${basedir}/vendor/phpdocumentor/type-resolver/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/phpspec/prophecy/LICENSE" tofile="${basedir}/build/phar/phpspec-prophecy/LICENSE"/>
        <copy todir="${basedir}/build/phar/phpspec-prophecy">
            <fileset dir="${basedir}/vendor/phpspec/prophecy/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/theseer/tokenizer/LICENSE" tofile="${basedir}/build/phar/theseer-tokenizer/LICENSE"/>
        <copy todir="${basedir}/build/phar/theseer-tokenizer">
            <fileset dir="${basedir}/vendor/theseer/tokenizer/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <copy file="${basedir}/vendor/webmozart/assert/LICENSE" tofile="${basedir}/build/phar/webmozart-assert/LICENSE"/>
        <copy todir="${basedir}/build/phar/webmozart-assert">
            <fileset dir="${basedir}/vendor/webmozart/assert/src">
                <include name="**/*.php" />
            </fileset>
        </copy>
    </target>

    <target name="-phar-build" depends="-phar-determine-version">
        <copy todir="${basedir}/build/phar/phpunit">
            <fileset dir="${basedir}/src">
                <include name="**/*.php"/>
                <include name="**/*.tpl*"/>
            </fileset>
        </copy>

        <exec executable="${basedir}/build/phar-version.php" outputproperty="_version" failonerror="true">
            <arg value="${version}"/>
            <arg value="${type}"/>
        </exec>

        <exec executable="${basedir}/tools/phpab" taskname="phpab" failonerror="true">
            <arg value="--all" />
            <arg value="--static" />
            <arg value="--once" />
            <arg value="--phar" />
            <arg value="--hash" />
            <arg value="SHA-1" />
            <arg value="--output" />
            <arg path="${basedir}/build/phpunit-library-${_version}.phar" />
            <arg value="--template" />
            <arg path="${basedir}/build/library-phar-autoload.php.in" />
            <arg path="${basedir}/build/phar" />
        </exec>

        <copy file="${basedir}/build/binary-phar-autoload.php.in" tofile="${basedir}/build/binary-phar-autoload.php"/>
        <replace file="${basedir}/build/binary-phar-autoload.php" token="X.Y.Z" value="${_version}"/>

        <exec executable="${basedir}/tools/phpab" taskname="phpab" failonerror="true">
            <arg value="--all" />
            <arg value="--nolower" />
            <arg value="--static" />
            <arg value="--phar" />
            <arg value="--hash" />
            <arg value="SHA-1" />
            <arg value="--output" />
            <arg path="${basedir}/build/phpunit-${_version}.phar" />
            <arg value="--template" />
            <arg path="${basedir}/build/binary-phar-autoload.php" />
            <arg path="${basedir}/build/phar" />
        </exec>

        <chmod file="${basedir}/build/phpunit-${_version}.phar" perm="ugo+rx"/>

        <delete dir="${basedir}/build/phar"/>
        <delete file="${basedir}/build/binary-phar-autoload.php"/>
    </target>

    <target name="-phar-determine-version">
        <exec executable="${basedir}/build/version.php" outputproperty="version" failonerror="true" />
    </target>

    <target name="generate-project-documentation" depends="-phploc,-checkstyle,-phpunit">
        <exec executable="${basedir}/tools/phpdox" dir="${basedir}/build" taskname="phpdox"/>
    </target>

    <target name="update-tools">
        <exec executable="phive">
            <arg value="--no-progress"/>
            <arg value="update"/>
        </exec>

        <exec executable="${basedir}/tools/composer">
            <arg value="self-update"/>
        </exec>
    </target>

    <target name="-phploc" depends="prepare">
        <exec executable="${basedir}/tools/phploc" output="/dev/null" taskname="phploc">
            <arg value="--count-tests"/>
            <arg value="--log-xml"/>
            <arg path="${basedir}/build/logfiles/phploc.xml"/>
            <arg path="${basedir}/src"/>
            <arg path="${basedir}/tests"/>
        </exec>
    </target>

    <target name="-checkstyle" depends="prepare">
        <exec executable="${basedir}/tools/php-cs-fixer" output="${basedir}/build/logfiles/checkstyle.xml" error="/dev/null" taskname="php-cs-fixer">
            <arg value="--diff"/>
            <arg value="--dry-run"/>
            <arg value="fix"/>
            <arg value="--format=checkstyle"/>
        </exec>
    </target>

    <target name="-phpunit" depends="setup">
        <exec executable="${basedir}/phpunit" taskname="phpunit">
            <arg value="--coverage-xml"/>
            <arg path="${basedir}/build/logfiles/coverage"/>
            <arg value="--log-junit"/>
            <arg path="${basedir}/build/logfiles/junit.xml"/>
        </exec>
    </target>
</project>

