<?xml version="1.0" encoding="UTF-8"?>
<project name="swing-template-build">
     
 <!-- ***************************************************
            Inizializzazione delle proprieta'
      *************************************************** -->
        
    <target name="-init-folders" depends="">
        <!-- Cartelle -->
        <property name="src.dir" location="./src" />
        <property name="test.src.dir" location="./test" />
        <property name="build.dir" location="./build/classes" />
        <property name="test.dir" location="./build/test" />
        <property name="test.build.dir" location="${test.dir}/classes" />
        <property name="test.report.dir" location="${test.dir}/report" />
        <property name="test.data.dir" location="${test.src.dir}/dati" />
        <property name="dist.dir" location="./dist" />
        <property name="utilita.src.dir" location="../utilita/src" />
        <property name="utilita.build.dir" location="../utilita/build/classes" />
        <property name="javadoc.dir" location="./dist/javadoc" />
        <property name="utilita.javadoc.dir" location="../utilita/dist/javadoc" />    
        <property name="misc.dir" location="./varie" />
        <property name="resources.dir" location="${misc.dir}/risorse" />
        <condition property="test.data.dir.present">
            <available file="${test.data.dir}" type="dir" />    
        </condition>
        <condition property="resources.dir.present">
            <available file="${resources.dir}" type="dir" />    
        </condition>
    </target>

    <target name="-init-dependent" depends="">
        <property file="./dependent.properties" />
        <echo message="lib.dir = ${lib.dir}" />
        <echo message="mylib.dir = ${mylib.dir}" />
    </target>

    <target name="-init-classpath" depends="-init-folders, -init-dependent">
        <property name="build.sysclasspath" value="ignore" />
        <path id="compile.classpath">
            <pathelement location="${build.dir}" />
            <pathelement location="${mylib.dir}/utilita.jar" />
        </path>
        <path id="run.classpath">
            <path refid="compile.classpath" />
        </path>
        <path id="test.classpath">
            <path refid="compile.classpath" />
            <pathelement location="${test.build.dir}" />
            <pathelement location="${lib.dir}/junit.jar" />
            <pathelement location="${lib.dir}/jfcunit.jar" />
            <pathelement location="${lib.dir}/jakarta-regexp.jar" />
            <pathelement location="${lib.dir}/xerces.jar" />
        </path>
    </target>    
    
    <target name="-init-release" if="release.build" depends="">
        <echo message="Build di release" />
        <property name="build.debug" value="off" />
        <property name="build.debuglevel" value="lines" />
        <property name="build.optimize" value="on" />
    </target>
    
    <target name="-init-debug" unless="release.build" depends="">
        <echo message="Build di debug" />
        <property name="build.debug" value="on" />
        <property name="build.debuglevel" value="lines,vars,source" />
        <property name="build.optimize" value="off" />
    </target>

    <target name="init" depends="-init-folders, -init-classpath, -init-release, -init-debug" description="inizializza le proprieta' e i path">
        <property name="main.class" value="it.unibas.${ant.project.name}.controllo.Controllo" />
        <echo message="${main.class}" />        
    </target>    
    
 <!-- ***************************************************
             Clean
      *************************************************** -->
    <target name="clean" depends="init" description="ripulisce le cartelle riproducibili">
        <delete dir="${build.dir}" />
        <delete dir="${test.dir}" />
        <delete dir="${dist.dir}" /> 
    </target>

   <!-- ***************************************************
             Preparazione
      *************************************************** -->    
    <target name="-create-misc-dir" depends="init">
        <mkdir dir="${misc.dir}" />
    </target>
    
    <target name="prepare" depends="-create-misc-dir" description="ricrea le cartelle riproducibili">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${test.build.dir}" />
        <mkdir dir="${test.report.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>
    
    <target name="-create-resources-dir" depends="init" unless="resources.dir.present">
        <mkdir dir="${resources.dir}" />
    </target>
    
    <target name="-prepare-resources" depends="prepare,-create-resources-dir">
        <copy todir="${build.dir}/risorse">
            <fileset dir="${resources.dir}" />
        </copy>
    </target>

    <target name="-create-test-data-dir" depends="init" unless="test.data.dir.present">
        <mkdir dir="${test.data.dir}" />
    </target>

    <target name="-prepare-test" depends="prepare,-create-test-data-dir">
        <copy todir="${test.build.dir}/dati">
            <fileset dir="${test.data.dir}" />
        </copy>
    </target>

 <!-- ***************************************************
                      Compilazione 
      *************************************************** -->
    <target name="compile" depends="prepare, -prepare-resources" description="compila il codice">
        <javac srcdir="${src.dir}"
               destdir="${build.dir}"
               classpathref="compile.classpath" 
               debug="${build.debug}"
               debuglevel="${build.debuglevel}"
               optimize="${build.optimize}">
        </javac>
    </target>
    
    <target name="compile-test" depends="compile" description="compila i test">
        <javac srcdir="${test.src.dir}"
               destdir="${test.build.dir}"
               classpathref="test.classpath"
               debug="${build.debug}"
               debuglevel="${build.debuglevel}">
        </javac>
    </target>

 <!-- ***************************************************
                Esecuzione dei test
      *************************************************** -->
    <target name="test" depends="-prepare-test, compile-test" description="esegue i test">
        <junit haltonfailure="false"
               errorProperty="test.failed"
               failureProperty="test.failed"
               fork="true">
            <assertions>
               <enable/>
            </assertions>
            <classpath refid="test.classpath" />
            <batchtest todir="${test.report.dir}">
                <fileset dir="${test.build.dir}">
                    <include name="**/Test*.class" />
                </fileset>
            </batchtest>
            <formatter type="plain" usefile="false" />
            <formatter type="xml" />
        </junit>
        <fail if="test.failed">
            Esecuzione dell'operazione fallita a causa del fallimento dei test
        </fail>
    </target>
    
    <target name="test-single" depends="-prepare-test, compile-test">
        <junit haltonfailure="true">
            <classpath refid="test.classpath" />
            <test todir="${test.report.dir}" name="${testcase}" if="testcase" />
            <formatter type="plain" usefile="false" />
            <formatter type="xml" />
        </junit>
    </target>
 
     <!--         
     java -Djfcunit.xmlroot.record=true \
          -Djfcunit.xmlroot.classname=demo.SwingSet\
          -Djfcunit.xmlroot.testsuite=testcases.xml\
          -classpath jfcunit.jar;SwingSet.jar;jarkarta-regexp-1.2.jar;junit.jar\
          junit.swingui.TestRunner junit.extensions.jfcunit.tools.XMLRoot
     -->
    <target name="test-record" depends="compile, -prepare-test">
        <java classname="junit.textui.TestRunner">
            <classpath refid="test.classpath" />
            <sysproperty key="jfcunit.xmlroot.record" value="true" />
            <sysproperty key="jfcunit.xmlroot.classname" value="${main.class}" />
            <sysproperty key="jfcunit.xmlroot.testsuite" value="test/dati/recordtest.xml" />
            <arg value="junit.extensions.jfcunit.tools.XMLRoot" />
        </java>
    </target>

     <!--     
     java -Djfcunit.xmlroot.classname=demo.SwingSet\
          -Djfcunit.xmlroot.testsuite=testcases.xml\
          -classpath jfcunit.jar;SwingSet.jar;jarkarta-regexp-1.2.jar;junit.jar\
          junit.swingui.TestRunner junit.extensions.jfcunit.tools.XMLRoot
     -->
    <target name="replay-test" depends="compile, -prepare-test">
        <java classname="junit.textui.TestRunner">
            <classpath refid="test.classpath" />
            <sysproperty key="jfcunit.xmlroot.classname" value="${main.class}" />
            <sysproperty key="jfcunit.xmlroot.testsuite" value="test/dati/recordtest.xml" />
            <arg value="junit.extensions.jfcunit.tools.XMLRoot" />
        </java>
    </target>
     
     <!-- ***************************************************
                Build
      *************************************************** -->

    <target name="build" depends="compile" description="compila ed esegue i test">
    </target>

    <target name="rebuild" depends="clean, test, build" description="ripulisce e ricostruisce">
    </target>

    <!-- ***************************************************
                Esecuzione dell'applicazione
      *************************************************** -->
    <target name="run" depends="compile" description="esegue l'applicazione">
        <java classname="${main.class}" fork="true">
            <classpath refid="run.classpath" />
            <assertions>
               <enable />
            </assertions>
        </java>
    </target>


 <!-- ***************************************************
                Distribuzione
      *************************************************** -->
    <target name="-init-version" depends="init">
        <buildnumber file="build.properties" />
        <property file="build.properties" />    
    </target>

    <target name="javadoc" depends="compile">
        <javadoc destdir="${javadoc.dir}" 
                 sourcepath="${src.dir}"
                 defaultexcludes="yes"
                 packagenames="it.unibas.*">
            <classpath refid="compile.classpath" />
        </javadoc>
    </target>
    
    <target name="-create-manifest" depends="init, -init-version">
        <manifest file="${dist.dir}/MANIFEST.MF" >
            <attribute name="Built-by" value="${user.name}" />
            <attribute name="Implementation-Version" value="${major.version}.${minor.version}.${build.number}" />
            <attribute name="Main-Class" value="${main.class}" />
            <attribute name="Class-Path" value="lib/utilita.jar" />
        </manifest>
    </target>

    <target name="jar" depends="rebuild, -init-version, -create-manifest" description="crea il jar">
        <jar destfile="${dist.dir}/${ant.project.name}-${major.version}.${minor.version}.${build.number}.jar"
             manifest="${dist.dir}/MANIFEST.MF">
            <fileset dir="${build.dir}">
                 <include name="**/*.*" />
            </fileset>
        </jar>
    </target>

    <target name="dist" depends="rebuild, -init-version, jar, javadoc" description="crea lo zip distribuibile">
        <zip zipfile="${dist.dir}/${ant.project.name}-java-${major.version}.${minor.version}.${build.number}.zip">
            <!-- jar -->
            <zipfileset dir="${dist.dir}" prefix="${ant.project.name}-java" includes="*.jar" />
            <zipfileset dir="${mylib.dir}" prefix="${ant.project.name}-java/lib" includes="utilita.jar" />
            <zipfileset dir="${misc.dir}" prefix="${ant.project.name}-java" includes="*.bat" />
            <!-- sorgenti -->
            <zipfileset dir="${src.dir}" prefix="${ant.project.name}-java/src" />
            <!-- test -->
            <zipfileset dir="${test.dir}" prefix="${ant.project.name}-java/test" />
            <zipfileset dir="${test.src.dir}" prefix="${ant.project.name}-java/test/src" includes="**/*.java" />
            <!-- javadoc -->
            <zipfileset dir="${javadoc.dir}" prefix="${ant.project.name}-java/javadoc/${ant.project.name}" />
            <!-- <zipfileset dir="${utilita.javadoc.dir}" prefix="${ant.project.name}-java/javadoc/utilita" /> -->
            <!-- varie -->
            <zipfileset dir="${misc.dir}" prefix="${ant.project.name}-java/varie" />
        </zip>
    </target>

 <!-- ***************************************************
               Target particolari
      *************************************************** -->

    <target name="all" depends="dist">
    </target>

    <target name="debug" depends="init">
       <echoproperties />
    </target>

    <target name="default" depends="build">
    </target>
 
</project>