<?xml version="1.0"?>

<!-- 

Client-side code is compiled by using GWTCompiler which compiles client-side Java code into
JavaScript. Generated files are located under WebContent/VAADIN/widgetsets/*.

Client-side compilation is required if you create new or modify existing widgets.
You may use either this script or Vaadin Hosted Mode Browser.launch (in Eclipse)
to compile your client-side java code. 

By default Vaadin first tries to serve widgetset resources from the file system, if that
fails then files are streamed from vaadin-@version@.jar. 

See configure target to adjust this buildfile.

-->

<project name="Widgetset compile example" basedir="." default="compile-widgetset">

	<!-- 
		Update based on your project structure, by default this buildfile assumes that you 
		1. use WebContent under your project's root directory
		2. WebContent/WEB-INF/lib/vaadin-@version@.jar exists
		3. WebContent/WEB-INF/src contains your project source files
		4. gwt directory contains extracted GWT distribution
	-->
	<target name="configure">
		
		<!-- Path from this file to the project root -->
		<property name="base" 
			      value="../../../" />
		
		<!-- Location of GWT distribution -->
		<property name="gwt-location" 
			      value="${base}gwt" />
		
		<!-- Location of Vaadin JAR -->
		<property name="vaadin-jar-location" 
			      value="${base}WebContent/WEB-INF/lib/vaadin-@version@.jar" />
		
		<!-- Location of project source code -->
		<property name="src-location" 
			      value="${base}WebContent/WEB-INF/src" />

		<!-- Target where to compile server-side classes -->
		<property name="server-side-destination" 
			      value="${base}WebContent/WEB-INF/classes"/>
		
		<!-- Target where to compile the widget set -->
		<property name="client-side-destination" 
			      value="${base}WebContent/VAADIN/widgetsets" />
	</target>
		
    <!-- Modify this configuration to compile your own widgetset -->
    <target name="configure-widgetset">
        <echo>Modify this example Ant script to compile your own widget sets.</echo>

        <!-- Name of the widget set -->
        <property name="widgetset" value="com.vaadin.demo.colorpicker.gwt.ColorPickerWidgetSet"/>

        <!-- If generated automatically as a combining widget set -->
        <!-- from all the widget sets included in the class path. -->
        <!-- <property name="widgetset" value="com.vaadin.demo.widgetset.CombiningWidgetSet"/>  -->
        <!-- <property name="generate.widgetset" value="1"/>      --> 
        
        <!-- Path to the generated widget set directory. Must be  --> 
    	<!-- relative to $src-location, which must be the first   --> 
    	<!-- entry in the class path.                             --> 
        <property name="widgetset-path" value="com/vaadin/demo/widgetset"/>
    </target>

    <!-- Modify this configuration to package your own widgetset Jar. -->
    <target name="configure-jar">
        <!-- The compiled JAR name -->
        <property name="jar-destination" value="${base}colorpicker.jar"/>

        <!-- Title of the widget set (for JAR) -->
        <property name="widgetset-title" value="ColorPicker"/>
        
        <!-- Version of the widget set (for JAR) -->
        <property name="widgetset-version" value="1.0"/>
        
        <!-- Vendor of the widget set (for JAR) -->
        <property name="widgetset-vendor" value="Vaadin Ltd"/>
    </target>
	
	<!-- ================================================== -->
	<!-- Build Targets                                      -->
    <!-- ================================================== -->
	
	<target name="init" depends="configure">

		<echo>Requirements for classpath:</echo>
		<echo>  ${gwt-location}/gwt-dev.jar</echo>
		<echo>  ${gwt-location}/gwt-user.jar</echo>
        <echo>  ${gwt-location}/validation-api-1.0.0.GA.jar</echo>
        <echo>  ${gwt-location}/validation-api-1.0.0.GA-sources.jar</echo>
		<echo>  ${vaadin-jar-location}</echo>
		<echo>  ${src-location}</echo>
		<echo>Output will be written into ${client-side-destination}</echo>

		<!-- Check that files exist -->
		<fail message="Some of the required files (listed above) are missing.">
			<condition><not><resourcecount count="5">
				<filelist files="${gwt-location}/gwt-dev.jar,${gwt-location}/gwt-user.jar,${gwt-location}/validation-api-1.0.0.GA.jar,${gwt-location}/validation-api-1.0.0.GA-sources.jar,${vaadin-jar-location}"/>
			</resourcecount></not></condition>
		</fail>

		<!-- Construct and check classpath -->
		<!-- Includes paths required for both server and client-side compilation -->
		<path id="compile.classpath">
			<!-- The source location must be first, as required by generate-widgetset. -->
            <pathelement path="${src-location}" />
			<pathelement path="${server-side-destination}" />
			<pathelement path="${vaadin-jar-location}" />
			<pathelement path="${gwt-location}/gwt-user.jar" />
            <pathelement path="${gwt-location}/gwt-dev.jar" />
            <pathelement path="${gwt-location}/validation-api-1.0.0.GA.jar" />
            <pathelement path="${gwt-location}/validation-api-1.0.0.GA-sources.jar" />
		    <fileset dir="${base}WebContent/WEB-INF/lib/">
		    	<include name="*.jar"/>
		     </fileset>
		</path>
	</target>

	<!-- Compiled server-side components are needed for building the client-side -->
	<target name="compile-server-side" depends="init">
		<javac srcdir="${src-location}" destdir="${server-side-destination}">
			<classpath>
				<path refid="compile.classpath"/>
			</classpath>
		</javac>
	</target>

    <!-- Generates a combined widget set from all widget    -->
    <!-- sets in the class path, including project sources. -->
    <!-- Updates the configuration if it already exists.    -->
    <target name="generate-widgetset"
            depends="compile-server-side, configure-widgetset"
            if="generate.widgetset">
        
        <!-- Create the directory if it does not already exist. -->
        <mkdir dir="${src-location}/${widgetset-path}"/>
        
        <java classname="com.vaadin.terminal.gwt.widgetsetutils.WidgetSetBuilder"
              failonerror="yes" fork="yes" maxmemory="256m">
            <arg value="${widgetset}"/>
            <jvmarg value="-Xss1024k"/>
            <jvmarg value="-Djava.awt.headless=true"/>
            <classpath>
                <path refid="compile.classpath"/>
            </classpath>
        </java>
    </target>

	<!-- Build the widget set. -->
	<target name="compile-widgetset"
            depends="compile-server-side, generate-widgetset">
		<echo>Compiling ${widgetset}...</echo>
		
		<java classname="com.google.gwt.dev.Compiler"
			  failonerror="yes" fork="yes" maxmemory="256m">
			<arg value="-war" />
			<arg value="${client-side-destination}" />
			<arg value="${widgetset}" />
            <jvmarg value="-Xss1024k"/>
            <jvmarg value="-Djava.awt.headless=true"/>
			<classpath>
				<path refid="compile.classpath"/>
			</classpath>
		</java>
	</target>

    <!-- Build JAR -->
    <target name="package-jar"
            depends="init, configure-widgetset, configure-jar">
        <jar jarfile="${jar-destination}" compress="true">
            <manifest>
                <attribute name="Vaadin-Package-Version" value="1" />
                <attribute name="Vaadin-Widgetsets" value="${widgetset}" />
                <attribute name="Implementation-Title" value="${widgetset-title}" />
                <attribute name="Implementation-Version" value="${widgetset-version}" />
                <attribute name="Implementation-Vendor" value="${widgetset-vendor}" />
                
                <!-- The following are Vaadin-specific. -->
                <attribute name="Vaadin-License-Title" value="Apache2" />
                <attribute name="Vaadin-License-File" value="http://www.apache.org/licenses/LICENSE-2.0" />
            </manifest>
            
            <!-- The built server-side classes are here. -->
            <fileset dir="${server-side-destination}">
                <patternset>
                    <include name="**/*" />
                </patternset>
            </fileset>

            <!-- Especially all the widget set source files are required. -->
            <fileset dir="${src-location}">
                <patternset>
                    <include name="**/*" />
                </patternset>
            </fileset>
        </jar>
    </target>
</project>