<?xml version="1.0" encoding="UTF-8"?>
<project name="FFDec Library" basedir="." default="build" xmlns:jacoco="antlib:org.jacoco.ant">
    <description>Builds project FFDec Library</description>  
    <!--
    TODO: Figure out how to make versioning which (not) depends(?) on GUI
    -->              
    <loadproperties srcfile="build.properties" />
    
    
    <path id="ant.lib.classpath">
        <fileset dir="${ant.lib.dir}" includes="**/*.jar"/>
    </path>
    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="ant.lib.classpath"  />
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpathref="ant.lib.classpath" />
    <taskdef name="jflex" classname="jflex.anttask.JFlexTask" classpathref="ant.lib.classpath" />
        
    
    
    <property name="jar.filename" value="${prefix.filename}"/>  
    <description>Builds project</description>      
    <property name="dist.lib.dir" value="${dist.dir}/${dist.lib.dirname}"/>    
    <patternset id="compiler.resources">
        <include name="**/?*.properties"/>        
        <include name="**/?*.bin"/>
        <include name="**/?*.xml"/>
        <include name="**/?*.txt"/>
        <include name="**/?*.gif"/>
        <include name="**/?*.png"/>
        <include name="**/?*.jpeg"/>
        <include name="**/?*.jpg"/>
        <include name="**/?*.html"/>
        <include name="**/?*.dtd"/>
        <include name="**/?*.tld"/>
        <include name="**/?*.mid"/>
        <include name="**/?*.wav"/>
        <include name="**/?*.js"/>
    </patternset>

    
   
    <target name="compile" depends="lexers">
        <mkdir dir="${compile.dir}"/>
        <mkdir dir="${lib.dir}"/>        
        <javac srcdir="${src.dir}" destdir="${compile.dir}" includes="**/*.java" target="${target.java}" source="${target.java}" debug="true"
               debuglevel="lines,vars,source" includeantruntime="false" encoding="utf-8">
            <compilerarg line="-Xlint:unchecked"/> <!-- For Java 8: -Xdiags:verbose -->
            <classpath>
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
            </classpath>
        </javac>
        <copy todir="${compile.dir}">
            <fileset dir="${src.dir}">
                <patternset refid="compiler.resources"/>
                <type type="file"/>
            </fileset>
        </copy>
    </target>
    
    <target name="compile-tests">
        <delete dir="${compile.test.dir}"/>
        <mkdir dir="${compile.test.dir}"/>
        <javac srcdir="${test.dir}" destdir="${compile.test.dir}" includes="**/*.java" target="${target.java}" source="${target.java}" debug="true" includeantruntime="false" encoding="utf-8">                        
            <classpath>
                <pathelement path="${compile.dir}"/>
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
                <fileset dir="${test.lib.dir}" includes="**/*.jar"/>
            </classpath>
        </javac>
    </target>

    <target name="-test-sethalt" depends="">
        <property name="test.halt" value="true" />
    </target>
    <target name="-test-setnohalt" unless="test.halt">
        <property name="test.halt" value="false" />
    </target>
    <target name="testhalt" depends="-test-sethalt,test">
    </target>

    <target name="test" depends="-test-setnohalt,compile,compile-tests">
        <delete dir="${test.result.dir}"/>
        <mkdir dir="${test.result.dir}"/>
        <mkdir dir="${test.result.dir}/raw/"/>
        
         <mkdir dir="${coverage.dir}" />
        <jacoco:coverage destfile="${coverage.dir}/jacoco.exec">
        <testng 
            outputDir="${test.result.dir}"
            haltOnFailure="${test.halt}" verbose="2" workingDir="${basedir}" >
        
            <classpath>
                <pathelement path="${compile.test.dir}"/>
                <pathelement path="${compile.dir}"/>
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
                <fileset dir="${test.lib.dir}" includes="**/*.jar"/>
            </classpath>
            <jvmarg value="-noverify" />
            <classfileset dir="${compile.test.dir}" includes="**/*.class" />                        
            
        </testng>      
        </jacoco:coverage>          
        <!-- Coverage report -->
        <mkdir dir="${coverage.result.dir}"/>
        
        <jacoco:report>
            <executiondata>
                <file file="${coverage.dir}/jacoco.exec" />
            </executiondata>
            <structure name="${project.name}">
                <classfiles>
                    <fileset dir="${compile.dir}" />
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${src.dir}" />
                </sourcefiles>
            </structure>
            <html destdir="${coverage.result.dir}" />
        </jacoco:report>        
    </target>
    
   
    
    <target name="nightly">
        <property name="nightly" value="true" />                                 
        <antcall target="all" />      
    </target>             
    
    <target name="-nightly-suffix" if="nightly">        
        <property name="version.suffix" value="_${git.short.tag}"/>         
    </target>
    
    <target name="-nonightly-suffix" unless="nightly">
        <property name="version.suffix" value=""/>         
    </target>
    
    
    <target name="-git-check-exists" >
        <property environment="env" />
        <available file="git" filepath="${env.PATH}" property="git.present" />
        <available file="git.exe" filepath="${env.Path}" property="git.present"/>
        <available file="git.exe" filepath="${env.PATH}" property="git.present" />
    </target>              
                            
    <target name="-do-git-tag" if="git.present">
        <exec executable="git" outputproperty="git.tag" failonerror="true">
            <arg value="rev-parse"/>
            <arg value="HEAD"/>
        </exec>  
        <exec executable="git" outputproperty="git.short.tag" failonerror="true">
            <arg value="rev-parse"/>
            <arg value="--short"/>
            <arg value="HEAD"/>
        </exec>        
        <echo level="info" message="Git tag:${git.tag}"/>
        <echo level="info" message="Git shorttag:${git.short.tag}"/>
    </target>
    
    <target name="-no-git-tag" unless="git.present">        
        <property name="git.tag" value="unknown" />
        <property name="git.short.tag" value="unk" />
        <echo level="warning" message="Warning: Git executable not found. No revision hash will be included in release." />
    </target>
    
    <target name="-git-tag" depends="-git-check-exists,-no-git-tag,-do-git-tag">        
        
    </target>  
    
    
    <target name="-dotimestamp">
        <tstamp>
            <format property="BUILTAT" pattern="MM/dd/yyyy hh:mm aa" timezone="CET"/>
            <format property="VERDATE" pattern="yyyyddMM_hhmm" timezone="CET"/>
        </tstamp>                         
    </target>
      
    <target name="-timestamp" depends="-dotimestamp,-git-tag"> 
        <echo level="info" message="Library Version: ${version}"/>
    </target>
    
    <target name="build" depends="-timestamp,-nightly-suffix,-nonightly-suffix,compile">
        <mkdir dir="${dist.dir}"/>                        
                                                     
        <propertyfile file="${compile.dir}/${properties.name}"
                      comment="This file is automatically generated - DO NOT EDIT">        
            <entry key="buildtime" value="${BUILTAT}"/>
            <entry key="build" value="${git.tag}"/>
            <entry key="builder" value="${builder}"/>             
            <entry key="version" value="${version}"/>          
            <entry key="nightly" value="${nightly}"/>
        </propertyfile>        

        <jar destfile="${dist.dir}/${jar.filename}.jar" basedir="${compile.dir}">
            
        </jar>
    </target>
    
    <target name="javadoc">
        <mkdir dir="${javadoc.dir}"/>
        <javadoc sourcepath="${src.dir}" destdir="${javadoc.dir}" windowtitle="${project.name}"
                 useexternalfile="yes">
            <fileset dir="${src.dir}" includes="**/*.java"/>
            <classpath>
                <fileset dir="${lib.dir}" includes="**/*.jar"/>
            </classpath>
        </javadoc>
    </target>
    <target name="clean">        
        <delete dir="${dist.dir}"/>
        <delete dir="${compile.test.dir}"/>
        <delete dir="${compile.dir}"/>
        <delete dir="${coverage.dir}"/>
        <delete dir="${test.result.dir}"/>
        <delete dir="${coverage.result.dir}"/>
        <delete dir="${javadoc.dir}"/>
    </target>
   
    <target name="run">        
        <ant antfile="../../build.xml" target="run" inheritall="false"/>
    </target>
            
    <target name="-lexer" if="lexer">
        <jflex file="${lexer}" destdir="${src.dir}" nobak="on"></jflex>        
    </target>
    
    <target name="lexers">       
        <pathconvert pathsep="|" property="lexers">
            <fileset dir="${lexers.dir}" includes="**/*.flex"/>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*" to="/*"/>
            </chainedmapper>
        </pathconvert>
        <script language="javascript"><![CDATA[
        var classpath = project.getProperty("lexers");
        // + '' converts from a Java string to a JavaScript string, in order
        // to have .split() behave predictably on all installations.
        // Otherwise, some versions of ANT will treat it's argument as a regex
        // while others will not.
        var parts = (classpath + '').split('|');
        for each (var part in parts) {
            project.setProperty("lexer",project.getProperty("lexers.dir")+part);
            self.project.executeTarget("-lexer");
        }
    ]]></script>
    </target>
</project>

