<!--
   Build script for the project "Crypto.SHA1".

   This script is an Ant script using Ivy extension to resolve dependencies.
   You will need both Ant (http://ant.apache.org/) and Ivy (http://ant.apache.org/ivy) to use it.
-->
<project name="sha1" default="verify" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
        Runs JsUnit tests. This target saves a lot of verbosity in the projects' build scripts.
        
        Firefox 3 users : change the security.fileuri.strict_origin_policy parameter to 'false' for the following target to work
        
        // static configuration
        @in jsunit.dir          the directory where JsUnit resides
        @in jsunit.coreJs       the jsUnitCore.js file
        @in jsunit.testRunner   the test runner's HTML page
        -->
    <taskdef name="jsunit" classname="net.jsunit.ant.StandaloneTestTask" classpath="/home/cbonar/src/jsunit/java/bin" />

    <!-- ==================== -->
    <!-- Settings -->
    <!-- ==================== -->

    <!-- default properties, specific to this project -->
    <property file="local.properties"/>
    <property file="build.properties"/>
    <!-- properties common to all projects -->
    <property file="../../local.properties"/>
    <property file="../../common.properties"/>


    <!-- ==================== -->
    <!-- Public targets : they are greatly inspired from the Maven build lifecycle -->
    <!-- ==================== -->


    <target name="clean" description="Restores the initial state of the project, deleting any generated file.">
        <delete includeemptydirs="true" dir="${target.dir}"/>
        <delete includeemptydirs="true" dir="${build.dir}"/>
    </target>


    <target name="validate" description="Validates the project is correct and all necessary information is available.">
        <!-- Retreives dependencies with ivy -->
        <ivy:retrieve/>
        <!-- Creates necessary directories -->
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${target.dir}" />
    </target>


    <target name="compile" depends="validate" description="Compiles the source code of the project.">

        <!-- We use this target to copy all required files for the tests together in a consistent hierarchy -->
        <!--
        <copy file="${jsunit.coreJs}" todir="${jsunit.tmp}"/>
        <copy todir="${jsunit.tmp}" overwrite="true">
            <resources refid="jsunit.in.files"/>
        </copy>
        -->

    </target>


    <!-- NOTE : These tests should not require the code be packaged or deployed. -->
    <target name="test" depends="compile" description="Tests the compiled source code using a suitable unit testing framework.">

        <jsunit showoutput="true"
                printsummary="true"
                filtertrace="false"
                jsUnitRoot="${jsunit.dir}"
                haltonfailure="true"
                haltonerror="true"
                >
            <sysproperty key="browserFileNames" value="${jsunit.browsers}"/>
            <sysproperty key="port" value="45678"/>
            <sysproperty key="logsDirectory" value="${tests.logs}"/>
            <test todir="${tests.logs}">
                <fileset dir="${src.dir}" includes="**/*.js"/>
                <fileset dir="${tests.dir}" includes="**/*.js"/>
            </test>
            <!--
            <scripts dir="${src.dir}" includes="**/*.js"/>
            <scripts dir="${tests.dir}" includes="**/*.js"/>
            -->
            <classpath>
                <fileset dir="${jsunit.dir}/java/lib">
                    <include name="*.jar"/>
                </fileset>
                <!--
                    <fileset dir="${jsunit.dir}/java/bin">
                        <include name="jsunit.jar"/>
                    </fileset>
                -->
                <dirset dir="/home/cbonar/src/jsunit/java/bin"/>
                <dirset dir="${jsunit.dir}/java/config"/>
            </classpath>
            <formatter type="xml"/>
        </jsunit>

    </target>


    <target name="package" depends="test, doc, build-default, build-compressed" description="Takes the compiled code and package it in its distributable format.">
    </target>


    <target name="integration-test" depends="package" description="Processes and deploys the package if necessary into an environment where integration tests can be run.">

        <!-- Testing the compressed version of the library -->

        <!-- Replaces the tested library with the compressed version -->
        <!-- FIXME : it's not clean : it should not override a file already in build.dir -->
        <!--
        <copy file="${target.dir}/${ant.project.name}-compressed-${ivy.revision}.js" tofile="${build.dir}/sha1.js"/>
        -->

        <!--
        <resources id="jsunit.in.files">
            <file file="${build.dir}/sha1.js"/>
            <fileset dir="${tests.dir}"/>
        </resources>
        -->

        <!--
        <antcall target="jsunit">
            <reference refid="jsunit.in.files"/>
            <param name="jsunit.in.testsuite" value="AllTests.html"/>
            <param name="jsunit.out.logs" value="${tests.logs}"/>
        </antcall>
        -->

    </target>


    <target name="verify" depends="integration-test" description="Runs any checks to verify the package is valid and meets quality criteria.">
        <!-- nothing to do -->
    </target>


    <target name="install" depends="verify" description="Installs the package into the local repository, for use as a dependency in other projects locally.">
        <ivy:publish resolver="local" artifactspattern="${lib.dir}/[artifact]-[revision].[ext]" overwrite="true" />
    </target>


    <target name="deploy" depends="verify" description="Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.">
        <!-- nothing to do -->
    </target>


    <target name="cruisecontrol" depends="verify" description="This target can be used to trigger a build useable with CruiseControl.">
    </target>


    <!-- ==================== -->
    <!-- Internal targets -->
    <!-- ==================== -->


    <!-- Generates developer documentation. -->
    <target name="doc" depends="validate">
        <!-- Generates a report of dependencies -->
        <ivy:report todir="${doc.dir}"/>
        <!-- Generates the API doc -->
        <exec command="${jsdoc}" failonerror="true">
            <arg value="-d"/> <arg value="${doc.dir}"/>
            <arg value="--no-lexical-privates"/>
            <arg value="--package-naming"/>
            <arg value="--nested-file-naming"/>
            <arg line="--project-name Crypto.SHA1"/>
            <arg value="${src.dir}"/>
        </exec>
    </target>


    <!-- Builds the default package. -->
    <target name="build-default" depends="validate">
        <!-- Simply copies the library to the target dir (nothing more to do for the standard library) -->
        <copy file="${src.dir}/sha1.js" tofile="${target.dir}/${ant.project.name}-${ivy.revision}.js" />
    </target>


    <!-- Builds the compressed version of the package. -->
    <target name="build-compressed" depends="validate">
        <!-- Creates the compressed version of the library -->
        <java jar="${yuicompressor.jar}" fork="true" output="${target.dir}/${ant.project.name}-compressed-${ivy.revision}.js" failonerror="true">
            <arg line="--type js" />
            <arg file="${src.dir}/sha1.js"/>
        </java>
    </target>


    <!-- ==================== -->
    <!-- CruiseControl targets -->
    <!-- ==================== -->

</project>
