<!--
    Example of build file for Ant
    Copyright 2003-2012 DeNova
    All rights reserved worldwide.
    
    The following build assumes that you have a JExpress project already created which builds your installers. 
    
    Search this file for the word WARNING and adapt each of the values to your system. 
     
    If your installer include custom classes and you use a JExpress subproject to find all the
    custom classes for the installer, then adapt the build-custom.xml file.
-->

<project name="Installer" default="build" basedir=".">

    <description>
        Installer build file
    </description>

    <property environment="env"/>

    <!-- release information -->
    <property name="version" value="1.0"/>
    
    <!-- directories -->
    
    <!-- WARNING: Adapt to your local system -->
    <!-- installation directory for JExpress -->
    <property name="jexpress.installed.dir" value="/usr/local/bin/JExpress"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- program name for your Java compiler -->
    <property name="compiler.name" value="javac"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- Directory where your project resides -->
    <property name="projects.dir" value="${jexpress.installed.dir}/projects"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- Installer project created by JExpress. This should be the -->
    <!-- the directory name, not the .jex file. -->
    <property name="project.name" value="Hello World"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- Application name it must match name entered on the Welcome panel,
         but be in all lower case with dashes in place of spaces -->
    <property name="app.name" value="hello-world"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- distribution directory for your app's installer -->
    <property name="dist.dir" value="${user.dir}/distribution/hello"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- JVM version to compile source files -->
    <property name="jvm.version" value="1.5"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- directory to store custom classes; if you're running on
         Windows Vista/7, you probably need to change this to a subdirectory
         of your Users directory -->
    <property name="custom.installer.destdir" value="${jexpress.installed.dir}/CustomInstaller"/>
    
    
    <!-- targets -->

    <!-- builds all custom class files and the installer -->
    <target name="build" 
        depends="init,installer.build"
        description="Build Installer"/>
        
    <!-- cleans class files, compiles, and builds a new installer -->
    <target name="cleanbuild" 
        depends="init,clean,build"
        description="Remove buildable files and then build"/>
        
    <!-- runs an already created installer -->
    <target name="run"
        depends="init"
        description="Run the Pure Java installer">
        
        <exec dir="${dist.dir}/installers" executable="java">

            <arg value="-jar"/>
            <arg path="${app.name}-cross-platform-${version}.jar"/>
        </exec>
    </target>

    <!-- compiles the Samples classes -->
    <!-- You probably want to limit the java files to the ones 
         you want included in your installer -->
    <!-- You should set the Location of classes on the Custom | Installer panel
         to match the custom.installer.destdir -->
    <!-- You should add this class as a dependency to the installer.build if your
         installer needs to use any of the sample custom classes. -->
    <target name="compile.sample">
        <javac 
            srcdir="${jexpress.installed.dir}/Samples"
            destdir="${custom.installer.destdir}"
            classpath="${jexpress.installed.dir}/JExpressInstaller"
            source="${jvm.version}"
            target="${jvm.version}"
            includeantruntime="true"
            deprecation="false"
            failonerror="true">
            
            <include name="**/*.java"/>
        </javac>
    </target>
            
    <!-- the following targets are unlikely to be invoked from the command line -->
    
    <target name="init">
        <tstamp/>
        <buildnumber/>
        
        <property name="build.compiler" value="${compiler.name}"/>

        <antcall target="jexpress.required" />
    </target>
    
    <target name="jexpress.required">
        <condition property="is.jexpress.installed">
            <available 
                classpath="${jexpress.installed.dir}"
                classname="com.denova.JExpress.Builder.JExpressAdvanced"/>
        </condition>
  
        <fail unless="is.jexpress.installed"
            message="Change the jexpress.installed.dir property to match your installation directory for JExpress."/>
    </target>
    
    <target name="installer.build"
        depends="init">
        
        <!-- build the installer -->
        <jexpress.build 
            jexpress.installed.dir="${jexpress.installed.dir}" 
            project="${projects.dir}/${project.name}" 
            buildir="${dist.dir}"
            version="${version}"/>
    </target>
       
    <target name="clean" 
        depends="init,installer.clean,dist.clean"
        description="Remove buildable files"/>
        
    <target name="installer.clean" 
        description="Remove installer's class files">
        
        <delete quiet="true">
            <fileset dir="${src.dir}">
                <include name="**/*.class"/>
            </fileset>
        </delete>
    </target>
    
    <target name="dist.clean"
        description="Remove distribution files">
        
        <!-- delete this entire subdirectory tree -->
        <delete quiet="true" dir="${dist.dir}"/>
    </target>
        
    <macrodef name="jexpress.build">
        <attribute name="jexpress.installed.dir"/>
        <attribute name="project"/>
        <attribute name="buildir"/>
        <attribute name="version"/>
        <sequential>
        <delete failonerror="false" quiet="true" file="@{jexpress.installed.dir}/errors.log"/>
        <delete failonerror="false" quiet="true" file="@{jexpress.installed.dir}/warnings.log"/>
        <echo message="Building: @{project}"/>
        <exec 
            executable="java"
            dir="@{jexpress.installed.dir}"
            failonerror="false"
            resultproperty="build.result">

            <arg value="-classpath"/>
            <arg path="@{jexpress.installed.dir}"/>

            <arg value="com.denova.JExpress.Builder.JExpressAdvanced"/>
            <arg value="@{project}"/>
            
            <arg value="-buildir"/>
            <arg value="@{buildir}"/>
            <arg value="-version"/>
            <arg value="@{version}"/>
        </exec>
        <condition property="build.failed">
            <or>
                <equals arg1="${build.result}" arg2="-1"/>
                <equals arg1="${build.result}" arg2="255"/>
            </or>
        </condition>
        <fail message="Build failed. See @{jexpress.installed.dir}/errors.log">
            <condition><isset property="build.failed"/></condition>
        </fail>
        </sequential>
    </macrodef>
    
    <!-- This target is intentionally empty.
         Simply running another target, even empty, cancels the current one. --> 
    <target name="cancel" 
        description="Cancel current Ant process"/>        
</project>