<?xml version="1.0" encoding="UTF-8"?>
<project name="Invition Print &amp; Ship" default="build" basedir=".">
	<target name="build"
		depends="prepare,lint,phploc,pdepend,phpmd-ci,phpcs-ci,phpcpd,phpdox,phpcb,zip-artifacts" />

	<target name="build-parallel"
		depends="prepare,lint,tools-parallel" />

	<target name="tools-parallel" description="Run tools in parallel">
		<parallel threadCount="2">
			<sequential>
				<antcall target="pdepend" />
				<antcall target="phpmd-ci" />
			</sequential>

			<antcall target="phpcpd" />
			<antcall target="phpcs-ci" />
			<antcall target="phploc" />
			<antcall target="phpdox" />
		</parallel>
	</target>

	<target name="clean" description="Cleanup build artifacts">
		<delete dir="${basedir}/build/api" />
		<delete dir="${basedir}/build/deliver" />
		<delete dir="${basedir}/build/logs" />
		<delete dir="${basedir}/build/pdepend" />
		<delete dir="${basedir}/build/phpcb" />
		<delete dir="${basedir}/build/phpdox" />
	</target>

	<target name="prepare" depends="clean" description="Prepare for build">
		<mkdir dir="${basedir}/build/api" />
		<mkdir dir="${basedir}/build/deliver" />
		<mkdir dir="${basedir}/build/logs" />
		<mkdir dir="${basedir}/build/pdepend" />
		<mkdir dir="${basedir}/build/phpcb" />
		<mkdir dir="${basedir}/build/phpdox" />
	</target>

	<target name="lint" description="Perform syntax check of sourcecode files">
		<apply executable="php" failonerror="true">
			<arg value="-l" />

			<fileset dir="${basedir}">
				<include name="**/*.php" />
				<modified />
			</fileset>
		</apply>
	</target>

	<target name="phploc" depends="prepare" description="Measure project size using PHPLOC">
		<exec executable="phploc" failonerror="false">
            <arg line="--count-tests" />
			<arg value="--log-xml" />
			<arg value="${basedir}/build/logs/phploc.xml" />
			<arg path="${basedir}" />
		</exec>
        <echo message="##teamcity[publishArtifacts '${basedir}/build/logs/phploc.xml']" />
	</target>

	<target name="pdepend" depends="prepare" description="Calculate software metrics using PHP_Depend">
		<exec executable="pdepend" failonerror="false">
			<arg value="--jdepend-xml=${basedir}/build/logs/jdepend.xml" />
			<arg value="--jdepend-chart=${basedir}/build/pdepend/dependencies.png" />
			<arg value="--overview-pyramid=${basedir}/build/pdepend/overview-pyramid.png" />
			<arg value="--summary-xml=${basedir}/build/logs/pdepend.summary.xml" />
			<arg value="--ignore=${basedir}/lib/" />
			<arg path="${basedir}" />
		</exec>
		<echo file="${basedir}/build/pdepend/pdepend.html">
			<![CDATA[
				<img src="dependencies.png" />
				<img src="overview-pyramid.png" />
			]]>
		</echo>
      <echo message="##teamcity[publishArtifacts '${basedir}/build/logs/jdepend.xml']" />
      <echo message="##teamcity[publishArtifacts '${basedir}/build/pdepend/dependencies.png']" />
      <echo message="##teamcity[publishArtifacts '${basedir}/build/pdepend/overview-pyramid.png']" />
	   <echo message="##teamcity[publishArtifacts '${basedir}/build/logs/pdepend.summary.xml']" />
		<echo message="##teamcity[publishArtifacts '${basedir}/build/pdepend/pdepend.html']" />
	</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="phpmd">
			<arg path="${basedir}" />
			<arg value="text" />
			<arg value="${basedir}/build/phpmd.xml" />
			<arg value="--exclude" />
			<arg value="*/lib/*,*/tests/*" />
		</exec>
	</target>

	<target name="phpmd-ci" depends="prepare" description="Perform project mess detection using PHPMD creating a log file for the continuous integration server">
        <property name="pmd-html" value="${basedir}/build/logs/phpmd.html" />
        <exec executable="phpmd" failonerror="false">
            <arg path="${basedir}" />
            <arg value="html" />
            <arg value="${basedir}/build/phpmd.xml" />
            <arg value="--reportfile" />
            <arg value="${pmd-html}" />
            <arg value="--exclude" />
	         <arg value="*/lib/*,*/tests/*" />
        </exec>
        <echo message="##teamcity[publishArtifacts '${pmd-html}']" />
	</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="phpcs">
            <arg value="-s" />
            <arg value="--extensions=php" />
			<arg value="--standard=${basedir}/build/phpcs.xml" />
			<arg path="${basedir}" />
		</exec>
	</target>

	<target name="phpcs-ci" depends="prepare" description="Find coding standard violations using PHP_CodeSniffer creating a log file for the continuous integration server">
		<exec executable="phpcs" failonerror="false">
            <arg value="--extensions=php" />
            <arg value="--tab-width=4" />
			<arg value="--report=checkstyle" />
			<arg value="--report-file=${basedir}/build/logs/checkstyle.xml" />
			<arg value="--standard=${basedir}/build/phpcs.xml" />
			<arg path="${basedir}" />
		</exec>
        <echo message="##teamcity[importStockData type='checkstyle' path='${basedir}/build/logs/checkstyle.xml']"/>
	</target>

	<target name="phpcpd" depends="prepare" description="Find duplicate code using PHPCPD">
        <property name="phpcpd" value="${basedir}/build/logs/pmd-cpd.xml" />
		<exec executable="phpcpd" failonerror="false">
			<arg value="--log-pmd" />
			<arg value="${phpcpd}" />
			<arg value="--exclude" />
			<arg value="lib" />
            <arg value="--exclude" />
            <arg value="tests" />
			<arg path="${basedir}" />
		</exec>
        <echo message="##teamcity[importStockData type='pmdCpd' path='${phpcpd}']"/>
	</target>

	<target name="phpunit"
	        depends="prepare"
	        description="Run unit tests with PHPUnit">
		<exec executable="phpunit" failonerror="true">
			<arg value="--configuration"/>
			<arg path="${basedir}/build/phpunit.xml"/>
		</exec>
        <echo message="##teamcity[importStockData type='junit' path='${basedir}/build/phpunit.xml' parseOutOfDate='true']" />
	</target>

	<target name="phpdox" depends="prepare" description="Generate API documentation using phpDox">
		<property name="phpdox" value="${basedir}/build/deliver/phpdox.zip" />
		<exec executable="phpdox" failonerror="false">
			<arg line="--file ${basedir}/build/phpdox.xml" />
		</exec>
        <zip destfile="${phpdox}" basedir="${basedir}/build/api" />
        <echo message="##teamcity[publishArtifacts '${phpdox}']" />
	</target>

	<target name="phpcb" depends="prepare" description="Generate Code Browser files using phpCB">
		<property name="phpcb" value="${basedir}/build/deliver/phpcb.zip" />
		<exec executable="phpcb" failonerror="false">
			<arg line="--log ${basedir}/build/logs" />
			<arg line="--source ${basedir}" />
			<arg line="--output ${basedir}/build/phpcb" />
			<arg line="--ignore ${basedir}/lib" />
			<arg line="--ignore ${basedir}/build" />
		</exec>
		<zip destfile="${phpcb}" basedir="${basedir}/build/phpcb" />
		<echo message="##teamcity[publishArtifacts '${phpcb}']" />
	</target>

	<target name="zip-artifacts" depends="prepare" description="Create a ZIP file of the build artifacts">
		<property name="zip" value="${basedir}/build/deliver/${build}.zip" />
		<zip destfile="${zip}" basedir="${basedir}" excludes="build/, tests/, build.xml, cache.properties" />
		<echo message="##teamcity[publishArtifacts '${zip}']" />
	</target>
</project>
