<?xml version="1.0" encoding="UTF-8"?>
<project name="fabrik_js" default="build_js">
	<!--Build -->
	<target name="js" description="lints and builds js project files for production use"
		depends="load.properties">
		<antcall target="js.lint"></antcall>
		<antcall target="js.minify"></antcall>
	</target>

	<target name="load.properties" description="Set properties for this build">
		<echo>load properties</echo>
		<!--YUI Compressor location -->
		<property name="yui.dir"
			value="${basedir}/fabrik_build/yuicompressor/build/yuicompressor-2.4.6.jar" />
		<!--Source JS dir -->
		<property name="src.js.dir" value="${basedir}/media/com_fabrik/js" />
		<!--Source CSS dir -->
		<property name="src.css.dir" value="${basedir}/media/com_fabrik/css" />
		<!--Source Lint/Hint dir -->
		<property name="jslint.js" value="${basedir}/fabrik_build/lint/jshint.js" />
		<!--Rhino dir -->
		<property name="js.jar" value="${basedir}/fabrik_build/lint/rhino/js.jar" />
		<!-- Rhino lint -->
		<property name="jshint-rhino.js" value="${basedir}/fabrik_build/lint/jshint-rhino.js" />
		<!--Output dir -->
		<property name="build.dir" value="${basedir}/media/com_fabrik/" />
		<!--Build version information -->
		<property name="build.major" value="1" />
		<property name="build.minor" value="1" />
		<property name="jshint.flags"
			value="browser=true,mootools=true,white=true,loopfunc=true,expr=true,evil=true,boss=true,maxerr=25,undef=false,curly=true,debug=false,eqeqeq=true,immed=true,newcap=true" />
		<property name="jshint.predef"
			value="console,confirm" />
		<property name="jshint.predef.test"
			value="${jshint.predef},icon" />

	</target>

	<!--JS Lint -->
	<target name="js.lint">
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/administrator/components/com_fabrik/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${src.js.dir}" />
		</antcall> 
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_cron/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_element/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_form/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_list/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_validationrule/" />
		</antcall>
		<antcall target="js.lint.do">
			<param name="dir" value="${basedir}/plugins/fabrik_visualization/" />
		</antcall>
	</target>
	
	<target name="js.lint.do">
		<echo>dir = ${dir}</echo>
		<apply dir="${basedir}/fabrik_build/lint" failonerror="true" executable="java" errorproperty="linterror" resultproperty="lintresult">
			<fileset dir="${dir}">
				<include name="**/*.js" />
				<exclude name="**/lib/" />
				<exclude name="**/*-min.js" />
				<exclude name="**/libs/" />
				<exclude name="**/swatches/" />
			</fileset>
			<arg line="-jar ${js.jar} ${jshint-rhino.js}" />
			<srcfile />
			<arg value="${jshint.flags}" />
			<arg value="${jshint.predef}" />
		</apply>
		<echo>lint error: ${linterror}</echo>
		<echo>lint result: ${lintresult}</echo>
		<!--  0 is no errors - > 0 is an erorr count -->
		<echo>${dir} JSHint Passed</echo>
	</target>
	
	<target name="js.minify" depends="load.properties">
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/administrator/components/com_fabrik/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${src.js.dir}" />
		</antcall> 
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_cron/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_element/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_form/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_list/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_validationrule/" />
		</antcall>
		<antcall target="js.minify.do">
			<param name="dir" value="${basedir}/plugins/fabrik_visualization/" />
		</antcall>
		
	</target>
	
		<!--Minify JS files -->
	<target name="js.minify.do" description="Minifies JavaScript files">
	
	<!--  delete min files first - seems that sometimes they arent updated after a merge with master -->
	
		<delete>
			<fileset dir="${dir}" includes="**/*-min.js" excludes="**/lib/" />
		</delete>
		<echo>${dir}</echo>
		<apply executable="java" parallel="false" dest="${dir}" failonerror="true" >
			<fileset dir="${dir}">
				<include name="**/*.js" />
				<exclude name="**/lib/" />
				<exclude name="**/*-min.js" />
				<exclude name="**/swatches/" />
			</fileset>
			<arg line="-jar" />
			<arg path="${yui.dir}" />
			<srcfile />
			<arg line="-o" />
			
			<mapper type="glob" from="*.js" to="*-min.js" />
			<targetfile />
		</apply>
		<echo>Finished</echo>
	</target>
</project>