<?xml version="1.0"?>
<project name="JFlex" default="help">

  <property name="version" value="1.6.1" />
  <property name="bootstrap.version" value="1.6.0" />
  <property name="junit.version" value="4.11" />
  <property name="cup.version" value="11a" />
    
  <!-- use any of these files to override properties -->
  <property file="build.properties" />
  <property file="${user.home}/.jflex.properties" />
  
  <!-- location of development tools necessary for the build -->
  <property name="lib.dir" value="lib"/>
    
  <!-- override these if you want to use your own versions -->
  <property name="bootstrap.jflex.jar" value="${lib.dir}/jflex-${bootstrap.version}.jar" />
  <property name="cup.jar" value="${lib.dir}/java-cup-${cup.version}.jar" />
  <property name="junit.jar" value="${lib.dir}/junit-${junit.version}.jar" />

  <!-- where to get tool jars from -->
  <property name="maven.central.url" value="http://central.maven.org/maven2" />
  <property name="bootstrap.jflex.jar.url" 
  	        value="${maven.central.url}/de/jflex/jflex/${bootstrap.version}/jflex-${bootstrap.version}.jar" />
  <property name="junit.jar.url"
            value="${maven.central.url}/junit/junit/${junit.version}/junit-${junit.version}.jar" />

  <!-- where build output goes, including the jflex jar -->
  <property name="build.dir" value="build"/>
  <property name="generated.sources.dir" value="${build.dir}/generated-sources"/>
  <property name="compile.output.dir" value="${build.dir}/classes"/>
  <property name="test.compile.output.dir" value="${build.dir}/test-classes"/>
  <property name="tests.dir" value="${build.dir}/tests"/>

  <!-- where the output jar goes -->
  <property name="output.jar.name" value="${build.dir}/jflex-${version}.jar" />
  	        
  <target name="help" description="show help message">
  	<echo message="Ant build file for JFlex. Useful targets:"/> 
  	<echo message=""/> 
  	<echo message="gettools:  download development tools for the build"/> 
  	<echo message="clean:     remove all generated and compiled files"/>
    <echo message="compile:   generates sources and compiles non-test classes"/>
  	<echo message="jar:       make ${output.jar.name}"/>
    <echo message="build:     complete build from scratch (clean jar)"/>
    <echo message="test:      run the unit tests"/>
  	<echo message=""/> 
  	<echo message="Use the gettool target or override the *.jar properties"/>
  	<echo message="before you do the first build."/> 
  </target> 

  <target name="gettools" description="download development tools">
    <get src="${bootstrap.jflex.jar.url}" dest="${bootstrap.jflex.jar}"/>
    <get src="${junit.jar.url}" dest="${junit.jar}"/>
  </target>

  <target name="build" depends="clean,jar" 
          description="complete build from scratch (clean jar)"/>

  <target name="clean" description="remove all generated and compiled files">
    <delete includeemptydirs="true" failonerror="false">
      <fileset dir="${build.dir}" includes="**/*"/>
    </delete>
    <delete file="${lib.dir}/jflex-${version}.jar" />
  </target>

  <target name="jar" depends="compile,-copy-resources" 
          description="make ${output.jar.name}">
    <jar basedir="${compile.output.dir}" jarfile="${output.jar.name}">
      <manifest>
        <attribute name="Main-Class" value="jflex.Main"/>
      </manifest>
    </jar>
    <copy file="${output.jar.name}" todir="${lib.dir}" />
  </target>

  <target name="compile" depends="-generate-scanner,-generate-parser"
          description="generate sources and compile all non-test classes">
    <mkdir dir="${compile.output.dir}" />
    <javac destdir="${compile.output.dir}"
           debug="on"
           includeantruntime="true">
      <src path="src/main/java"/>
      <src path="${generated.sources.dir}"/>
    </javac>
  </target>
    
  <target name="test" depends="compile,-copy-resources,-compile-test">
    <mkdir dir="${tests.dir}"/>
    <junit dir="." printsummary="true" errorproperty="junit.failed">
      <classpath>
        <file file="${junit.jar}"/>
        <pathelement location="${compile.output.dir}"/>
        <pathelement location="${test.compile.output.dir}"/>
      </classpath>
      <assertions>
        <enable/>
      </assertions>
      <batchtest fork="yes" todir="${tests.dir}">
        <formatter type="plain"/>
        <fileset dir="src/test/java">
          <include name="**/*Test*.java"/>
        </fileset>
      </batchtest>
    </junit>
    <fail if="junit.failed"/>
  </target>

  <target name="-copy-resources">
    <copy todir="${compile.output.dir}">
      <fileset dir="src/main/resources"/>
    </copy>
  </target>

  <target name="-generate-scanner">
    <taskdef classname="jflex.anttask.JFlexTask" 
             name="jflex" classpath="${bootstrap.jflex.jar}"/>
    <mkdir dir="${generated.sources.dir}"/>
    <jflex file="src/main/jflex/LexScan.flex"
           destdir="${generated.sources.dir}"
           skeleton="src/main/jflex/skeleton.nested-1.6.0"
           nobak="on"/>
  </target>

  <target name="-generate-parser">
    <taskdef classname="java_cup.anttask.CUPTask" 
             name="cup" classpath="${cup.jar}"/>
    <mkdir dir="${generated.sources.dir}"/>
    <cup srcfile="src/main/cup/LexParse.cup" 
         destdir="${generated.sources.dir}" 
         interface="true" 
         parser="LexParse"/>
  </target>
    
  <target name="-compile-test" depends="compile">
    <mkdir dir="${test.compile.output.dir}"/>
    <javac srcdir="src/test/java"
           destdir="${test.compile.output.dir}"
           classpath="${compile.output.dir}:${junit.jar}"
           includeantruntime="true"/>
  </target>
</project>
