<?xml version="1.0" encoding="UTF-8"?>
<project name="aws-sdk-for-php" default="test">

    <property name="dir.output" value="${project.basedir}/build/artifacts" />
    <property name="coverage" value="false" />
    <property name="mock" value="false" />
    <property name="min" value="false" />
    <property name="sdk_url" value="http://aws.amazon.com/sdkforphp2" />

    <fileset id="src_files" dir="${project.basedir}/src" includes="**/*.php" />

    <target name="test" description="Run unit tests" depends="test-init">
        <exec passthru="true" command="vendor/bin/phpunit" />
    </target>

    <target name="integration" description="Run integration tests">
        <if>
            <isset property="service" />
            <then>
                <property name="testpath" value="tests/Aws/Tests/${service}" />
            </then>
            <else>
              <property name="testpath" value="" />
            </else>
        </if>
        <if>
            <available file="phpunit.functional.xml" />
            <then>
                <if>
                    <equals arg1="${mock}" arg2="true" />
                    <then>
                        <echo>php -d mock=true `which phpunit` -c phpunit.functional.xml</echo>
                        <exec passthru="true" command="php -d mock=true `which phpunit` -c phpunit.functional.xml ${testpath}" />
                    </then>
                    <else>
                        <exec passthru="true" command="phpunit -c phpunit.functional.xml ${testpath}" />
                    </else>
                </if>
            </then>
            <else>
                <fail>You must copy phpunit.functional.dist to phpunit.functional.xml and modify the appropriate property settings</fail>
            </else>
        </if>
    </target>

    <target name="create-staging" description="Creates a staging directory for zip and phar creation">

        <delete dir="${dir.output}/staging" failonerror="false" quiet="true"/>
        <mkdir dir="${dir.output}/staging"/>
        <mkdir dir="${dir.output}/staging/Aws"/>
        <mkdir dir="${dir.output}/staging/Guzzle"/>
        <mkdir dir="${dir.output}/staging/Doctrine/Common/Cache"/>
        <mkdir dir="${dir.output}/staging/Symfony"/>
        <mkdir dir="${dir.output}/staging/Monolog"/>

        <patternset id="sdk-files">
            <include name="**/*.php" />
            <include name="**/*.pem" />
            <include name="**/*.md5" />
            <include name="**/LICENSE*" />
        </patternset>

        <!-- Copy AWS deps -->
        <copy file="${project.basedir}/build/aws-autoloader.php" tofile="${dir.output}/staging/aws-autoloader.php"/>
        <copy todir="${dir.output}/staging">
            <fileset dir="src">
                <patternset refid="sdk-files"/>
            </fileset>
        </copy>
        <copy file="${project.basedir}/LICENSE.md" tofile="${dir.output}/staging/Aws/LICENSE.md"/>
        <copy file="${project.basedir}/NOTICE.md" tofile="${dir.output}/staging/Aws/NOTICE.md"/>

        <!-- Copy Symfony dependencies -->
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/symfony/event-dispatcher">
                <include name="**/*.php" />
            </fileset>
        </copy>
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/symfony/class-loader">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <!-- Copy Guzzle deps -->
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/guzzle/guzzle/src">
                <patternset refid="sdk-files"/>
            </fileset>
        </copy>

        <!-- Copy Monolog deps -->
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/monolog/monolog/src">
                <patternset refid="sdk-files"/>
            </fileset>
        </copy>

        <!-- Copy PSR deps -->
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/psr/log">
                <include name="**/*.php" />
            </fileset>
        </copy>

        <!-- Copy Doctrine deps -->
        <copy todir="${dir.output}/staging">
            <fileset dir="vendor/doctrine/cache/lib">
                <patternset refid="sdk-files"/>
            </fileset>
        </copy>

    </target>

    <target name="phar" depends="create-staging" description="Create a phar with an autoloader">
        <pharpackage destfile="build/aws.phar" stub="build/phar-stub.php" basedir="${dir.output}/staging">
            <fileset dir="${dir.output}/staging">
                <include name="**/**"/>
            </fileset>
            <metadata>
                <element name="link" value="${sdk_url}" />
            </metadata>
        </pharpackage>
    </target>

    <target name="zip" depends="create-staging" description="Create a ZIP file containing the SDK and its dependencies">
        <zip destfile="build/aws.zip" basedir="${dir.output}/staging">
            <fileset dir="${dir.output}/staging">
                <include name="**/**"/>
            </fileset>
        </zip>
    </target>

    <target name="test-init" description="Initialize test dependencies">
        <copy file="phpunit.xml.dist" tofile="phpunit.xml" overwrite="false" />
        <copy file="phpunit.functional.xml.dist" tofile="phpunit.functional.xml" overwrite="false" />
        <copy file="test_services.json.dist" tofile="test_services.json" overwrite="false" />
    </target>

    <target name="clean" description="Deletes build artifacts">
        <delete dir="${dir.output}"/>
    </target>

    <target name="prepare" depends="clean,test-init">
        <mkdir dir="${dir.output}"/>
        <mkdir dir="${dir.output}/logs" />
    </target>

    <target name="clean-dependencies" description="Deletes all dependencies downloaded by Composer">
        <delete dir="${project.basedir}/vendor"/>
        <delete file="composer.lock" />
    </target>

    <target name="update-dependencies" description="Updates Composer dependencies">
        <exec command="php composer.phar update --dev" passthru="true" />
    </target>

    <target name="coverage">
        <if>
            <isset property="service" />
            <then>
                <property name="testpath" value="tests/Aws/Tests/${service}" />
            </then>
            <else>
              <property name="testpath" value="" />
            </else>
        </if>
        <mkdir dir="${dir.output}/logs" />
        <exec passthru="true" command="phpunit --coverage-html=${dir.output}/coverage --coverage-clover=${dir.output}/logs/clover.xml ${testpath}" />
    </target>

    <target name="view-coverage">
        <exec passthru="true" command="open ${dir.output}/coverage/index.html" />
    </target>

    <target name="phpdepend">
        <delete dir="${dir.output}/pdepend" includeemptydirs="true" verbose="true" failonerror="true" />
        <mkdir dir="${dir.output}/pdepend" />
        <phpdepend>
            <fileset refid="src_files" />
            <analyzer type="coderank-mode" value="method"/>
            <logger type="jdepend-chart" outfile="${dir.output}/pdepend/jdepend-chart.svg" />
            <logger type="overview-pyramid" outfile="${dir.output}/pdepend/overview-pyramid.svg" />
            <logger type="jdepend-chart" outfile="${dir.output}/pdepend/jdepend-chart.png" />
            <logger type="overview-pyramid" outfile="${dir.output}/pdepend/overview-pyramid.png" />
            <logger type="jdepend-xml" outfile="${dir.output}/pdepend/jdepend.xml" />
            <logger type="phpunit-xml" outfile="${dir.output}/pdepend/phpunit.xml" />
            <logger type="summary-xml" outfile="${dir.output}/pdepend/summary.xml" />
        </phpdepend>
    </target>

    <target name="phpcs">
        <delete dir="${dir.output}/phpcs" includeemptydirs="true" verbose="true" failonerror="true" />
        <mkdir dir="${dir.output}/phpcs" />
        <!-- <phpcodesniffer></phpcodesniffer> -->
    </target>

    <target name="phpmd">
        <delete dir="${dir.output}/phpmd" includeemptydirs="true" verbose="true" failonerror="true" />
        <mkdir dir="${dir.output}/phpmd" />
        <phpmd>
            <fileset refid="src_files" />
            <formatter type="html" outfile="${dir.output}/phpmd/phpmd.html"/>
            <formatter type="xml" outfile="${dir.output}/phpmd/phpmd.xml"/>
        </phpmd>
    </target>

    <target name="phpcpd">
        <delete dir="${dir.output}/phpcpd" includeemptydirs="true" verbose="true" failonerror="true" />
        <mkdir dir="${dir.output}/phpcpd" />
        <phpcpd>
            <fileset refid="src_files" />
            <formatter type="pmd" outfile="${dir.output}/phpcpd/pmd.xml" />
            <formatter type="default" outfile="${dir.output}/phpcpd/default.xml" />
        </phpcpd>
    </target>

    <target name="phploc">
        <exec command="phploc --log-csv ${dir.output}/logs/phploc.csv ." dir="${project.basedir}/src" passthru="true" />
    </target>

    <target name="phplint">
        <phplint>
            <fileset refid="src_files" />
        </phplint>
    </target>

    <target name="phpcb" description="Aggregate tool output with PHP_CodeBrowser">
        <exec executable="phpcb">
            <arg value="--log" />
            <arg path="${dir.output}/logs" />
            <arg value="--source" />
            <arg path="${project.basedir}/src" />
            <arg value="--output" />
            <arg path="${dir.output}/code-browser" />
        </exec>
    </target>

    <target name="install-build-deps">
        <exec command="pear install --alldeps pear.phpunit.de/PHPUnit" passthru="true" />
        <exec command="pear install --alldeps phpunit/PHP_CodeBrowser" passthru="true" />
        <exec command="pear install --alldeps phpunit/phploc" passthru="true" />
        <exec command="pear install --alldeps pear.pdepend.org/PHP_Depend-beta" passthru="true" />
        <exec command="pear install --alldeps pear.phpmd.org/PHP_PMD" passthru="true" />
        <exec command="pear install --alldeps pear.phpunit.de/phpcpd" passthru="true" />
        <exec command="pear install --alldeps PHP_CodeSniffer" passthru="true" />
        <exec command="pear install --alldeps pear.phing.info/phing" passthru="true" />
    </target>

    <target name="all" depends="clean, prepare, test-init, build, report"/>
    <target name="build" depends="phplint, prepare, test-init, test, phar"/>
    <target name="report" depends="coverage, phploc, phpcs, phpmd, phpcpd, phpdepend, phpcb"/>

</project>
