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

<project name="plugintemplate" default="pack">
  <!-- Edit the file plugin.properties to set
   - scala.home
   - plugin.name
   - plugin.commandname
   - plugin.description
   - plugin.mainclass
   - version numbers
  -->
  <property file="${basedir}/plugin.properties"/>

  <property name="version" value="${version.major}.${version.minor}"/>

  <taskdef resource="scala/tools/ant/antlib.xml">
    <classpath>
      <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
      <pathelement location="${scala.home}/lib/scala-library.jar"/>
    </classpath>
  </taskdef>


  <!-- =================================================================
  End-user targets
  ================================================================== -->

  <target name="build" depends="build.main"/>
  <target name="build.all" depends="build.done"/>
  <target name="test" depends="test.all"/>
  <target name="guitest" depends="guitest.all"/>
  <target name="doc" depends="doc.done"/>
  <target name="pack" depends="pack.done"/>
  <target name="dist" depends="dist.done"/>
  <target name="clean" depends="clean.all"/>

  <!-- =================================================================
  Build targets
  ================================================================== -->

  <target name="build.main">
    <mkdir dir="build/build.main"/>
    <scalac srcdir="src"
            destdir="build/build.main"
            includes="**/*.scala">
      <classpath>
        <pathelement location="${scala.home}/lib/scala-library.jar"/>
        <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
      </classpath>
    </scalac>
  </target>

  <target name="build.test" depends="build.main">
    <mkdir dir="build/build.test"/>
    <scalac srcdir="test"
            destdir="build/build.test"
            includes="**/*.scala">
        <classpath>
          <pathelement location="${scala.home}/lib/scala-library.jar"/>
          <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
          <pathelement location="lib/scalatest.jar"/>
          <pathelement location="build/build.main"/>
        </classpath>
    </scalac>
  </target>

  <target name="build.examples" depends="build.test">
    <mkdir dir="build/build.examples"/>
    <scalac srcdir="doc/examples"
            destdir="build/build.examples"
            includes="**/*.scala">
      <classpath>
        <pathelement location="${scala.home}/lib/scala-library.jar"/>
        <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
        <pathelement location="build/build.main"/>
      </classpath>
    </scalac>
  </target>

  <target name="build.done" depends="build.examples"/>

  <!-- =================================================================
  Test targets
  ================================================================== -->

  <target name="test.all" depends="build.done">
    <!-- fork="true" is good for running the graphical mode -->
    <java classname="org.scalatest.tools.Runner" fork="true">
      <classpath>
        <pathelement location="${scala.home}/lib/scala-compiler.jar" />
        <pathelement location="${scala.home}/lib/scala-library.jar" />
        <pathelement location="lib/scalatest.jar" />
        <pathelement location="build/build.main" />
        <pathelement location="build/build.test" />
      </classpath>

      <arg value="-p" />
      <arg value="build/build.test" />

      <arg value="-o" />
    </java>
  </target>

  <target name="guitest.all" depends="build.done">
    <!-- fork="true" is good for running the graphical mode -->
    <java classname="org.scalatest.tools.Runner" fork="true">
      <classpath>
        <pathelement location="${scala.home}/lib/scala-compiler.jar" />
        <pathelement location="${scala.home}/lib/scala-library.jar" />
        <pathelement location="lib/scalatest.jar" />
        <pathelement location="build/build.main" />
        <pathelement location="build/build.test" />
      </classpath>

      <arg value="-p" />
      <arg value="build/build.test" />

      <arg value="-g" />
    </java>
  </target>

  <!-- =================================================================
  Doc targets
  ================================================================== -->

  <target name="doc.main">
    <mkdir dir="doc/api"/>
    <scaladoc srcdir="src"
              destdir="doc/api"
              includes="**/*.scala">
      <classpath>
        <pathelement location="${scala.home}/lib/scala-library.jar"/>
        <pathelement location="${scala.home}/lib/scala-compiler.jar"/>
      </classpath>
    </scaladoc>
  </target>

  <target name="doc.done" depends="doc.main"/>

  <!-- =================================================================
  Pack targets
  ================================================================== -->

  <target name="pack.main" depends="build.done">
    <mkdir dir="build/pack"/>
    <jar destfile="build/pack/${plugin.name}.jar">
      <fileset dir="build/build.main"/>
      <fileset file="plugin.properties"/>
      <fileset file="misc/scalac-plugin.xml"/>
    </jar>
  </target>

  <target name="pack.src" depends="pack.main">
    <jar destfile="build/pack/${plugin.name}-src.jar"
      basedir="src"
      includes="**/*.scala"/>
  </target>

  <target name="pack.done" depends="pack.src"/>

  <!-- =================================================================
  Dist targets
  ================================================================== -->

  <target name="dist.bin" depends="test.all,pack.done,doc.done">
    <mkdir dir="build/toolscript"/>
    <scalascript
      file="build/toolscript/${plugin.commandname}"
      class="${plugin.mainclass}"/>
    <chmod dir="build/toolscript" perm="a+rx" excludes="*.bat"/>
  </target>

  <target name="dist.archive" depends="dist.bin">
    <mkdir dir="build/dist"/>
    <tar destfile="build/dist/${plugin.name}-${version}.tgz"
         compression="gzip">
      <tarfileset prefix="lib" dir="build/pack"
                  includes="${plugin.name}.jar"/>
      <tarfileset prefix="src" dir="build/pack"
                  includes="${plugin.name}-src.jar"/>
      <tarfileset prefix="doc/${plugin.name}" dir="doc"
                  includes="README"/>
      <tarfileset prefix="doc/${plugin.name}" dir="doc"
                  includes="examples/**"/>
      <tarfileset prefix="doc/${plugin.name}" dir="doc"
                  includes="api/**"/>
      <tarfileset prefix="bin" dir="build/toolscript"
                  includes="${plugin.commandname}" mode="755"/>
      <tarfileset prefix="bin" dir="build/toolscript"
                  includes="${plugin.commandname}.bat"/>
      <tarfileset prefix="misc/scala-devel/plugins" dir="build/pack"
                  includes="${plugin.name}.jar"/>
    </tar>
  </target>

  <target name="dist.sbaz" depends="dist.archive">
    <sbaz file="build/dist/${plugin.name}-${version}.sbp"
          adfile="build/dist/${plugin.name}-${version}.advert"
          name="${plugin.name}"
          version="${version}"
          depends="scala-devel"
          desc="${plugin.description}"
          link="${plugin.sbazbaseurl}/${plugin.name}-${version}.sbp">
      <libset dir="build/pack" includes="${plugin.name}.jar"/>
      <srcset dir="build/pack" includes="${plugin.name}-src.jar"/>
      <docset dir="doc" includes="README"/>
      <docset dir="doc" includes="examples/**"/>
      <docset dir="doc" includes="api/**"/>
      <binset dir="build/toolscript"/>
      <looseset destination="misc/scala-devel/plugins">
        <fileset file="build/pack/${plugin.name}.jar"/>
      </looseset>
    </sbaz>
  </target>

  <target name="dist.done" depends="dist.sbaz"/>

  <!-- =================================================================
  Local installation
  ================================================================== -->

  <target name="install" depends="dist.done">
    <exec executable="sbaz">
      <arg line="-v install -f build/dist/${plugin.name}-${version}.sbp"/>
    </exec>
  </target>

  <target name="uninstall">
    <exec executable="sbaz">
      <arg line="remove ${plugin.name}"/>
    </exec>
  </target>

  <!-- =================================================================
  Clean targets
  ================================================================== -->

  <target name="clean.build">
    <delete dir="build/build.main" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
    <delete dir="build/build.test" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
    <delete dir="build/build.examples" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
  </target>

  <target name="clean.pack" depends="clean.build">
    <delete dir="build/pack" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
  </target>

  <target name="clean.dist" depends="clean.pack">
    <delete dir="build/dist" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
    <delete dir="build/toolscript" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
  </target>

  <target name="clean.doc">
    <delete dir="doc/api" includeemptydirs="yes"
            quiet="yes" failonerror="no"/>
  </target>

  <target name="clean.all" depends="clean.dist,clean.doc"/>
</project>
