<?xml version="1.0" encoding="utf-8" ?>
 <!--
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 -->
<project name="etch-ant-task" basedir="." default="help">
    <description>Etch-Ant-Task implementation</description>
    <property name="Etch.basedir" location="${basedir}/../.." />
    <import file="${Etch.basedir}/build-support/etch.includes.xml" />

    <!-- Static properties of the sub-project -->
    <property name="proj"                 location="${Etch.basedir}/plugins/ant" />
    <property name="target"               location="${proj}/target" />
    <property name="src"                  location="${proj}/src" />        
    <property name="generatedSrc"         location="${target}/generated-sources" />
    <property name="classesDirectory"     location="${target}/classes" />
    <property name="testResultsDirectory" location="${target}/test-results" />

    <!-- MACRO: init-target -->
    <macrodef name="init-target" >
        <sequential>
            <delete dir="${classesDirectory}"   failonerror="false" quiet="true" />
            <mkdir dir="${classesDirectory}" />
            <mkdir dir="${classesDirectory}/main" />
            <mkdir dir="${classesDirectory}/test" />
        </sequential>
    </macrodef>

    <!-- MACRO: compile-sources -->
    <macrodef name="compile-sources" >
        <sequential>            
            <!-- compile ant plugin -->
            <javac  debug="${Etch.javac.debug}"
                    target="1.5"
                    optimize="${Etch.javac.optimize}"
                    destdir="${classesDirectory}/main" >
                <src path="${src}/main/java" />
                <exclude name="**/.svn/**" />
                <classpath refid="Etch.dependencies.jar.paths" />
                <classpath>
                    <pathelement location="${Etch.HOME}/lib/${etch-compiler.jar}" />
                    <pathelement location="${Etch.HOME}/lib/${etch-util.jar}" />
                </classpath>
            </javac>

            <!-- compiler plugin resources -->
            <copy todir="${classesDirectory}/main" >
                <fileset dir="${src}/main/resources" excludes="**/.svn/**" />
            </copy>

            <!-- Compile Tests -->
            <!--
            <javac  debug="${Etch.javac.debug}"
                    target="1.5"
                    optimize="${Etch.javac.optimize}"
                    destdir="${classesDirectory}/test" >
                <src path="${src}/test/java" />
                <exclude name="**/.svn/**" />
                <classpath refid="Etch.dependencies.jar.paths" />
                <classpath>
                    <pathelement location="${classesDirectory}/main" />
                    <pathelement location="${Etch.HOME}/lib/${etch-compiler.jar}" />
                </classpath>
            </javac>
            -->

            <!-- compiler plugin resources -->
            <!--
            <copy todir="${classesDirectory}/test" >
                <fileset dir="${src}/test/resources" excludes="**/.svn/**" />
            </copy>
            -->
        </sequential>
    </macrodef>
    
    <!-- MACRO: bundle-jars -->
    <macrodef name="bundle-jars" >
        <attribute name="dist" default="${Etch.dist}" />
        <sequential>
            <mkdir dir="@{dist}/lib" />
            
            <!-- CREATE jars -->

            <!-- Package up etch-ant-task jar -->
            <jar jarfile="@{dist}/lib/${etch-ant-task.jar}" >
                <manifest>
                    <attribute name="Copyright"    value="${Etch.copyrightNotice}" />
                    <attribute name="Version"      value="${Etch.version}" />
                    <attribute name="LongVersion"  value="${Etch.longversion}" />
                    <attribute name="Build-Tag"    value="${Etch.buildTag}" />
                    <attribute name="SVN-Revision" value="${Etch.runtime.revisionNumber}" />
                    <attribute name="Class-Path"   value="${etch-compiler.jar}" />
                </manifest>
                <metainf dir="${Etch.basedir}">
                    <include name="NOTICE.txt" />
                    <include name="LICENSE.txt" />
                </metainf>
                <fileset dir="${classesDirectory}/main"/>
            </jar>

            <!-- CREATE source archives -->
            
            <!-- package up etch-ant-task src -->
            <zip destfile="@{dist}/lib/${etch-ant-task-src.zip}" >
                <fileset dir="${src}/main/java" excludes="**/.svn/**" />
                <fileset dir="${src}/main/resources" excludes="**/.svn/**" />
            </zip>

        </sequential>
    </macrodef>
    
    <!-- INIT TARGET -->
    <!-- Modify this target to define project specific properties that can only be set at runtime -->
    <target name="do-init">
        <delete dir="${target}" failonerror="false" quiet="true" />

        <mkdir dir="${target}" />
        <mkdir dir="${generatedSrc}" />
        <mkdir dir="${classesDirectory}" />
        <mkdir dir="${testResultsDirectory}" />
    </target>

    <!-- CLEAN TARGET -->
    <target name="do-clean">
        <delete dir="${target}" />
    </target>

    <!-- BUILD TARGET -->
    
    <target name="generate-sources" >
        <!-- Generate version info -->
    </target>

    <target name="compile-for-dist" >
        <!-- Initialize target directories -->
        <init-target />

        <!-- Compile Source -->
        <compile-sources />

        <!-- Bundle Jars -->
        <bundle-jars dist="${Etch.dist}" />
    </target>
    
    <target name="compile-for-clover" if="Clover.enabled" >

        <echo message="Rebuilding with clover" />

        <!-- initialize-clover -->
        <initialize-clover suffix="etchanttask" >
            <fileset dir="${src}/main/java">
                <include name="**/*.java" />
            </fileset>
            <!--
            <testsources dir="${src}/test/java">
                <include name="**/*.java" />
            </testsources>
            -->
        </initialize-clover>
        
        <!-- Initialize target directories -->
        <init-target />
        
        <!-- Compile Source -->
        <compile-sources />
        
        <!-- Bundle Jars -->
        <bundle-jars dist="${Etch.clover-dist}" />

    </target>
    
    <target name="do-build" depends="generate-sources,compile-for-dist,compile-for-clover" />

    <!-- TEST TARGET -->
    <target name="do-test">
        
        <!-- Run Unit Tests -->
        <!--
        <junit printsummary="yes" haltonfailure="no" dir="${classesDirectory}"
            errorProperty="build.tests.fail" failureProperty="build.tests.fail">
            <classpath>
                <pathelement location="${classesDirectory}/main" />
                <pathelement location="${classesDirectory}/test" />
                <pathelement location="${Etch.dependency.junit.jar}" />
                <pathelement location="${Etch.HOME}/lib/${etch-compiler.jar}" />
                <pathelement location="${Etch.dependency.clover.jar}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="true" todir="${testResultsDirectory}">
                <fileset dir="${src}/test/java">
                    <include name="**/*.java" />
                </fileset>
            </batchtest>
        </junit>
        -->
    </target>

    <!-- POSTBUILD TARGET -->
    <target name="do-postbuild">
    </target>

    <target name="do-publish" if="build.tests.fail">
        <!-- Set flag file if any tests failed -->
        <touch file="${Etch.runtime.tests.fail}"/>
    </target>

</project>
