<?xml version="1.0" encoding="UTF-8"?>
<project name="tsw" default="build">
    <!-- By default, we assume all tools to be on the $PATH -->
    <property name="pdepend" value="pdepend"/>
    <property name="phpcpd"  value="phpcpd"/>
    <property name="phpcs"   value="phpcs"/>
    <property name="phploc"  value="phploc"/>
    <property name="phpmd"   value="phpmd"/>
    <property name="phpunit" value="${basedir}/vendor/bin/phpunit"/>

    <property name="databasename" value="tsw"/>

    <target name="build"
            depends="prepare"
            description="Prepare the application"/>

    <target name="test" depends="build, database, static-analysis, phpunit"/>

    <target name="static-analysis"
            depends="lint,phploc-ci,pdepend, phpcs-ci,phpcpd-ci"
            description="Performs static analysis" />

    <!-- Adjust the threadCount attribute's value to the number of CPUs -->
    <target name="static-analysis-parallel"
            description="Performs static analysis (executing the tools in parallel)">
        <parallel threadCount="2">
            <sequential>
                <antcall target="pdepend"/>
                <antcall target="phpmd-ci"/>
            </sequential>
            <antcall target="lint"/>
            <antcall target="phpcpd-ci"/>
            <antcall target="phpcs-ci"/>
            <antcall target="phploc-ci"/>
        </parallel>
    </target>

    <target name="clean"
            unless="clean.done"
            description="Cleanup build artifacts">
        <delete dir="${basedir}/build/api"/>
        <delete dir="${basedir}/build/coverage"/>
        <delete dir="${basedir}/build/logs"/>
        <delete dir="${basedir}/build/pdepend"/>
        <delete dir="${basedir}/vendor"/>
        <antcall target="drop-database"/>
        <property name="clean.done" value="true"/>
    </target>

    <target name="database" depends="clean">
        <antcall target="create-database"/>
        <antcall target="migrate-database"/>
    </target>

    <target name="prepare"
            unless="prepare.done"
            depends="clean"
            description="Prepare for build">
        <mkdir dir="${basedir}/build/api"/>
        <mkdir dir="${basedir}/build/coverage"/>
        <mkdir dir="${basedir}/build/logs"/>
        <mkdir dir="${basedir}/build/pdepend"/>
        <delete dir="${basedir}/tests/_output"/>
        <antcall target="composer-install"/>
        <antcall target="composer-dumpautoload"/>
        <property name="prepare.done" value="true"/>
    </target>

    <target name="composer-install" description="Installing dependencies">
        <echo>Installing dependencies</echo>
        <exec executable="composer" failonerror="true">
            <arg value="install" />
            <arg value="--prefer-dist" />
        </exec>
    </target>

    <target name="composer-dumpautoload" description="Generate autoload files">
        <echo>Generating autoload files</echo>
        <exec executable="composer" failonerror="true">
            <arg value="dumpautoload" />
        </exec>
    </target>


    <target name="create-database">
        <exec dir="${basedir}/" executable="mysql" failonerror="true">
            <arg line="-u test -ptest -e 'create database ${databasename}'" />
        </exec>
    </target>

    <target name="drop-database">
        <exec dir="${basedir}/" executable="mysql" failonerror="false">
            <arg line="-u test -ptest -e 'drop database ${databasename}'" />
        </exec>
    </target>

    <target name="migrate-database">
        <exec dir="${basedir}/" executable="php" failonerror="true">
            <arg value="artisan" />
            <arg value="migrate" />
            <arg value="--force" />
            <arg value="--seed" />
        </exec>
    </target>

    <target name="codeception" description="Start Codeception Tests">
        <parallel>
            <sequential>
                <antcall target="start-server" />
            </sequential>
            <sequential>
                <waitfor maxwait="10" maxwaitunit="second">
                    <socket server="localhost" port="8021" />
                </waitfor>
                <exec dir="${basedir}/" executable="php" failonerror="true">
                    <arg line="codecept run acceptance --html --no-ansi" />
                </exec>
            </sequential>
        </parallel>
    </target>

    <target name="start-server">
        <exec dir="${basedir}/" executable="php" spawn="true">
            <arg line="artisan serv" />
            <arg line="--port" />
            <arg line="8022" />
        </exec>
    </target>

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

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

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

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

    <target name="phploc"
            unless="phploc.done"
            description="Measure project size using PHPLOC and print human readable output. Intended for usage on the command line.">
        <exec executable="${phploc}" taskname="phploc">
            <arg value="--count-tests" />
            <arg path="${basedir}/app" />
            <arg path="${basedir}/tests" />
        </exec>

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

    <target name="phploc-ci"
            unless="phploc.done"
            depends="prepare"
            description="Measure project size using PHPLOC and log result in CSV and XML format. Intended for usage within a continuous integration environment.">
        <exec executable="${phploc}" taskname="phploc">
            <arg value="--count-tests" />
            <arg value="--log-csv" />
            <arg path="${basedir}/build/logs/phploc.csv" />
            <arg value="--log-xml" />
            <arg path="${basedir}/build/logs/phploc.xml" />
            <arg path="${basedir}/app" />
            <arg path="${basedir}/tests" />
        </exec>

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

    <target name="pdepend"
            unless="pdepend.done"
            depends="prepare"
            description="Calculate software metrics using PHP_Depend and log result in XML format. Intended for usage within a continuous integration environment.">
        <exec executable="${pdepend}" taskname="pdepend">
            <arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
            <arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.svg" />
            <arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.svg" />
            <arg path="${basedir}/app" />
        </exec>

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

    <target name="phpmd"
            unless="phpmd.done"
            description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing.">
        <exec executable="${phpmd}" taskname="phpmd">
            <arg path="${basedir}/app" />
            <arg value="text" />
            <arg path="${basedir}/build/phpmd.xml" />
        </exec>

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

    <target name="phpmd-ci"
            unless="phpmd.done"
            depends="prepare"
            description="Perform project mess detection using PHPMD and log result in XML format. Intended for usage within a continuous integration environment.">
        <exec executable="${phpmd}" taskname="phpmd">
            <arg path="${basedir}/app" />
            <arg value="xml" />
            <arg path="${basedir}/build/phpmd.xml" />
            <arg value="--reportfile" />
            <arg path="${basedir}/build/logs/pmd.xml" />
        </exec>

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

    <target name="phpcs"
            unless="phpcs.done"
            description="Find coding standard violations using PHP_CodeSniffer and print human readable output. Intended for usage on the command line before committing.">
        <exec executable="${phpcs}" taskname="phpcs">
            <arg value="--standard=PSR2" />
            <arg value="--extensions=php" />
            <arg value="--ignore=autoload.php" />
            <arg path="${basedir}/app" />
            <arg path="${basedir}/tests" />
        </exec>

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

    <target name="phpcs-ci"
            unless="phpcs.done"
            depends="prepare"
            description="Find coding standard violations using PHP_CodeSniffer and log result in XML format. Intended for usage within a continuous integration environment.">
        <exec executable="${phpcs}" output="/dev/null" taskname="phpcs">
            <arg value="--report=checkstyle" />
            <arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
            <arg value="--standard=PSR2" />
            <arg value="--extensions=php" />
            <arg value="--ignore=autoload.php" />
            <arg path="${basedir}/app" />
            <arg path="${basedir}/tests" />
        </exec>

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

    <target name="phpcpd"
            unless="phpcpd.done"
            description="Find duplicate code using PHPCPD and print human readable output. Intended for usage on the command line before committing.">
        <exec executable="${phpcpd}" taskname="phpcpd">
            <arg path="${basedir}/app" />
        </exec>

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

    <target name="phpcpd-ci"
            unless="phpcpd.done"
            depends="prepare"
            description="Find duplicate code using PHPCPD and log result in XML format. Intended for usage within a continuous integration environment.">
        <exec executable="${phpcpd}" taskname="phpcpd">
            <arg value="--log-pmd" />
            <arg path="${basedir}/build/logs/pmd-cpd.xml" />
            <arg path="${basedir}/app" />
        </exec>

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

    <target name="phpunit"
            unless="phpunit.done"
            depends="prepare"
            description="Run unit tests with PHPUnit">
        <exec executable="${phpunit}" resultproperty="result.phpunit" taskname="phpunit">
            <arg value="--coverage-html" />
            <arg value="build/report" />
            <arg value="--coverage-clover" />
            <arg value="build/coverage.xml" />
        </exec>

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

    <target name="phpunit-no-coverage"
            unless="phpunit.done"
            depends="prepare"
            description="Run unit tests with PHPUnit (without generating code coverage reports)">
        <exec executable="${phpunit}" failonerror="true" taskname="phpunit">
            <arg value="--configuration"/>
            <arg path="${basedir}/phpunit.xml"/>
            <arg value="--no-coverage"/>
        </exec>

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

</project>