<?xml version="1.0" encoding="UTF-8"?>
<project name="pim" default="build"
>
    <property name="symfony.catalog.storage.dir" value="catalog" />

    <target name="build" depends="prepare,composer,vendors,setupdb,fixtures,lint,phploc,pdepend,phpmd-ci,phpcs-ci,phpcpd,phpdox,phpunit,phpspec,phpcb"/>

    <target name="build-phpunit" depends="prepare,composer,require-mongo,vendors-no-scripts,phpunit,phpspec,phpspec-junit"/>

    <target name="build-coverage" depends="prepare,composer,require-mongo,coverage"/>

    <target name="build-behat" depends="prepare,composer,vendors,install-all-behat,behat"/>

    <target name="build-behat-deprecated" depends="prepare-deprecated-behat,prepare,composer,vendors,install-all-behat,behat"/>

    <target name="build-behat-mongo" depends="prepare,composer,require-mongo,activate-mongo,vendors,install-all-behat,behat"/>

    <target name="build-code-quality" depends="prepare,lint,phploc,pdepend,phpmd-ci,php-cs-fixer,phpcpd,phpdoc"/>

    <target name="prepare-deprecated-behat" description="Build deprecated behats">
        <!--
            In order to keep old import behats with deprecated processor, writer, and reader, we delete
            the new config file and replace this one with the old one, and we also delete the one in
            connector bundle to avoid this one to be also called.
        -->
        <delete file="${basedir}/src/Pim/Bundle/ConnectorBundle/Resources/config/batch_jobs.yml" />
        <delete file="${basedir}/src/Pim/Bundle/BaseConnectorBundle/Resources/config/batch_jobs.yml" />
        <copy file="${basedir}/features/Context/fixtures/base_connector_deprecated_batch_jobs.yml" tofile="${basedir}/src/Pim/Bundle/BaseConnectorBundle/Resources/config/batch_jobs.yml" />
        <copy file="${basedir}/features/Context/fixtures/connector_deprecated_batch_jobs.yml" tofile="${basedir}/src/Pim/Bundle/ConnectorBundle/Resources/config/batch_jobs.yml" />
    </target>

    <target name="clean" description="Cleanup build artifacts">
        <delete dir="${basedir}/app/build/api"/>
        <delete dir="${basedir}/app/build/code-browser"/>
        <delete dir="${basedir}/app/build/coverage"/>
        <delete dir="${basedir}/app/build/coverage-parts"/>
        <delete dir="${basedir}/app/build/logs"/>
        <delete dir="${basedir}/app/build/pdepend"/>

        <delete file="${basedir}/composer.phar" />
        <delete file="${basedir}/app/config/parameters.yml" />
        <delete file="${basedir}/app/config/parameters_test.yml" />
        <delete dir="${basedir}/app/cache" />
    </target>

    <target name="prepare" depends="clean" description="Prepare for build">
        <mkdir dir="${basedir}/app/build/api"/>
        <mkdir dir="${basedir}/app/build/code-browser"/>
        <mkdir dir="${basedir}/app/build/coverage"/>
        <mkdir dir="${basedir}/app/build/coverage-parts"/>
        <mkdir dir="${basedir}/app/build/logs"/>
        <mkdir dir="${basedir}/app/build/logs/phpunit"/>
        <mkdir dir="${basedir}/app/build/logs/phpspec"/>
        <mkdir dir="${basedir}/app/build/logs/behat"/>
        <mkdir dir="${basedir}/app/build/logs/phpdoc"/>
        <mkdir dir="${basedir}/app/build/pdepend"/>
        <mkdir dir="${basedir}/app/build/phpdox"/>

        <mkdir dir="${basedir}/app/cache" />
        <!--
            We still need a parameters.yml otherwise composer will create it without comments,
            thus raising a "You have requested a non-existent parameter "mongodb_database"." error.
        -->
        <copy file="${basedir}/app/config/parameters.yml.dist" tofile="${basedir}/app/config/parameters.yml" />
        <copy file="${basedir}/app/config/parameters.yml.dist" tofile="${basedir}/app/config/parameters_test.yml" />
        <replaceregexp match="database_name:.*$" replace="database_name: %database.name%" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="parameters*.yml" />
            </fileset>
        </replaceregexp>
        <replaceregexp match="database_user:.*$" replace="database_user: %database.user%" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="parameters*.yml" />
            </fileset>
        </replaceregexp>
        <replaceregexp match="database_password:.*$" replace="database_password: %database.password%" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="parameters*.yml" />
            </fileset>
        </replaceregexp>
        <replaceregexp match="catalog_storage_dir:.*$" replace="catalog_storage_dir: %kernel.root_dir%/file_storage/%catalog.storage.dir%" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="pim_parameters.yml" />
            </fileset>
        </replaceregexp>
        <replaceregexp match="installer_data:.*$" replace="installer_data: PimInstallerBundle:minimal" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="pim_parameters.yml" />
            </fileset>
        </replaceregexp>
        <copy file="${basedir}/behat.yml.dist" tofile="${basedir}/behat.yml" overwrite="true"/>
        <replaceregexp match="pim-behat" replace="${behat.base_url}" byline="true">
            <fileset dir="${basedir}">
                <include name="behat.yml" />
            </fileset>
        </replaceregexp>
        <replaceregexp match="tags: .*$" replace="tags: ${behat.tags}" byline="true">
            <fileset dir="${basedir}">
                <include name="behat.yml" />
            </fileset>
        </replaceregexp>
        <condition property="hasBuildTags">
            <and>
                <isset property="buildTags" />
                <length string="${buildTags}" trim="true" when="greater" length="0"/>
            </and>
        </condition>
        <replaceregexp match="tags: (.*)$" replace="tags: \1&amp;&amp;${buildTags}" byline="true">
            <fileset dir="${basedir}">
                <include name="behat.yml" if="hasBuildTags" />
            </fileset>
        </replaceregexp>
        <copy file="${basedir}/phpdoc.xml.dist" tofile="${basedir}/phpdoc.xml" />
    </target>

    <target name="lint" description="Perform syntax check of sourcecode files">
        <apply executable="php" failonerror="true">
            <arg value="-l" />

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

    <target name="check-interfaces" description="Check if the interfaces are being used">
        <exec executable="${basedir}/app/Resources/jenkins/interface_check.sh" failonerror="true">
            <arg value="${basedir}/src" />
        </exec>
        <exec executable="${basedir}/app/Resources/jenkins/interface_check.sh" failonerror="false">
            <arg value="${basedir}/spec" />
        </exec>
    </target>

    <target name="phploc" description="Measure project size using PHPLOC">
        <exec executable="phploc">
            <arg value="--log-csv" />
            <arg value="${basedir}/app/build/logs/phploc.csv" />
            <arg path="${basedir}/src" />
        </exec>
    </target>

    <target name="pdepend" description="Calculate software metrics using PHP_Depend">
        <exec executable="pdepend">
            <arg value="--jdepend-xml=${basedir}/app/build/logs/jdepend.xml" />
            <arg value="--jdepend-chart=${basedir}/app/build/pdepend/dependencies.svg" />
            <arg value="--overview-pyramid=${basedir}/app/build/pdepend/overview-pyramid.svg" />
            <arg value="--ignore=Tests" />
            <arg path="${basedir}/src" />
        </exec>
    </target>

    <target name="phpmd" description="Perform project mess detection using PHPMD and print human readable output. Intended for usage on the command line before committing.">
        <exec executable="${basedir}/app/Resources/jenkins/phpmd_akeneo">
            <arg path="${basedir}/src,${basedir}/features" />
            <arg value="text" />
        </exec>
    </target>

    <target name="phpmd-ci" description="Perform project mess detection using PHPMD creating a log file for the continuous integration server">
        <exec executable="${basedir}/app/Resources/jenkins/phpmd_akeneo">
            <arg path="${basedir}/src" />
            <arg value="xml" />
            <arg value="--reportfile" />
            <arg value="${basedir}/app/build/logs/pmd.xml" />
        </exec>
    </target>

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

    <target name="php-cs-fixer" description="Find coding standard violations using PHP_CodeSniffer and print human readable output. Intended for usage on the command line before committing.">
        <exec executable="bash">
            <arg value="-c"/>
            <arg value="curl http://get.sensiolabs.org/php-cs-fixer.phar -o ${basedir}/bin/php-cs-fixer"/>
        </exec>

        <exec executable="bash">
            <arg value="-c"/>
            <arg value="chmod a+x ${basedir}/bin/php-cs-fixer"/>
        </exec>

        <exec executable="${basedir}/bin/php-cs-fixer" failonerror="true">
            <arg value="fix" />
            <arg path="${basedir}/src" />
            <arg value="-v" />
            <arg value="--config-file=${basedir}/.php_cs.local.php" />
            <arg value="--dry-run" />
        </exec>
    </target>

    <target name="phpdoc" description="Check php documentation">
        <exec executable="phpdoc">
            <arg line="--directory ${basedir}/src" />
            <arg line="--target ${basedir}/build/api" />
            <arg line="--template checkstyle" />
        </exec>
    </target>

    <target name="phpcs-ci" description="Find coding standard violations using PHP_CodeSniffer creating a log file for the continuous integration server">
        <exec executable="phpcs" output="/dev/null">
            <arg value="--report=checkstyle" />
            <arg value="--report-file=${basedir}/app/build/logs/checkstyle.xml" />
            <arg value="--standard=PSR2" />
            <arg value="--extensions=php" />
            <arg path="${basedir}/src" />
            <arg path="${basedir}/features" />
        </exec>
    </target>

    <target name="phpcpd" description="Find duplicate code using PHPCPD">
        <exec executable="phpcpd">
            <arg value="--exclude" />
            <arg value="Tests" />
            <arg value="--log-pmd" />
            <arg value="${basedir}/app/build/logs/pmd-cpd.xml" />
            <arg path="${basedir}/src" />
        </exec>
    </target>

    <target name="phpdox" description="Generate API documentation using phpDox">
        <exec executable="phpdox">
            <arg value="-f" />
            <arg path="${basedir}/app/Resources/jenkins/phpdox.xml" />
        </exec>
    </target>

    <target name="coverage" description="Run unit tests with PHPUnit and generate coverage">
        <exec executable="sh" outputproperty="xdebug_path">
            <arg value="-c" />
            <arg value="php -i | grep extension_dir | cut -d ' ' -f3"/>
        </exec>

        <exec executable="${basedir}/composer.phar" failonerror="true">
            <arg value="require" />
            <arg value="--no-update" />
            <arg value="henrikbjorn/phpspec-code-coverage" />
            <arg value="1.0.*@dev" />
        </exec>

        <!--
            As long as composer does not provide an option to ignore platform dependency,
            it must be executed with the xdebug extension activated
        -->
        <exec executable="php" failonerror="true">
            <arg value="-d zend_extension=${xdebug_path}/xdebug.so" />
            <arg value="${basedir}/composer.phar" />
            <arg value="install" />
            <arg value="--no-progress" />
            <arg value="--no-interaction" />
            <arg value="--prefer-dist" />
            <arg value="--optimize-autoloader" />
            <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
            <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
            <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
            <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
            <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>

        <exec executable="php">
            <arg value="-d zend_extension=${xdebug_path}/xdebug.so" />
            <arg value="${basedir}/bin/phpunit" />
            <arg value="-c" />
            <arg path="${basedir}/app/phpunit.jenkins-coverage.xml" />
            <arg value="--testsuite=Jenkins_PIM_Test" />
        </exec>

        <exec executable="php">
            <arg value="-d zend_extension=${xdebug_path}/xdebug.so" />
            <arg value="${basedir}/bin/phpspec" />
            <arg value="run" />
            <arg value="-c" />
            <arg path="${basedir}/app/phpspec.jenkins-coverage.yml" />
        </exec>

        <exec executable="php">
            <arg value="-d zend_extension=${xdebug_path}/xdebug.so" />
            <arg value="${basedir}/bin/merge-coverage" />
            <arg value="${basedir}/app/build/coverage-parts/phpunit.php" />
            <arg value="${basedir}/app/build/coverage-parts/phpspec.php" />
        </exec>
    </target>

    <target name="phpunit" description="Run unit tests with PHPUnit">
        <exec executable="${basedir}/bin/phpunit">
            <arg value="-c" />
            <arg path="${basedir}/app/phpunit.jenkins.xml" />
            <arg value="--testsuite=Jenkins_PIM_Test" />
        </exec>
    </target>

    <target name="phpspec" description="Run specs with PhpSpec Dot formatter">
        <exec executable="./phpspec-fix">
            <arg value="-fdot" />
            <arg value="--no-interaction" />
            <arg value="--ansi" />
        </exec>
    </target>

    <target name="phpspec-junit" description="Run specs with PhpSpec JUnit formatter">
        <exec executable="./phpspec-fix" output="app/build/logs/phpspec/junit.xml">
            <arg value="-fjunit" />
            <arg value="--no-interaction" />
        </exec>
    </target>

    <target name="link-pim-docs" description="Clone pim-docs repo and configure pim-dev to work with">
        <exec executable="${basedir}/app/Resources/jenkins/pim-docs/link-pim-docs.sh" failonerror="true" />
    </target>

    <target name="behat" description="Run acceptance tests with Behat">
        <exec executable="${basedir}/app/Resources/jenkins/behat/run_behat.sh" failonerror="true">
            <arg value="-c" />
            <arg value="${behat.concurrency}" />
            <arg value="-x" />
            <arg value="${behat.xdebug}" />
            <arg value="-d" />
            <arg value="${symfony.database.name}_" />
            <arg value="-p" />
            <arg value="${behat.profile.prefix}-" />
            <arg value="-f"/>
            <arg value="${behat.features} "/>
            <arg value="-b"/>
            <arg value="${basedir}/bin/behat ${behat.options}"/>
            <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
            <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
            <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
            <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
            <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
            <env key="BEHAT_CONTEXT" value="JENKINS" />
        </exec>
    </target>

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

    <target name="composer" description="Install composer.phar">
        <exec executable="bash">
            <arg value="-c"/>
            <arg value="curl -sS https://getcomposer.org/installer | php"/>
        </exec>
    </target>

    <target name="require-mongo" description="Require the mongo bundle">
        <exec executable="${basedir}/composer.phar" failonerror="true">
            <arg value="require" />
            <arg value="--no-update" />
            <arg value="doctrine/mongodb-odm-bundle" />
            <arg value="v3.0.1" />
        </exec>
    </target>

    <target name="vendors" description="Install vendors">
        <exec executable="${basedir}/composer.phar" failonerror="true">
            <arg value="install" />
            <arg value="--no-progress" />
            <arg value="--no-interaction" />
            <arg value="--prefer-dist" />
            <arg value="--optimize-autoloader" />
            <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
            <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
            <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
            <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
            <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>
    </target>

    <target name="vendors-no-scripts" description="Install vendors">
        <exec executable="${basedir}/composer.phar" failonerror="true">
            <arg value="install" />
            <arg value="--no-progress" />
            <arg value="--no-interaction" />
            <arg value="--prefer-dist" />
            <arg value="--optimize-autoloader" />
            <arg value="--no-scripts" />
        </exec>
    </target>

    <target name="activate-mongo" description="Activate the mongo bundle">
        <replace
            file="${basedir}/app/AppKernel.php"
            token="// new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle()"
            value="new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle()"
            failOnNoReplacements="true"
        />

        <replaceregexp match="# mongodb" replace="mongodb" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="pim_parameters*" />
            </fileset>
        </replaceregexp>

        <replaceregexp match="mongodb_database:.*$" replace="mongodb_database: %mongodb.database%" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="pim_parameters*" />
            </fileset>
        </replaceregexp>

        <replaceregexp match="pim_catalog_product_storage_driver: doctrine/orm" replace="pim_catalog_product_storage_driver: doctrine/mongodb-odm" byline="true">
            <fileset dir="${basedir}/app/config/">
                <include name="pim_parameters*" />
            </fileset>
        </replaceregexp>
    </target>

    <target name="setupdb" description="Setup db">
        <exec executable="app/console" failonerror="true">
          <arg value="pim:install" />
          <arg value="--force" />
          <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
          <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
          <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
          <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
          <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>
    </target>

    <target name="setupdb-test" description="Setup db for test env">
        <exec executable="app/console" failonerror="true">
          <arg value="pim:install" />
          <arg value="--env=test" />
          <arg value="--force" />
          <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
          <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
          <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
          <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
          <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>
    </target>

    <target name="install-all-behat" description="Setup db and install assets for Behat">
        <exec executable="app/console" failonerror="true">
          <arg value="pim:install" />
          <arg value="--env=behat" />
          <arg value="--force" />
          <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
          <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
          <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
          <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
          <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>
    </target>

    <target name="fixtures" description="Load fixtures">
        <exec executable="app/console">
          <arg value="doctrine:fixtures:load" />
          <arg value="--no-interaction" />
          <env key="SYMFONY__DATABASE__NAME" value="${symfony.database.name}" />
          <env key="SYMFONY__DATABASE__USER" value="${symfony.database.user}" />
          <env key="SYMFONY__DATABASE__PASSWORD" value="${symfony.database.password}" />
          <env key="SYMFONY__MONGODB__DATABASE" value="${symfony.mongodb.database}" />
          <env key="SYMFONY__CATALOG__STORAGE__DIR" value="${symfony.catalog.storage.dir}" />
        </exec>
    </target>

</project>
