<?xml version="1.0" encoding="UTF-8"?>

<project name="generic-build" basedir="." default="compile-src" xmlns:ivy="antlib:org.apache.ivy.ant">

    <description>
        A generic build script for a single component.
        Compiling a component with dependencies will compile and
        locally publish all its dependencies.
    </description>
    
    <import file="ant-macros.xml"/>

    <property name="component" value="${ant.project.name}"/>    
    <property name="default.code.path" value="org/limewire"/>
    
    <target name="initialize" depends="ivy-load" unless="initialized.${component}">
        <do.setup-properties/>
    </target>
    
    <target name="publish-dependencies" depends="initialize" unless="ivy.dependencies.published">
        <property name="ivy.dependencies.published" value="true"/>
        <do.subant dir="${limewire.home}" includes="**/build.xml" root="${component}" target="publish" excluderoot="true"/>
    </target>
    
    <target name="clean" depends="initialize">
        <do.clean/>
    </target>
    
    <target name="compile" depends="compile-src">
    </target>
    
    <target name="compile-src" depends="publish-dependencies">
        <do.compile-src/>
    </target>
    
    <target name="clean-src" depends="initialize">
        <do.clean-src/>
    </target>
    
    
    <target name="jar" depends="compile-src">
        <do.jar/>
    </target>
    
    <target name="javadoc" depends="compile-src">
        <do.javadoc/>
    </target>
    
    <target name="publish" depends="jar">
       <do.publish/>
    </target>
    
    <target name="clean-dist" depends="initialize">
        <do.clean-dist/>
    </target>
    
    <macrodef name="do.setup-properties" description="Sets up common properties every macro uses.">
        <sequential>
            <ivy:configure file="${limewire.ivy.settings.file}" override="true"/>
        
            <property name="initialized.${component}" value="true"/>
            
            <!-- paths for src java files and resource files -->
            <property name="limewire.components.this.home"
                      location="${limewire.components.home}/${component}"/>
            <property name="limewire.components.this.src.home"
                      location="${limewire.components.this.home}/src/main"/>    
            <property name="limewire.components.this.src"
                      location="${limewire.components.this.src.home}/java"/>                      
            <property name="limewire.components.this.src.resources"
                      location="${limewire.components.this.src.home}/resources"/>
                      
            <!-- paths for compiling src -->
            <property name="build.limewire.components.this.home"
                      location="${limewire.components.this.home}/build"/>
            <property name="build.limewire.components.this.classes"
                      location="${build.limewire.components.this.home}/classes"/>
            <property name="build.limewire.components.this.lib"
                      location="${build.limewire.components.this.home}/lib"/>    
            <!-- paths for distribution files, including javadoc & jar. -->
            <property name="dist.limewire.components.this.home"
                      location="${limewire.components.this.home}/dist"/>
            <property name="dist.limewire.components.this.javadoc"
                      location="${dist.limewire.components.this.home}/javadoc"/>
            <property name="dist.limewire.components.this.src.jar.dir"
                      location="${dist.limewire.components.this.home}/main"/>
            <property name="dist.limewire.components.this.src.jar"
                      location="${dist.limewire.components.this.src.jar.dir}/${component}.jar"/>
            <!--<property name="dist.limewire.components.this.api.jar"
                      location="${dist.limewire.components.this.home}/${component}-api.jar"/> -->
           
               <!-- the src build classpath -->
            <path id="build.limewire.components.this.src.classpath">
                <!-- TODO parameterize 'compile' with ivy-module configuration name -->
                <fileset dir="${build.limewire.components.this.lib}/compile/" includes="**/*"/> <!-- ivy src dependencies -->
            </path>
        </sequential>
    </macrodef>
    
    <macrodef name="do.clean" description="Cleans all generated directories.">
        <sequential>
            <echo message="cleaning ${component}..." />
            <do.clean-build   />
            <do.clean-dist    />
        </sequential>
    </macrodef>
    
    <macrodef name="do.resolve-dependencies" description="retreive dependencies with ivy">
        <attribute name="ivy.file" default="${limewire.components.this.home}/ivy.xml"/>
        <sequential>
            <mkdir dir="${build.limewire.components.this.lib}"/>
            <ivy:resolve  conf="*"
                          file="@{ivy.file}"
                          log="quiet"/>
            <ivy:retrieve conf="*"
                          pattern="${build.limewire.components.this.lib}/[conf]/[type]/[artifact](-[revision]).[ext]"
                          sync="true"
                          log="quiet"/>
                          
            <!-- because ivy:retrieve has sync==true, if there are no dependencies, the dir will be removed.
                 we don't want it removed, because then javac fails because the classpath is missing. -->
            <mkdir dir="${build.limewire.components.this.lib}/compile"/>
        </sequential>
    </macrodef>
    
    <macrodef name="do.compile-src" description="Compiles only the source code.">
        <element name="more-src-paths" optional="true"/>
        <attribute name="excludes" default=""/>
        <sequential>
            <echo message="compiling source for ${component}..." />
            <do.build-init/>
            <mkdir dir="${limewire.components.this.src}"/>
            <mkdir dir="${build.limewire.components.this.classes}"/>
            <do.resolve-dependencies/>
            <javac destdir        ="${build.limewire.components.this.classes}"
                    debug        ="${compile.debug}"
                    deprecation  ="${compile.deprecation}"
                    optimize     ="${compile.optimize}"
                    encoding     ="UTF-8"
                    source        ="${compile.source}"
                    target       ="${compile.target}"
                    sourcepath   =""
                    excludes     ="@{excludes}"
                    includeantruntime="false"
			 >
                <compilerarg value="-Xlint:unchecked"/>
                <src>
                    <pathelement location="${limewire.components.this.src}"/>
                    <more-src-paths/>
                </src>
                <classpath refid="build.limewire.components.this.src.classpath"/>
            </javac>
            
        </sequential>
    </macrodef>
    
    <macrodef name="do.clean-src" description="Cleans generated classfiles from compiling source.">
        <sequential>
            <echo message="cleaning ${component}..." />
            <delete dir="${build.limewire.components.this.classes}" />
        </sequential>
    </macrodef>
    
    <macrodef name="do.jar" description="Generates a jar of the classfiles.">        
        <attribute name="compress"    default="false"/>
        <sequential>
            <do.dist-init/>
            <mkdir dir="${limewire.components.this.src.resources}"/>
            <jar destfile="${dist.limewire.components.this.src.jar}"
                 compress="@{compress}"
                 basedir="${build.limewire.components.this.classes}"
                 whenmanifestonly="skip"
            >
                <fileset dir="${limewire.components.this.src.resources}"/>
            </jar>
            
        </sequential>
    </macrodef>
    
    <macrodef name="do.publish" description="Publishes the jar to the local repository.">        
        <sequential>
            <ivy:publish resolver="local"
                         overwrite="true"
                         forcedeliver="true">
                <artifacts pattern="${dist.limewire.components.this.src.jar.dir}/[artifact].[ext]"/>
            </ivy:publish>
        </sequential>
    </macrodef>
    
    <tstamp>
        <format property="timestamp.today" pattern="EEEE, MMMM d yyyy"/>
    </tstamp>
    
    <macrodef name="do.javadoc" description="Generates a javadoc for the source.">        
        <attribute name="windowtitle" default="WireShare ${component} component api for ${timestamp.today}"/>
        <attribute name="doctitle" default="WireShare ${component} component api &lt;br/&gt; for ${timestamp.today}"/>
        <attribute name="header" default="WireShare ${component} component api"/>
        <element name="more-javadoc-elements" optional="true"/>
        <attribute name="classpathrefid" default="build.limewire.components.this.src.classpath"/>
        <attribute name="sourcepath" default="${limewire.components.this.src}"/>
        <attribute name="includes" default="**/*.java"/>
        <attribute name="excludes" default=""/>
        <sequential>
            <delete dir="${dist.limewire.components.this.javadoc}"/>
            <mkdir dir="${dist.limewire.components.this.javadoc}"/>
            <javadoc destdir="${dist.limewire.components.this.javadoc}"
                     windowtitle="@{windowtitle}"
                     header="@{header}"
                     doctitle="@{doctitle}"
                     use="true"
                     author="false"
                     version="false"
                     packagenames="*"
                     useexternalfile="yes">
                <link href="http://java.sun.com/javase/6/docs/api/"/>
                <link href="http://jakarta.apache.org/commons/logging/apidocs/"/>
                <link href="http://www.icu-project.org/apiref/icu4j/"/>
                <link href="http://www.limewire.org/nightly/modules/bittorrent-api/api/"/>
                <link href="http://www.limewire.org/nightly/modules/collection/api/"/>
                <link href="http://www.limewire.org/nightly/modules/common/api/"/>
                <link href="http://www.limewire.org/nightly/modules/core-api/api/"/>
                <link href="http://www.limewire.org/nightly/modules/core-glue/api/"/>
                <link href="http://www.limewire.org/nightly/modules/core-settings/api/"/>
                <link href="http://www.limewire.org/nightly/modules/friend/api/"/>
                <link href="http://www.limewire.org/nightly/modules/gnutella-core/api/"/>
                <link href="http://www.limewire.org/nightly/modules/http/api/"/>
                <link href="http://www.limewire.org/nightly/modules/io/api/"/>
                <link href="http://www.limewire.org/nightly/modules/libtorrent/api/"/>
                <link href="http://www.limewire.org/nightly/modules/mojito/api/"/>
                <link href="http://www.limewire.org/nightly/modules/mojito-ui/api/"/>
                <link href="http://www.limewire.org/nightly/modules/net/api/"/>
                <link href="http://www.limewire.org/nightly/modules/nio/api/"/>
                <link href="http://www.limewire.org/nightly/modules/player/api/"/>
                <link href="http://www.limewire.org/nightly/modules/player-api/api/"/>
                <link href="http://www.limewire.org/nightly/modules/rest/api/"/>
                <link href="http://www.limewire.org/nightly/modules/rudp/api/"/>
                <link href="http://www.limewire.org/nightly/modules/security/api/"/>
                <link href="http://www.limewire.org/nightly/modules/setting/api/"/>
                <link href="http://www.limewire.org/nightly/modules/statistic/api/"/>
                <link href="http://www.limewire.org/nightly/modules/xmpp/api/"/>
                <link href="http://www.limewire.org/nightly/modules/xmpp-api/api/"/>
                <link href="http://www.limewire.org/nightly/modules/integrated-ui/api/"/>
                <link href="http://www.limewire.org/nightly/modules/swingui/api/"/>
                <!-- TODO: add more components as time goes on, and figure out a way to not require this. -->
                
                 <classpath refid="@{classpathrefid}"/>
                <!--sourcepath location="@{sourcepath}"/-->
                <sourcefiles>
                    <fileset dir="@{sourcepath}" includes="@{includes}" excludes="@{excludes}"/>
                </sourcefiles>
                 <more-javadoc-elements/>
                <bottom><![CDATA[Copyright &#169; 2009 <a href="http://www.limewire.org">Lime Wire LLC</a>. All Rights Reserved.]]></bottom>                
            </javadoc>
        </sequential>
    </macrodef>
    
    <macrodef name="do.build-init" description="Initializes directories for building the code.">        
        <sequential>
            <mkdir dir="${build.limewire.components.this.home}" />
        </sequential>
    </macrodef>
    
    <macrodef name="do.dist-init" description="Initializes directories for distribution.">        
        <sequential>
            <mkdir dir="${dist.limewire.components.this.home}"/>
            <mkdir dir="${dist.limewire.components.this.src.jar.dir}"/>
        </sequential>
    </macrodef>
    
    <macrodef name="do.clean-build" description="Cleans all data related to building the code.">        
        <sequential>
            <delete dir="${build.limewire.components.this.home}" />
        </sequential>
    </macrodef>
        
    <macrodef name="do.clean-dist" description="Cleans all distribution files and directories.">        
        <sequential>
            <delete dir="${dist.limewire.components.this.home}"/>
        </sequential>
    </macrodef>
    
</project>
