<?xml version="1.0"?>
<!-- 
============================================================================ 
Name        : build.xml 
Part of     : Helium AntLib

Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
All rights reserved.
This component and the accompanying materials are made available
under the terms of the License "Eclipse Public License v1.0"
which accompanies this distribution, and is available
at the URL "http://www.eclipse.org/legal/epl-v10.html".

Initial Contributors:
Nokia Corporation - initial contribution.

Contributors:

Description:

============================================================================
-->
<project name="build-macros" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:au="org.apache.ant.antunit"
    xmlns:cs="antlib:com.puppycrawl.tools.checkstyle">
    <import file="common.ant.xml" />

    <path id="emma.classpath">
        <fileset dir="${builder.dir}/antlibs">
            <include name="**/*.jar" />
        </fileset>
    </path>
    
    <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="emma.classpath"/>
    <taskdef classpathref="emma.classpath" resource="emma_ant.properties" />
    
    
    <target name="clean">
        <delete dir="${module.temp.dir}" />
    </target>

    
    <target name="resolve">
        <mkdir dir="${lib.dir}" />
        <mkdir dir="${lib.dir}/zipsafe" />
        <mkdir dir="${lib.dir}/zipnotsafe" />
        <delete dir="${lib.dir}/zipnotsafe" includes="**/*" />
        
        <ivy:settings file="${ivy.settings.dir}/ivysettings.xml" />
        
        <!-- the call to resolve is not mandatory, retrieve makes an implicit call if we don't -->
        <ivy:resolve file="${ivy.file}" log="download-only"/>
        <ivy:retrieve pattern="${lib.dir}/zipsafe/[artifact].[ext]" type="egg" log="download-only"/>
        <ivy:retrieve pattern="${lib.dir}/zipsafe/[artifact].[ext]" type="py" log="download-only"/>
        <ivy:retrieve pattern="${lib.dir}/[artifact].[ext]" type="jar" log="download-only"/>
    </target>

    
    <target name="report" depends="resolve" description="--> generates a report of dependencies">
        <ivy:report todir="${build.dir}"/>
    </target>

    
    <target name="quality" depends="checkstyle" />
    
    
    <target name="checkstyle" depends="resolve">
        <mkdir dir="${module.temp.dir}"/>
        <echo>Running checkstyle on ${ant.project.name}.</echo>
        <cs:checkstyle config="${builder.dir}/java/config/java_checkstyle_config.xml">
            <classpath>
                <path refid="lib.path.id" />
                <pathelement location="${jar.file}" />
                <pathelement location="${module.bin.dir}/${ant.project.name}.jar" />
            </classpath>
            <fileset dir="${src.dir}" includes="**/*.java"/>
            <formatter type="plain"/>
            <formatter type="xml" toFile="${module.temp.dir}/checkstyle_errors.xml"/>
        </cs:checkstyle>
    </target>
    
    
    <target name="compile" depends="resolve" description="--> compile the project">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="lib.path.id" debug="true" source="1.6" target="1.6" listfiles="true"> 
            <compilerarg value="-Xlint:unchecked" />
            <compilerarg value="-Xlint:deprecation" />
            <compilerarg value="-Werror" />
        </javac>
    </target>

    
    <target name="ivy-new-version"  unless="ivy.new.revision">
        <!-- default module version prefix value 
        <property name="module.version.prefix" value="${module.version.target}-dev-b" />
        -->
        <!-- asks to ivy an available version number
        <ivy:info file="${ivy.file}" />
        <ivy:buildnumber 
            organisation="${ivy.organisation}" module="${ivy.module}" 
            revision="${module.version.prefix}" defaultBuildNumber="1" revSep=""/> -->
    </target>

    
    <target name="local-version">
        <!--<tstamp>
            <format property="now" pattern="yyyyMMddHHmmss"/>
        </tstamp>
        <property name="ivy.new.revision" value="${module.version.target}-local-${now}"/>-->
    </target>
    
    
    <target name="version" depends="ivy-new-version">
        <!-- create version file in classpath for later inclusion in jar 
        <mkdir dir="${classes.dir}"/>
        <echo message="version=${ivy.new.revision}" file="${classes.dir}/${ant.project.name}.properties" append="false" />
-->
        <!-- load generated version properties file 
        <property file="${classes.dir}/${ant.project.name}.properties" />-->
    </target>
    
    
    <target name="package" depends="local-version,version,compile" description="--> make a jar file for this project">
        <delete dir="${module.bin.dir}"/>
        <mkdir dir="${module.bin.dir}" />
        <jar destfile="${module.bin.dir}/${ant.project.name}.jar">
            <fileset dir="${classes.dir}" />
            <fileset dir="${src.dir}">
                <exclude name="**/distribution.policy.S60" />
                <exclude name="**/*.java" />
            </fileset>                
            <manifest>
                <section name="common">
                    <attribute name="Implementation-Title" value="${ant.project.name}" />
                    <attribute name="Implementation-Vendor" value="Nokia" />
                </section>
            </manifest>
        </jar>

        <!--copy file="${module.bin.dir}/${ant.project.name}.jar" todir="${bin.dir}" /-->
        <!--
        <mkdir dir="${bin.dir}/com.nokia.helium/${ant.project.name}/${version}" />
        <copy file="${bin.dir}/com.nokia.helium/${ant.project.name}/${version}/${ant.project.name}.jar" todir="${module.bin.dir}" />
        [organisation]/[module]/[color]/[revision]/[artifact].[ext] 
        
        pubrevision="${version}"
        pubdate="${now}"
        -->
        <ivy:publish artifactspattern="${module.bin.dir}/[artifact].[ext]" 
                        resolver="local"
                        status="integration"
                        forcedeliver="true"
                        pubrevision="1.0"
                        overwrite="true"
        />
    </target>


    <target name="test" depends="junit,antunit"/>

    
    <target name="junit" depends="resolve">
        <mkdir dir="${build.temp.dir}" />
        <if>
            <available type="dir" file="${basedir}/tests/src"/>
            <then>
                <echo>JUnit test module ${ant.project.name}</echo>
                <delete dir="${module.temp.dir}/junit_classes"/>
                <delete>
                    <fileset dir="${module.temp.dir}" includes="**/*_junit.emma" />
                    <fileset dir="${module.temp.dir}" includes="backup/*.jar" />
                </delete>
                <mkdir dir="${module.temp.dir}/junit_classes" />
                <javac srcdir="${basedir}/tests/src" destdir="${module.temp.dir}/junit_classes" debug="true"> 
                    <classpath>
                        <fileset dir="${module.temp.dir}">
                            <include name="**/*.jar"/>
                        </fileset>
                    </classpath>
                    <include name="**/*.java" />
                </javac>
        
                <mkdir dir="${module.temp.dir}/backup" />
                <copy file="${jar.file}" todir="${module.temp.dir}/backup/" overwrite="true"/>
                <emma enabled="true" verbosity="quiet">
                    <instr destdir="${module.temp.dir}/backup/" metadatafile="${module.temp.dir}/metadata_junit.emma" merge="true" mode="overwrite">
                        <instrpath>
                            <fileset dir="${module.temp.dir}/backup/" includes="${ant.project.name}.jar" />
                        </instrpath>
                    </instr>
                </emma>
                
                <mkdir dir="${junit.result.dir}" />
                <delete file="${module.temp.dir}/coverage.emma" failonerror="false" />
                <junit fork="yes" printsummary="yes" haltonfailure="yes">
                    <formatter type="brief" usefile="false"/>
                    <formatter type="xml" usefile="true"/>
                    <jvmarg value="-Demma.coverage.out.file=${module.temp.dir}/coverage_junit.emma" />
                    <jvmarg value="-Demma.coverage.out.merge=true" />
                    <jvmarg value="-Dtestdir=${basedir}" />
                    <jvmarg value="-Dtest.temp.dir=${test.temp.dir}" />
                    <classpath>
                        <fileset dir="${module.temp.dir}/backup/">
                            <include name="**/*.jar"/>
                        </fileset>
                        <fileset dir="${lib.dir}">
                            <include name="**/*.jar"/>
                        </fileset>
                        <fileset dir="${builder.dir}/antlibs">
                            <include name="**/*.jar"/>
                        </fileset>
                        <pathelement path="${module.temp.dir}/junit_classes" />
                        <fileset dir="${ant.home}/lib">
                            <include name="**/*.jar"/>
                        </fileset>
                    </classpath>
                    <batchtest fork="yes" todir="${junit.result.dir}">
                        <fileset dir="${basedir}/tests/src">
                            <include name="**/*Test*.java"/>
                        </fileset>
                    </batchtest>
                </junit>
                <delete dir="${module.temp.dir}/backup/" failonerror="false" />
                
                <emma enabled="true" verbosity="quiet">
                    <report sourcepath="${src.dir}">
                        <infileset dir="${module.temp.dir}" includes="*_junit.emma" />
                        <txt outfile="${module.temp.dir}/coverage.txt" />
                        <html outfile="${module.temp.dir}/coverage.html" />
                        <xml outfile="${module.temp.dir}/coverage.xml" />
                    </report>
                </emma>
                <if>
                    <available file="${module.temp.dir}/coverage.txt" />
                    <then>
                        <loadfile property="message" srcFile="${module.temp.dir}/coverage.txt"/>
                        <echo message="${message}"/>
                    </then>
                </if>
            </then>
        </if>
        <if>
            <resourcecount count="0" when="gt">
                <fileset dir="${components.temp.dir}/" includes="*/*.emma" />
            </resourcecount>
            <then>
                <emma enabled="true" verbosity="quiet">
                    <report sourcepath="${src.dir}">
                        <infileset dir="${components.temp.dir}/" includes="*/*.emma" />
                        <txt outfile="${build.temp.dir}/coverage.txt" />
                        <html outfile="${build.temp.dir}/coverage.html" />
                        <xml outfile="${build.temp.dir}/coverage.xml" />
                    </report>
                </emma>
            </then>
        </if>
    </target>
    
    
    <target name="antunit">
        <mkdir dir="${build.temp.dir}" />
        <if>
            <available type="dir" file="${basedir}/tests"/>
            <then>
                <trycatch property="antunitModule.error">
                    <try>
                        <!-- Temporarly replacing the jar file
                             with an instrumented one to do the 
                             actual coverage measurment
                         -->
                        <mkdir dir="${module.temp.dir}/backup" />
                        <delete>
                            <fileset dir="${module.temp.dir}" includes="**/*_antunit.emma" />
                        </delete>
                        <copy file="${jar.file}" todir="${module.temp.dir}/backup/" />
                        <emma enabled="true" verbosity="quiet">
                            <instr destdir="${module.temp.dir}/backup/" metadatafile="${module.temp.dir}/metadata_antunit.emma" merge="true" mode="overwrite">
                                <instrpath>
                                    <fileset dir="${module.temp.dir}/backup/" includes="${ant.project.name}.jar" />
                                </instrpath>
                            </instr>
                        </emma>
                        <pathconvert pathsep="${path.separator}" property="python.path">
                            <fileset dir="${lib.dir}" includes="zipsafe/*.egg"/>
                            <dirset dir="${lib.dir}" includes="zipnotsafe/*.egg"/>
                            <dirset dir="${lib.dir}" includes="zipsafe" />
                        </pathconvert>
                        <exec osfamily="windows" executable="cmd" dir="${basedir}/tests" failonerror="true">
                            <env key="ANT_ARGS" value="-lib ${lib.dir} -lib ${module.temp.dir}/backup/${ant.project.name}.jar -lib ${builder.dir}/antlibs -Dpython.path=${python.path}" />
                            <env key="PYTHONPATH" value="${python.path}" />
                            <env key="JYTHONPATH" value="${python.path}" />
                            <arg line="/c build.bat unittest " />
                            <arg value="-Dbuilder.dir=${builder.dir}" />
                        </exec>
                        <exec osfamily="unix" executable="/bin/bash" dir="${basedir}/tests" failonerror="true">
                            <env key="ANT_ARGS" value="-lib ${lib.dir} -lib ${module.temp.dir}/backup/${ant.project.name}.jar -lib ${builder.dir}/antlibs -Dpython.path=${python.path}" />
                            <env key="PYTHONPATH" value="${python.path}" />
                            <env key="JYTHONPATH" value="${python.path}" />
                            <arg value="./bld.sh" />
                            <arg line="unittest" />
                            <arg value="-Dbuilder.dir=${builder.dir}" />
                        </exec>
                        <move todir="${module.temp.dir}">
                            <fileset dir="${basedir}/tests" includes="**/coverage.ec" />
                            <chainedmapper>
                                <filtermapper>
                                    <replacestring from="/" to="_" />
                                    <replacestring from="\" to="_" />
                                </filtermapper>
                                <regexpmapper from="^(.*)\.ec$$" to="\1_antunit.emma" />
                            </chainedmapper>
                        </move>
                        <emma enabled="true" verbosity="quiet">
                            <report sourcepath="${src.dir}">
                                <infileset dir="${module.temp.dir}/" includes="*_antunit.emma" />
                                <txt outfile="${module.temp.dir}/coverage_antunit.txt" />
                                <html outfile="${module.temp.dir}/coverage_antunit.html" />
                                <xml outfile="${module.temp.dir}/coverage_antunit.xml" />
                            </report>
                        </emma>
                        <if>
                            <available file="${module.temp.dir}/coverage_antunit.txt" />
                            <then>
                                <loadfile property="message" srcFile="${module.temp.dir}/coverage_antunit.txt" />
                                <echo message="${message}" />
                            </then>
                        </if>
                    </try>
                    <catch>
                        <fail message="${antunitModule.error}" />
                    </catch>
                    <finally>
                        <delete>
                            <fileset dir="${module.temp.dir}" includes="backup/*.jar" />
                        </delete>                                
                    </finally>
                </trycatch>
            </then>
        </if>
        <if>
            <resourcecount count="0" when="gt">
                <fileset dir="${components.temp.dir}/" includes="*/*.emma" />
            </resourcecount>
            <then>
                <emma enabled="true" verbosity="quiet">
                    <report sourcepath="${src.dir}">
                        <infileset dir="${components.temp.dir}/" includes="*/*.emma" />
                        <txt outfile="${build.temp.dir}/coverage.txt" />
                        <html outfile="${build.temp.dir}/coverage.html" />
                        <xml outfile="${build.temp.dir}/coverage.xml" />
                    </report>
                </emma>
            </then>
        </if>
    </target>
    
    
    <target name="unittest" depends="antunit"/>
    
</project>


