<?xml version="1.0" encoding="UTF-8"?>
<project name="com.nokia.s60tools.creator" default="cc" basedir=".">

	<!-- Targets -->
	<target name="cc" depends="clean-test,jar, instr,feature, analyze, zip" description="Build, instrument, run unit tests and analyze code"/>


	<property name="basews" value="${ws}"/>
	<property name="baseos" value="${os}"/>
	<property name="basearch" value="${arch}"/>
	<property name="basenl" value="${nl}"/>

	<!-- Compiler settings -->
	<property name="javacFailOnError" value="true"/>
	<property name="javacDebugInfo" value="on"/>
	<property name="javacVerbose" value="false"/>
	<property name="logExtension" value=".log"/>
	<property name="compilerArg" value=""/>
	<property name="javacSource" value="5"/>
	<property name="javacTarget" value="5"/>
	
	<property name="bootclasspath" refid="path_bootclasspath"/>
	<property name="bundleJavacSource" value="${javacSource}"/>
	<property name="bundleJavacTarget" value="${javacTarget}"/>
	<property name="bundleBootClasspath" value="${bootclasspath}"/>
	
	<property name="help.project.name" value="${ant.project.name}.help"/>
	<property name="feature.project.name" value="com.nokia.carbide.extension.creator"/>
	<property name="feature.name" value="Creator"/>
	
	<property name="jar.filename" value="${ant.project.name}.jar"/>
	<property name="help.jar.filename" value="${help.project.name}.jar"/>
	
	<!-- Folder definitions -->
	<property name="feature.project.folder" location="../com.nokia.carbide.extension.creator"/>
	<property name="binaries.folder" location="../${feature.name}.binaries"/>
	
	<!-- Carbide layouts -->
	<property name="carbide.dev.folder" location="c:/Carbide_development/plugins"/>
	<property name="carbide.int.folder" location="c:/Carbide_internal/plugins"/>
	<property name="carbide.adt.folder" location="c:/Carbide_ADT/plugins"/>
	
	<property name="reports.folder" location="../reports"/>
	<property name="reports.emma" location="${reports.folder}/emma"/>
	<property name="instr.folder" location="../instr"/>
	
	
	<!-- EMMA configuration -->
 	<path id="emma.lib" >
    	<pathelement location="${ant.home}/lib/emma.jar" />
    	<pathelement location="${ant.home}/lib/emma_ant.jar" />
  	</path>

  	<taskdef resource="emma_ant.properties" classpathref="emma.lib" />
	
	<!-- PMD configuration -->
	<path id="pmd.lib" >
    	<pathelement location="${ant.home}/lib/pmd-4.2.jar" />
    	<pathelement location="${ant.home}/lib/asm-3.1.jar" />
    	<pathelement location="${ant.home}/lib/jaxen-1.1.1.jar" />
  	</path>
	
	<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.lib" />
	
	<path id="path_bootclasspath">
		<fileset dir="${java.home}/lib">
			<include name="*.jar"/>
		</fileset>
	</path>	
	
	<path id="build_classpath">
		<path refid="carbide_classpath"/>
	</path>
	
	<!-- Select Carbide layout -->
	<path id="carbide_classpath">
		<fileset dir="${carbide.int.folder}" includes="**/*.jar" />
	</path>
	
	<target name="properties" if="eclipse.running">
		<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
	</target>

	<!-- Initialization -->
	<target name="init" depends="properties">
		<condition property="pluginTemp" value="${buildTempFolder}/plugins">
			<isset property="buildTempFolder"/>
		</condition>
		<property name="pluginTemp" value="${basedir}"/>
		<condition property="build.result.folder" value="${pluginTemp}/${ant.project.name}">
			<isset property="buildTempFolder"/>
		</condition>
		<property name="build.result.folder" value="${basedir}"/>
		<property name="temp.folder" value="${basedir}/temp.folder"/>
		<property name="plugin.destination" value="${basedir}"/>

		<property name="jar.filename" value="${ant.project.name}.jar"/>
		
		<delete dir="${reports.folder}"/>
		<mkdir dir="${reports.folder}"/>
		
		<delete dir="../plugins"/>
		<mkdir dir="../plugins"/>
		
		<!-- Delete build folders -->
		<delete dir="${basedir}/com"/>
		
		<exec executable="svn" dir="../.">
			<arg line="up"/>
		</exec>
	</target>

	<!-- Build target -->
	<target name="build" depends="init" description="Build the source">
	
		<!-- Build the source -->
		<javac destdir="" failonerror="${javacFailOnError}" verbose="${javacVerbose}" debug="${javacDebugInfo}" includeAntRuntime="no" bootclasspath="${bundleBootClasspath}" source="${bundleJavacSource}" target="${bundleJavacTarget}">
			<compilerarg line="${compilerArg}" compiler="${build.compiler}"/>
			<classpath refid="build_classpath" />
			<src path="src/" />
			<compilerarg value="@${basedir}/javaCompiler...args" compiler="org.eclipse.jdt.core.JDTCompilerAdapter"/>
			<compilerarg line="-log '${temp.folder}/bin${logExtension}'" compiler="org.eclipse.jdt.core.JDTCompilerAdapter"/>
		</javac>

		<!-- Copy necessary resources -->
		<copy todir="" failonerror="true" overwrite="false">
			<fileset dir="src/" excludes="**/*.java, **/package.htm*" />
		</copy>

		<!-- Create build result folder -->
		<mkdir dir="${build.result.folder}"/>

	</target>

	
	<!-- JAR target -->
	<target name="jar" depends="build" description="Create JAR">

		<!-- Create JARs -->
		<zip destfile="../plugins/${jar.filename}">
				<fileset dir="${build.result.folder}">
						  <include name="com/**"/>
					    <include name="icons/**"/>
					    <include name="META-INF/**"/>
					    <include name="plugin.xml"/>
					    <include name="about.html"/>
					    <include name="about.ini"/> 
					    <include name="about.mappings"/> 
					    <include name="about.properties"/> 
				</fileset>
		</zip>
		

	</target>
	
	
	<!-- Instrumentation target, depends on JAR -->
	<target name="instr" depends="jar">
		<!-- Instrument the source code -->
		<emma>
			<instr instrpath="../plugins/${jar.filename}" destdir="${instr.folder}" metadatafile="${reports.emma}/metadata.emma" merge="true"/>
		</emma>
	</target>
	
	

	<!-- Feature target -->
	<target name="feature" depends="jar" description="Create feature">
		<property name="feature.folder" location="${binaries.folder}/feature"/>
		
		
		<mkdir dir="${feature.folder}"/>
		<mkdir dir="${feature.folder}/plugins"/>
		<mkdir dir="${feature.folder}/features/${feature.project.name}"/>
		<mkdir dir="${binaries.folder}/nightly_builds"/>		
				
		<!-- Create help JAR -->
		<zip destfile="../plugins/${help.jar.filename}" basedir="../${help.project.name}" excludes="src/,.*"/>
		
		<!-- Copy feature stuff to the temp folder -->
		<copy todir="${feature.folder}/features/${feature.project.name}" file="${feature.project.folder}/feature.xml"/>
		<copy todir="${feature.folder}/features/${feature.project.name}" file="${feature.project.folder}/license.txt"/>
		
		<!-- Set version numbers to plugins jar packages -->
		<exec dir="../" executable="bash" resolveexecutable="true" failonerror="true"> 
				<arg value="-c"/>
				<arg value="C:\\hudson\\jobs\\setPluginVersion.sh ${ant.project.name}.help"/>
		</exec>	
		<exec dir="../" executable="bash" resolveexecutable="true" failonerror="true"> 
				<arg value="-c"/>
				<arg value="C:\\hudson\\jobs\\setPluginVersion.sh ${ant.project.name}"/>
		</exec>
		
		<!-- Copy the actual plug-in to the feature folder -->
		<copy todir="${feature.folder}/plugins">
			<fileset dir="../plugins"/>
		</copy>
		
		
		<!-- Copy the actual plug-ins to the carbide folders -->
		<copy todir="${carbide.int.folder}" failonerror="true" overwrite="true">
				<fileset dir="${feature.folder}/plugins"/>
		</copy>
		<copy todir="${carbide.dev.folder}" failonerror="true" overwrite="true">
				<fileset dir="${feature.folder}/plugins"/>
		</copy>
		<copy todir="${carbide.adt.folder}" failonerror="true" overwrite="true">
				<fileset dir="${feature.folder}/plugins"/>
		</copy>
		
	</target>
	
	<!-- Analyze target -->
	<target name="analyze">
		<!-- PMD -->
		<pmd>
			<!-- Rules -->
			<ruleset>basic</ruleset>
			<ruleset>codesize</ruleset>
			<ruleset>coupling</ruleset>
			<ruleset>design</ruleset>
			<ruleset>strictexception</ruleset>
			<ruleset>strings</ruleset>
			<ruleset>sunsecure</ruleset>
			<ruleset>unusedcode</ruleset>
			<ruleset>junit</ruleset>
			
			<!-- XML output -->
			<formatter type="xml" toFile="${reports.folder}/report.pmd.xml"/>
			<formatter type="html" toFile="${reports.folder}/report.pmd.html"/>
			
			<!-- Files to analyze -->
			<fileset dir="src/">
					<include name="**/*.java"/>
			</fileset>
			
		</pmd>
	</target>
			
	<!-- Clean tests target -->
  	<target name="clean-test" description="Clean test">
	    	<delete dir="${instr.folder}"/>
	    	<delete dir="${reports.folder}"/>
  	</target>
  
	
	<!-- Make zip package of the feature and plugins -->
	<target name="zip">
	
			<!-- Create the feature zip -->
			<zip destfile="${binaries.folder}/${feature.name}.zip" basedir="${feature.folder}"/>
			
			<delete dir="${feature.folder}"/>
			
			<!-- Nightly build -->
			<tstamp>
			   <format property="timestamp" pattern="dd-MM-yyyy" />
			</tstamp>
	
			<delete dir="${binaries.folder}/nightly_builds"/>
	  	<mkdir dir="${binaries.folder}/nightly_builds"/>
	
			<copy tofile="${binaries.folder}/nightly_builds/${feature.name}-${timestamp}.zip" file="${binaries.folder}/${feature.name}.zip"/>
			
	</target>
	
</project>
