<?xml version="1.0" encoding="UTF-8"?>
<project name="phpcpd" default="setup">
    <target name="setup" depends="clean,install-dependencies"/>

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

    <target name="install-dependencies" unless="dependencies-installed" depends="-dependencies-installed" description="Install dependencies with Composer">
        <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>
    </target>

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

    <target name="signed-phar" description="Create signed PHAR archive of PHPCPD and all its dependencies (release)" depends="phar">
        <exec executable="bash" outputproperty="version">
            <arg value="-c" />
            <arg value="${basedir}/phpcpd --version | awk 'BEGIN { ORS = &quot;&quot;; } {print $2}'" />
        </exec>

        <exec executable="gpg" failonerror="true">
            <arg value="--armor" />
            <arg value="--detach-sign" />
            <arg path="${basedir}/build/phpcpd-${version}.phar" />
        </exec>
    </target>

    <target name="phar" description="Create PHAR archive of phpcpd and all its dependencies" depends="setup,phar-build">
    </target>

    <target name="phar-build">
        <exec executable="bash" outputproperty="version">
            <arg value="-c" />
            <arg value="${basedir}/phpcpd --version | awk 'BEGIN { ORS = &quot;&quot;; } {print $2}'" />
        </exec>

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

        <copy file="${basedir}/vendor/sebastian/cli-parser/LICENSE" tofile="${basedir}/build/phar/sebastian-cli-parser/LICENSE"/>
        <copy todir="${basedir}/build/phar/sebastian-cli-parser">
            <fileset dir="${basedir}/vendor/sebastian/cli-parser/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/phpunit/php-file-iterator/LICENSE" tofile="${basedir}/build/phar/phpunit-php-file-iterator/LICENSE"/>
        <copy todir="${basedir}/build/phar/phpunit-php-file-iterator">
            <fileset dir="${basedir}/vendor/phpunit/php-file-iterator/src">
                <include name="**/*.php" />
            </fileset>
        </copy>

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

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

        <exec executable="${basedir}/tools/phpab">
            <arg value="--all" />
            <arg value="--phar" />
            <arg value="--output" />
            <arg path="${basedir}/build/phpcpd-${version}.phar" />
            <arg value="--template" />
            <arg path="${basedir}/build/phar-autoload.php.in" />
            <arg value="--indent" />
            <arg value="            " />
            <arg path="${basedir}/build/phar" />
        </exec>

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

        <delete dir="${basedir}/build/phar"/>
    </target>

    <target name="-dependencies-installed">
        <available file="${basedir}/vendor" property="dependencies-installed" type="dir"/>
    </target>
</project>
