<!--##########################
As an interm solution until we have a formatter that is not tied to an IDE, if you wish to
auto-format incubator ant files you must use eclipse with the following configuration:
Tab size: 2
|  | Use tab character instead of spaces
Maximum line width: 100
|x| Wrap long element tags
|  | Align final '>' in multi-line element tags

Long term todos:
see if project.external.bin -> project.bin safely
create flag that wipes all test results, by default wipe only current
there exists a [out-of-date] command which may allow us to avoid the .body tasks.
-->

<project name="gwt-incubator" default="build">

  <!-- "build" is the default incubator target -->
  <property name="project.target" value="build" />

  <!-- Set up initial environment -->
  <property name="project.root" location="." />

  <property name="project.build.properties" value="${project.root}/build.properties" />

  <property environment="env" />

  <property file="${project.build.properties}" />

  <!-- support old variables names -->
  <condition property="gwt.hosts.web.remote" value="${gwt.remote.browsers}">
    <isset property="gwt.remote.browsers" />
  </condition>
  <condition property="gwt.hosts.web.selenium" value="${gwt.selenium.hosts}">
    <isset property="gwt.selenium.hosts" />
  </condition>

  <!-- Also check for global-incubator.properties in the directory above -->
  <property file="${project.root}/../global-incubator.properties" />

  <!-- import common config -->
  <import file="${project.root}/common.ant.xml" />
  <import file="${project.root}/testsets.ant.xml" />

  <!-- Specify project jars -->
  <property name="project.jar" value="${project.dist}/gwt-incubator.jar" />
  <property name="project.test.jar" location="${project.test}/incubator_tests.jar" />


  <!-- With a project.external.bin specified, set project.bin to it -->
  <available file="${gwt.tools}" type="dir" property="gwt.tools.exists" />


  <!-- Setting up the project bin -->

  <condition property="project.bin" value="${project.external.bin}">
    <isset property="project.external.bin" />
  </condition>

  <property name="project.bin" value="${project.build}/bin" />

  <!-- Javadoc -->
  <property name="project.javadoc" location="${project.out}/javadoc" />

  <!-- GWT demo creation properties -->
  <property name="demo.src.dir" value="${project.root}/src-demo" />
  <property name="demo.out.dir" value="${project.out}/compiled-demos" />
  <property name="demo.js.style" value="OBFUSCATED" />
  <property name="demo.filter" value="**/*.gwt.xml" />

  <!--As the ant task selector puts tasks in the order given by the build file,
    we have sorted all common public tasks first-->
  <target name="build" depends="checksetup, compile" description="Packages the incubator jar">
    <mkdir dir="build" />
    <mkdir dir="${project.dist}" />
    <jar destfile="${project.jar}">
      <zipfileset src="${gwt.tools}/lib/javaswf/javaswf-20050404.jar" />
      <zipfileset src="${gwt.tools}/lib/w3c/sac/sac-1.3.jar" />
      <zipfileset src="${gwt.tools}/lib/w3c/flute/flute-1.3.jar" />
      <fileset dir="src">
        <exclude name="**/demos/**" />
        <exclude name="**/examples/**" />
      </fileset>
      <fileset dir="super">
      </fileset>
      <fileset dir="${project.bin}">
        <exclude name="**/demo/**" />
        <exclude name="**/demos/**" />
        <exclude name="**/examples/**" />
      </fileset>
    </jar>
  </target>

  <target name="help" description="Short description of incubator properties and tasks">
    <echo>
            Incubator properties
            gwt.home
            The location of the GWT jars you wish to compile and run against.
            
            gwt.tools
            The location of your GWT tools dir.
            
            gwt.hosts.dev.remote
            list of remote browsers to run while running junit dev tests.

            gwt.hosts.web.remote
            list of remote browsers to run while running junit web tests.
            
            project.jar
            Where should the gwt-incubator jar be created.
            
            project.external.bin
            If you a running in a very tight debug loop, you can set this property
            so ant won't recompile your source. Instead ant will use the classes
            provided by the project.external.bin directory.
            Note: No validation checking is done, so only use this flag if you are
            absolutely sure that your external.bin is up to date and valid.
            
            project.test.filter
            We will run only the tests which match the filter provided when the
            user invokes a testing task.
            
            project.build.properties
            Where you have defined the above propeties.
            
            Incubator tasks
            The incubator tasks are self-documenting. The list of public ones are
            build,help,dist,clean,all,test, test.hosted,test.web,test.remoteweb, test.selenium, build.demos.
        </echo>
  </target>


  <target name="dist"
          depends="build.demos, createJavadoc"
          description="Assembles the full incubator distribution.">
    <mkdir dir="${project.out}" />
    <mkdir dir="${project.demos}" />

    <javac srcdir="${project.root}/src-demo"
           destdir="${project.bin}"
           debug="${javac.debug}"
           debuglevel="${javac.debuglevel}"
           source="${javac.source}"
           target="${javac.target}"
           nowarn="${javac.nowarn}"
           encoding="${javac.encoding}">
      <classpath>
        <pathelement location="${project.jar}" />
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.dev.jar}" />
      </classpath>
    </javac>


    <jar destfile="${project.demos}/incubator-demos.jar">
      <fileset dir="${project.src}">
        <include name="**/demos/**" />
        <include name="**/examples/**" />
      </fileset>
      <fileset dir="${project.root}/src-demo">
        <include name="**/demos/**" />
        <include name="**/examples/**" />
      </fileset>
      <fileset dir="${project.bin}">
        <include name="**/demos/**" />
        <include name="**/examples/**" />
      </fileset>
    </jar>
    <copy todir="${project.demos}/web">
      <fileset dir="demo">
      </fileset>
    </copy>
    <mkdir dir="${project.dist}" />
    <zip destfile="${project.dist}/gwt-incubator.zip" includesFile="${project.jar}">
      <zipfileset dir="${project.out}" />
      <fileset dir="${project.dist}" includes="${project.jar}" />
    </zip>
  </target>

  <target name="apicheck"
          depends="build"
          description="Checks API compatibility to the reference GWT incubator jar provided below">
    <java failonerror="true"
          fork="true"
          classname="com.google.gwt.tools.apichecker.ApiCompatibilityChecker">
      <jvmarg line="-Xmx512m" />
      <sysproperty key="gwt.devjar" value="${gwt.dev.jar}" />
      <classpath>
        <pathelement location="${project.bin}" />
        <pathelement location="${gwt.dev.jar}" />
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.home}/gwt-api-checker.jar" />
      </classpath>
      <arg value="-gwtDevJar" />
      <arg file="${gwt.dev.jar}" />
      <arg value="-gwtUserJar" />
      <arg file="${gwt.user.jar}" />
      <arg value="-refJar" />
      <arg file="tools/api-checker/reference/gwt-incubator.jar" />
      <arg value="-configFile" />
      <arg file="tools/api-checker/config/incubator.conf" />
      <arg value="-logLevel" />
      <arg value="WARN" />
    </java>
  </target>


  <target name="clean" description="Cleans intermediate and output files">
    <delete dir="${project.build}" />
  </target>

  <target name="all"
          description=" Cleans, builds,tests, and packages incubator"
          depends="clean,build,test,dist" />


  <!-- Testing public tasks -->
  <target name="test"
          depends="build.tests"
          description="Run hosted-mode, web-mode, and remoteweb tests for this project.">
    <!--
        Run hosted and web mode tests for the platform on which this build
        is executing
        -->
    <limit failonerror="true" hours="2">
      <parallel threadsPerProcessor="${gwt.threadsPerProcessor}"
      	  threadCount="${gwt.threadCount}">
        <!-- remote tests are a no-op unless gwt.hosts.dev.remote is defined -->
        <antcall target="test.dev.remote" />
        <!-- remote tests are a no-op unless gwt.hosts.web.remote is defined -->
        <antcall target="test.web.remote" />
        <!-- selenium tests are a no-op unless gwt.hosts.dev.selenium is defined -->
        <antcall target="test.dev.selenium" />
        <!-- selenium tests are a no-op unless gwt.hosts.web.selenium is defined -->
        <antcall target="test.web.selenium" />
      </parallel>
    </limit>
  </target>

  <target name="createJavadoc">
    <delete dir="${project.javadoc}" />
    <mkdir dir="${project.javadoc}" />
    <javadoc destdir="${project.javadoc}" windowtitle="Incubator API">
      <packageset dir="src">
        <include name="com/google/gwt/widgetideas/**" />
        <include name="com/google/gwt/libideas/**" />
        <include name="com/google/gwt/gen2/**" />
        <exclude name="**/impl/**" />
      </packageset>
      <!-- we have a few impl classes that we don't want javadoc created for that must live in ui-->
      <fileset dir="src/com/google/gwt/user/client/ui">
        <exclude name="**Impl**" />
      </fileset>
      <!-- Javadoc has a bug in it that requires the jars on the class path or the index.html will not be created.-->
      <classpath>
        <pathelement location="${project.jar}" />
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.dev.jar}" />
        <pathelement location="${gwt.tools.lib}/junit/junit-3.8.1.jar" />
      </classpath>
    </javadoc>
  </target>

  <!-- helper tasks -->
  <target name="checksetup">
    <fail message="unknown platform" unless="gwt.platform" />
    <echo message="gwt.home = ${gwt.home}" />
    <echo message="gwt.tools = ${gwt.tools}" />
    <echo message="gwt.platform = ${gwt.platform}" />
    <if>
      <isset property="project.external.bin" />
      <then>
        <echo message="project.external.bin = ${project.external.bin}" />
      </then>
    </if>
  </target>

  <!-- Compile the project, unless project.external.bin is specified -->
  <target name="compile" unless="project.external.bin">
    <mkdir dir="${project.bin}" />
    <echo message="Compiling source..." />
    <gwt.javac destdir="${project.bin}">
      <classpath>
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.dev.jar}" />
        <pathelement location="${gwt.tools}/lib/javaswf/javaswf-20050404.jar" />
        <pathelement location="${gwt.tools}/lib/w3c/sac/sac-1.3.jar" />
        <pathelement location="${gwt.tools}/lib/w3c/flute/flute-1.3.jar" />
      </classpath>
    </gwt.javac>

  </target>

  <!--#############################
    Testing helper tasks
    -->

  <!-- cleans the report dir to avoid confusion when tracking down unit test failures -->
  <target name="clean.test.results">
    <echo message="cleaning all test results" />
    <delete dir="${project.test.results}" />
  </target>

  <!-- compiles the test code. Even if an exteral bin is set, ant still must compile the tests,
    as we want to only include files reachable from the test source directory-->
  <target name="compile.tests">
    <mkdir dir="${project.test.bin}" />
    <gwt.javac destdir="${project.test.bin}">
      <classpath>
        <pathelement location="${gwt.tools.lib}/junit/junit-3.8.1.jar" />
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.dev.jar}" />
        <pathelement location="${project.jar}" />
        <pathelement location="${project.bin}" />
        <pathelement location="${gwt.tools}/lib/javaswf/javaswf-20050404.jar" />
        <pathelement location="${gwt.tools}/lib/w3c/sac/sac-1.3.jar" />
        <pathelement location="${gwt.tools}/lib/w3c/flute/flute-1.3.jar" />
        <pathelement location="${gwt.tools}/lib/selenium/selenium-java-client-driver.jar" />
      </classpath>
      <src>
        <pathelement location="${project.root}/test" />
      </src>
    </gwt.javac>
  </target>

  <!-- compiles the test code for this project -->
  <target name="build.tests" depends="clean.test.results,build, compile.tests">
    <mkdir dir="${project.test}" />
    <jar destfile="${project.test.jar}">
      <fileset dir="${project.test.bin}" />
      <fileset dir="test" />
    </jar>
  </target>

  <target name="test.dev.selenium" if="gwt.hosts.dev.selenium">
    <echo message="Performing remote testing using Selenium RC at ${gwt.hosts.dev.selenium}" />
    <gwt.junit test.args="-out www -runStyle Selenium:${gwt.hosts.dev.selenium}"
               test.out="${project.test.results}/dev-selenium"
               test.cases="default.hosted.tests">
    </gwt.junit>
    <fail message="Selenium tests failed, log should be in '${project.test.results}\dev-selenium' directory"
          if="junit.failure" />
	</target>

  <target name="test.web.selenium" if="gwt.hosts.web.selenium">
    <echo message="Performing remote testing using Selenium RC at ${gwt.hosts.web.selenium}" />
    <gwt.junit test.args="-web -out www -runStyle Selenium:${gwt.hosts.web.selenium}"
               test.out="${project.test.results}/web-selenium"
               test.cases="default.web.tests">
    </gwt.junit>
    <fail message="Selenium tests failed, log should be in '${project.test.results}\web-selenium' directory"
          if="junit.failure" />
  </target>

  <target name="test.dev.remote" if="gwt.hosts.dev.remote">
    <echo message="Performing remote testing at ${gwt.hosts.dev.remote}" />
    <gwt.junit test.args="-out www -runStyle RemoteWeb:${gwt.hosts.dev.remote}"
               test.out="${project.test.results}/dev-remote"
               test.cases="default.hosted.tests">
    </gwt.junit>
    <fail message="Selenium tests failed, log should be in '${project.test.results}\dev-remote' directory"
          if="junit.failure" />
  </target>

  <target name="test.web.remote" if="gwt.hosts.web.remote">
    <echo message="Performing remote testing at ${gwt.hosts.web.remote}" />
    <gwt.junit test.args="-web -out www -runStyle RemoteWeb:${gwt.hosts.web.remote}"
               test.out="${project.test.results}/web-remote"
               test.cases="default.web.tests">
    </gwt.junit>
    <fail message="Remote tests failed, log should be in '${project.test.results}\web-remote' directory"
          if="junit.failure" />
  </target>


  <!-- Building the demos -->

  <target name="gwtc">
    <taskdef name="gwtc" classname="com.google.ant.GwtCompilerTask">
      <classpath>
        <path path="${project.bin}" />
        <pathelement location="${gwt.dev.jar}" />
      </classpath>
    </taskdef>

    <property name="gwtc.vm.maxMemory" value="512m" />

    <gwtc src="${gwtc.src.dir}"
          war="${gwtc.out.dir}"
          moduleFile="${gwtc.module.file}"
          style="${gwtc.js.style}"
          vmMaxMemory="${gwtc.vm.maxMemory}"
          vmStackSize="${java.stackSize}">
      <classpath>
        <path path="${gwtc.src.dir}" />
        <path path="${project.jar}" />
        <pathelement location="${gwt.user.jar}" />
        <pathelement location="${gwt.dev.jar}" />
        <pathelement location="${gwt.tools}/lib/w3c/sac/sac-1.3.jar" />
        <pathelement location="${gwt.tools}/lib/w3c/flute/flute-1.3.jar" />
      </classpath>
    </gwtc>
  </target>

  <target name="build.demo.helper" depends="gwtc">
    <concat destfile="${gwtc.out.dir}/index.html" append="true">
      <filterchain>
        <replaceregex flags="g" pattern="\\" replace="/" />
        <replaceregex pattern="\('.*/src-demo/" replace="('" />
      </filterchain>
&lt;script&gt;writeDemoLink('${gwtc.module.file}');&lt;/script&gt;
	</concat>
  </target>

  <target name="clean.demos">
    <delete dir="${demo.out.dir}" />
  </target>

  <target name="build.demos" depends="build, clean.demos, build.demos.body" />

  <target name="build.demos.body">
    <delete dir="${demo.out.dir}" />
    <copy file="demoindex.template" tofile="${demo.out.dir}/index.html" />
    <foreach target="build.demo.helper" param="gwtc.module.file">
      <param name="gwtc.src.dir" value="${demo.src.dir}" />
      <param name="gwtc.out.dir" value="${demo.out.dir}" />
      <param name="gwtc.js.style" value="${demo.js.style}" />
      <path>
        <fileset dir="src-demo">
          <include name="${demo.filter}" />
        </fileset>
      </path>
    </foreach>
  </target>
</project>
