<?xml version="1.0" encoding="UTF-8"?>
<project name="partkeepr" default="build">
	<property file="build.properties" />

	<target name="pre-commit">
		<jsllint haltonfailure="true">
			<fileset dir="src/frontend">
				<include name="**/*.js" />
			</fileset>
		</jsllint>
	</target>

	<!-- Cleans the output directories -->
	<target name="clean">
		<delete dir="reports" />
		<delete dir="frontend" />
		<delete dir="setup" />
		<delete file="partkeepr.jsb3" />
	</target>

	<!--  Re-generates the proxies needed by Doctrine2 -->
	<target name="create-orm-proxies">
		<mkdir dir="data/" />
		<exec command="doctrine orm:generate-proxies" passthru="true" />
	</target>

	<target name="compile-theme">
		<exec dir="theme/sass" command="compass compile partkeepr.scss" passthru="true" />
		<copy file="theme/css/partkeepr.css" tofile="src/frontend/css/partkeepr-theme.css" />
		<copy file="theme/css/partkeepr.css" tofile="src/setup/css/partkeepr-theme.css" />

		<copy todir="src/frontend/themes/images/">
			<fileset dir="theme/themes/images/">
				<include name="**/*" />
			</fileset>
		</copy>

		<copy todir="src/setup/themes/images/">
			<fileset dir="theme/themes/images/">
				<include name="**/*" />
			</fileset>
		</copy>
	</target>

	<target name="git-update-submodules">
		<exec command="git submodule sync" passthru="true" checkreturn="true" />
		<exec command="git submodule init" passthru="true" checkreturn="true" />
		<exec command="git submodule update" passthru="true" checkreturn="true" />
	</target>

	<!-- Build the end-user 'setup' directory -->
	<target name="build-setup">
		<mkdir dir="setup/" />

		<echo>Copying extjs</echo>
		<mkdir dir="setup/extjs/" />
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -ptgo
				                         --include=/bootstrap.js
				                         --include=/ext-all.js
				                         --include=/ext-all-debug.js
				                         --include=/ext-all-debug-w-comments.js
				                         --exclude=*
				                         ${extjs.path}/* setup/extjs/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="false" todir="setup/extjs/">
					<fileset dir="${extjs.path}">
						<include name="bootstrap.js" />
						<include name="compatibility/ext3-core-compat.js" />
						<include name="compatibility/ext3-compat.js" />
						<include name="ext-all.js" />
						<include name="ext-all-debug.js" />
						<include name="ext-all-debug-w-comments.js" />
						<include name="adapter/ext/ext-base.js" />
						<include name="adapter/ext/ext-base-debug-w-comments.js" />
						<include name="source/adapter/ext-base.js" />
					</fileset>
				</copy>
			</else>
		</if>

		<echo>Copying Ext.ux.Wizard</echo>
		<mkdir dir="setup/js/wizard" />
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a --delete 3rdparty/ext-wizard/Ext.ux.Wizard/ setup/js/wizard/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="true" todir="setup/js/wizard">
					<fileset dir="3rdparty/ext-wizard/Ext.ux.Wizard">
						<include name="**" />
					</fileset>
				</copy>
			</else>
		</if>

		<echo>Copying src/setup</echo>
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a src/setup/ setup/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="true" todir="setup/">
					<fileset dir="src/setup">
						<include name="**" />
					</fileset>
				</copy>
			</else>
		</if>

	</target>

	<!-- Build the end-user 'frontend' directory -->
	<target name="build-frontend">
		<mkdir dir="frontend/" />

		<echo>Copying 3rdparty/extjs - this may take up to a few minutes</echo>
		<mkdir dir="frontend/extjs" />
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -ptgo -r
				                         --include=/bootstrap.js
				                         --include=/ext-all.js
				                         --include=/ext-all-debug.js
				                         --include=/ext-all-debug-w-comments.js
				                         --include=/resources/***
				                         --exclude=/*
				                         ${extjs.path}/* frontend/extjs/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="false" todir="frontend/extjs/">
					<fileset dir="${extjs.path}">
						<include name="bootstrap.js" />
						<include name="compatibility/ext3-core-compat.js" />
						<include name="compatibility/ext3-compat.js" />
						<include name="ext-all.js" />
						<include name="ext-all-debug.js" />
						<include name="ext-all-debug-w-comments.js" />
						<include name="adapter/ext/ext-base.js" />
						<include name="adapter/ext/ext-base-debug-w-comments.js" />
						<include name="source/adapter/ext-base.js" />
						<include name="resources/**" />
					</fileset>
				</copy>
			</else>
		</if>

		<echo>Copying Ext Statusbar</echo>
		<mkdir dir="frontend/js/Ext.ux/statusbar" />
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a --delete ${extjs.path}/examples/ux/statusbar/ frontend/js/Ext.ux/statusbar/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="false" todir="frontend/js/Ext.ux/">
					<fileset dir="${extjs.path}/examples/ux">
						<include name="statusbar/**" />
					</fileset>
				</copy>
			</else>
		</if>

		<echo>Copying Ext.tab.TabCloseMenu</echo>
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a ${extjs.path}/examples/ux/TabCloseMenu.js frontend/js/Ext.ux/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="false" todir="frontend/js/Ext.ux/">
					<fileset dir="${extjs.path}/examples/ux">
						<include name="TabCloseMenu.js" />
					</fileset>
				</copy>
			</else>
		</if>

        <echo>Copying Ext.ux.Wizard</echo>
        <copy overwrite="false" todir="frontend/js/Ext.ux/">
            <fileset dir="3rdparty/ext-wizard/Ext.ux.Wizard/">
                <include name="Ext.ux.Wizard.Card.js" />
                <include name="Ext.ux.Wizard.CardLayout.js" />
                <include name="Ext.ux.Wizard.Header.js" />
                <include name="Ext.ux.Wizard.js" />
            </fileset>
        </copy>

        <echo>Copying Ext.ux.Exporter</echo>
        <copy overwrite="false" todir="frontend/js/Ext.ux.Exporter/">
            <fileset dir="3rdparty/Ext.ux.Exporter">
                <include name="Base64.js" />
                <include name="Formatter.js" />
                <include name="Button.js" />
                <include name="Exporter.js" />
                <include name="downloadify.min.js" />
                <include name="wikiFormatter/*" />
                <include name="excelFormatter/*" />
                <include name="csvFormatter/*" />
            </fileset>
        </copy>

		<echo>Copying phpjs</echo>
		<mkdir dir="frontend/js/org.phpjs.lib/" />
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a 3rdparty/phpjs/php.default.min.js frontend/js/org.phpjs.lib/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="false" todir="frontend/js/org.phpjs.lib">
					<fileset dir="3rdparty/phpjs/">
						<include name="php.default.min.js" />
					</fileset>
				</copy>
			</else>
		</if>

		<echo>Copying JS frontend</echo>
		<if>
			<isset property="build.fast" />
			<then>
				<exec dir="." passthru="true" command="rsync -a --exclude=.git src/frontend/ frontend/" checkreturn="true" />
			</then>
			<else>
				<copy overwrite="true" todir="frontend/">
					<fileset dir="src/frontend">
						<include name="**" />
					</fileset>
				</copy>
			</else>
		</if>

		<phingcall target="create-orm-proxies" />

		<phingcall target="jsbuilder" />
	</target>

	<!-- Builds the frontend, regenerates the proxies and minifies the JS -->
	<target name="build">
		<phingcall target="git-update-submodules" />

		<phingcall target="build-frontend" />
		<phingcall target="build-setup" />
	</target>

	<!-- Fast build uses rsync instead of standard file copy -->
	<target name="build-fast">
		<condition property="build.fast">
			<or>
				<os family="mac" />
				<os family="unix" />
			</or>
		</condition>

		<fail unless="build.fast" message="Fast build only supported on Mac and Unix-like systems (rsync required)" />

		<phingcall target="build" />
	</target>

	<!--
	Creates a new PartKeepr release.
	
	This task asks for the target version number, and then generates the zip and tbz2 archives out of it.
	-->
	<target name="release">
		<propertyprompt propertyName="partkeepr.version" defaultValue="" promptText="Please enter the version number of the build to generate (e.g. 0.1)" />

		<phingcall target="prepare-archiving" />
		<phingcall target="set-version" />

		<zip destfile="partkeepr-${partkeepr.version}.zip" basedir="${packagepath}" prefix="partkeepr-${partkeepr.version}/" />
		<tar destfile="partkeepr-${partkeepr.version}.tbz2" compression="bzip2" basedir="${packagepath}" prefix="partkeepr-${partkeepr.version}/" />
	</target>

	<!--
	Runs the build process, then copies the current working tree to a temporary directory (specified by ${packagepath})
	while excluding stuff the end-user doesn't need (like .git directories, and some 3rdparty stuff).
	-->
	<target name="prepare-archiving">
		<phingcall target="build" />

		<delete quiet="true">
			<fileset dir="${packagepath}" />
		</delete>

		<copy todir="${packagepath}" includeemptydirs="false">
			<fileset dir=".">
				<exclude name="3rdparty/extjs/**" />
				<exclude name="3rdparty/phpjs/**" />
				<exclude name="3rdparty/ext-wizard/**" />
				<exclude name="theme/**/*" />
				<exclude name="testing/**/*" />
				<exclude name="util/**/*" />
				<exclude name="src/frontend/**/*" />
				<exclude name="src/setup/**" />
				<exclude name="partkeepr.jsb3" />
				<exclude name="partkeepr.jsb3.template" />
				<exclude name="pre-commit.hook" />
				<exclude name="RELEASE-TODO" />
				<exclude name="build.properties" />
				<exclude name="build.xml" />
				<exclude name="cli-config.php" />
				<exclude name="frontend/resources/fugue-icons/src/**/*" />
				<exclude name="frontend/resources/fugue-icons/bonus/**/*" />
				<exclude name="frontend/resources/fugue-icons/icons-shadowless/**/*" />
				<exclude name="frontend/resources/fugue-icons/all.png" />
				<exclude name="frontend/resources/fugue-icons/all-preview.png" />
				<exclude name="frontend/resources/fugue-icons/all-shadowless.png" />
				<exclude name=".git/**/*" />
				<exclude name="3rdparty/doctrine-migrations/.git/**/*" />
				<exclude name="frontend/resources/silkicons/.git/**/*" />
				<exclude name="frontend/resources/flags/.git/**/*" />
				<exclude name="frontend/resources/mimetypes/.git/**/*" />
				<exclude name="frontend/resources/fugue-icons/.git/**/*" />
				<exclude name="config.php" />

			</fileset>
		</copy>

		<delete dir="${packagepath}/theme/" />
		<delete dir="${packagepath}/testing/" />
		<delete dir="${packagepath}/tests/" />
		<delete dir="${packagepath}/src/frontend/" />
		<delete dir="${packagepath}/src/setup/" />
		<delete dir="${packagepath}/util/" />
		<delete dir="${packagepath}/.git/" />
		<delete dir="${packagepath}/3rdparty/doctrine-migrations/.git/" />
		<delete dir="${packagepath}/frontend/resources/silkicons/.git/" />
		<delete dir="${packagepath}/frontend/resources/flags/.git/" />
		<delete dir="${packagepath}/frontend/resources/mimetypes/.git/" />
		<delete dir="${packagepath}/frontend/resources/fugue-icons/.git/" />

		<delete dir="${packagepath}/3rdparty/extjs/" />
		<delete dir="${packagepath}/3rdparty/phpjs/" />
		<delete dir="${packagepath}/3rdparty/ext-wizard/" />
		<delete dir="${packagepath}/frontend/resources/fugue-icons/src/" />
		<delete dir="${packagepath}/frontend/resources/fugue-icons/bonus/" />
		<delete dir="${packagepath}/frontend/resources/fugue-icons/icons-shadowless/" />
	</target>

	<!--
	Sets the version of the release inside PartKeeprVersion.php
	-->
	<target name="set-version">
		<exec executable="sed">
			<arg value="-i" />
			<arg value="s/{V_GIT}/${partkeepr.version}/g" />
			<arg value="${packagepath}/src/backend/PartKeepr/PartKeeprVersion.php" />
		</exec>
	</target>

	<target name="nightly">
		<phingcall target="prepare-archiving" />

		<tstamp>
			<format property="build.time" pattern="partkeepr-nightly-%Y%m%d" />
		</tstamp>

		<property name="partkeepr.version" value="${build.time}" />
		<phingcall target="set-version" />

		<zip destfile="${build.time}.zip" basedir="${packagepath}" prefix="partkeepr-nightly/" />
		<tar destfile="${build.time}.tbz2" compression="bzip2" basedir="${packagepath}" prefix="partkeepr-nightly/" />
	</target>

	<!-- Builds the jsb3 file and build the minified JS files -->
	<target name="jsbuilder">
        <property name="jsbuilder.sources" value="--source=${project.basedir}/3rdparty/extjs/examples/ux/TabCloseMenu.js --source=${project.basedir}/src/frontend/js --source=${project.basedir}/3rdparty/ext-wizard/Ext.ux.Wizard/ --source=${project.basedir}/3rdparty/Ext.ux.Exporter/ --source ${project.basedir}/3rdparty/extjs/examples/ux/statusbar/"/>
		<exec passthru="true" command="php partkeepr.php partkeepr:js-minify ${project.basedir}/frontend/js/partkeepr-debug.js ${jsbuilder.sources}" />
        <exec passthru="true" command="php partkeepr.php partkeepr:js-minify ${project.basedir}/frontend/js/partkeepr.js --compress ${jsbuilder.sources}" />
    </target>

	<!-- Checks all JavaScript files for common mistakes. Requires JSLint from http://www.javascriptlint.com -->
	<target name="jslint">
		<jsllint>
			<fileset dir="src/frontend">
				<include name="**/*.js" />
			</fileset>
		</jsllint>
		<jsllint>
			<fileset dir="src/setup">
				<include name="**/*.js" />
			</fileset>
		</jsllint>
	</target>

	<!-- Checks all PHP files for messy code. -->
	<target name="phpmd">
		<mkdir dir="reports/logs" />

		<phpmd rulesets="codesize,unusedcode,design,naming">
			<fileset dir="src/">
				<include name="**/*.php" />
			</fileset>
			<formatter type="xml" outfile="reports/logs/pmd.xml" />
            <formatter type="html" outfile="reports/pmd.html"/>
		</phpmd>
	</target>

	<!-- Checks all PHP files for copy/paste code -->
	<target name="phpcpd">
		<mkdir dir="reports/logs" />
		<phpcpd minTokens="30">
			<fileset dir="src/">
				<include name="**/*.php" />
			</fileset>
			<formatter type="pmd" outfile="reports/logs/pmd-cpd.xml" />
		</phpcpd>
	</target>

	<target name="code-coverage">

		<mkdir dir="reports/coverage/db" />
		<mkdir dir="reports/phpunit" />

		<coverage-setup database="reports/coverage/db/coverage.db">
			<fileset dir="src/backend">
				<include name="**/*.php" />
			</fileset>
		</coverage-setup>

		<phpunit bootstrap="tests/bootstrap.php" haltonfailure="false" codecoverage="true" printsummary="true">

			<batchtest>
				<fileset dir="tests/">
					<include name="**/*Test.php" />
				</fileset>
			</batchtest>
			<formatter todir="reports/phpunit/" type="xml" />
			<formatter type="clover" usefile="true" todir="reports/coverage/" />

		</phpunit>

		<phpunitreport infile="reports/phpunit/testsuites.xml" format="frames" todir="reports/phpunit" />

		<coverage-report outfile="reports/coverage/db/coverage.xml">
			<report todir="reports/coverage/" />
		</coverage-report>
	</target>

	<target name="test">
		<mkdir dir="reports/phpunit" />

		<phpunit bootstrap="tests/bootstrap.php" haltonfailure="false" haltonerror="true" codecoverage="false" printsummary="true">
			<formatter todir="reports/phpunit/" type="xml" />
			<batchtest>
				<fileset dir="tests/">
					<include name="**/*Test.php" />
				</fileset>
			</batchtest>
			<formatter type="plain" usefile="false" />
		</phpunit>

		<phpunitreport infile="reports/phpunit/testsuites.xml" format="frames" todir="reports/phpunit" />

	</target>

	<target name="test-setup">
		<echo msg="Testing the installation..." />

		<phpunit haltonfailure="true" codecoverage="false" printsummary="true">
			<batchtest>
				<fileset dir="setup/tests/">
					<include name="*Test.php" />
				</fileset>
			</batchtest>
			<formatter type="plain" usefile="false" />
		</phpunit>

		<echo msg="" />

		<echo msg="       ***************************" />
		<echo msg="       Setup Test Complete" />
		<echo msg="       ***************************" />
		<echo msg="" />
		<echo msg="       No errors were found." />
		<echo msg="       You may now proceed!" />
	</target>

	<target name="apigen">
		<apigen source="${source.php}" destination="reports/api/" title="PartKeepr API Documentation" internal="true" deprecated="true" todo="true" charset="utf8" report="reports/logs/api-checkstyle.xml">

		</apigen>
	</target>

	<target name="jenkins" depends="clean,phpmd,apigen,jslint,phpcpd,code-coverage">

	</target>
</project>
