<!--
    Example of build file for Ant
    Copyright 2003 DeNova, Inc.
    All rights reserved worldwide.
    
    The following build assumes that you have 2 JExpress Professional (.jex) projects. 
    
    The first, ${custom.subproject.name}, is a JExpress Professional "subproject" (i.e., 
    the Subproject box is checked on the Build panel). The subproject finds all of your 
    custom classes for the installer. The "build directory" for the subproject is the 
    JExpressInstaller subdirectory.
    
    The second project, ${installer.project.name}, is the JExpress Professional project that
    builds the installer(s) for your app.
    
    Search this file for the word WARNING and adapt each of the values to your system. If your
    installer does not include custom classes, or you do you use a subproject to find all the
    custom classes for the installer, then eliminate the various sections discussed below.
-->

<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 Professional -->
    <property name="jexpress.dir" value="/usr/local/bin/JExpressProfessional"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- program name for your Java compiler -->
    <property name="compiler.name" value="jikes"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- JExpress project that builds your installer -->
    <property name="installer.project.name" value="Hello.jex"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- distribution directory for your app's installer -->
    <!-- IMPORTANT: this directory must match the "build" directory in ${installer.project.name} -->
    <property name="dist.dir" value="${user.dir}/distribution/MyApp"/>

    <!-- WARNING: Adapt to your local system -->
    <!-- source directory for your custom classes for the installer -->
    <property name="src.dir" value="/usr/src/custom-installer"/>

    <!-- WARNING: Adapt to your local system -->
    <!-- JExpress project name that finds all your custom classes for the installer -->
    <!-- if you do not have a special project to find your custom classes, -->
    <!-- then eliminate the following property and the subproject.build target and all references to it -->
    <property name="custom.subproject.name" value="CustomSubproject.jex"/>
    
    <!-- WARNING: Adapt to your local system -->
    <!-- package subdirectory that is included in the installer -->
    <!-- For example, if your custom classes for the installer use a package -->
    <!-- called com.mypackage, then you should set the following value to com/mycompany -->
    <!-- the build will automatically delete the subdirectory JExpressInstaller/com/mycompany -->
    <!-- after it finishes building the installer so another install project won't unnecessarily -->
    <!-- include your package with that other installer. -->
    <!-- If you do not include any special packages as part of the installer, then you should --> 
    <!-- eliminate this property and the "delete dir" that uses it -->
    <property name="my.package.dir" value="com/mycompany"/>
    
    
    <!-- properties related to JExpressProfessional -->    
    <property name="jexpress.installer.dir" value="${jexpress.dir}/JExpressInstaller"/>
    <property name="purejava.installer.dir" value="platforms/PureJava/Standalone"/>
    <property name="jexpress.errors.log" value="errors.log"/>
         
           
    <!-- classpaths -->
    
    <path id="src.classpath">
        <pathelement location="${src.dir}"/>
    </path>
    <property name="src.classpath" refid="src.classpath"/>
        
    <path id="jexpress.installer.classpath">
        <pathelement location="${jexpress.installer.dir}"/>
    </path>
    <property name="jexpress.installer.classpath" refid="jexpress.installer.classpath"/>
    
    <path id="compile.classpath">
        <pathelement path="${src.classpath}"/>
        <pathelement path="${jexpress.installer.classpath}"/>
    </path>
    <property name="compile.classpath" refid="compile.classpath"/>
    
    <!-- targets -->

    <!-- builds all custom class files and the installer -->
    <target name="build" 
        depends="init,src.build,subproject.build,installer.build,subproject.clean"
        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}/${purejava.installer.dir}" executable="java">

            <arg value="-cp"/>
            <arg path="${dist.dir}/${purejava.installer.dir}/install.jar"/>

            <arg value="load"/>
        </exec>        
    </target>

    
    <!-- the following targets are unlikely to be invoked from the command line -->
    
    <target name="init" 
        description="Initialization">
        
        <tstamp/>
        <buildnumber/>
        
        <property name="build.compiler" value="${compiler.name}"/>

        <!-- make sure JExpress Professional is available -->
        <condition property="jexpress.not.configured">
            <not>
                <available file="${jexpress.dir}"/>
            </not>
        </condition>
        <antcall target="jexpress.required" />
    </target>
    
    <target name="src.build" 
        depends="init"
        description="Compile custom classes">
        
        <javac 
            srcdir="${src.dir}" 
            classpath="${compile.classpath}"
            debug="yes">
            
            <include name="**/*.java"/>            
        </javac>
    </target>
        

    <target name="subproject.build" 
        depends="init,src.build"
        description="Build custom classes subproject">        
        
        <!-- remove the error.log -->
        <delete failonerror="false" quiet="true" file="${jexpress.dir}/${jexpress.errors.log}"/>

        <!-- build the subproject with the custom classes for the installer -->
        <exec dir="${jexpress.dir}" resultproperty="subproject.result" executable="java">

            <arg value="-cp"/>
            <arg path="${jexpress.dir}"/>

            <arg value="com.denova.JExpress.Build.JExpressDeveloper"/>
            <arg value="${src.dir}/${custom.subproject.name}"/>
            <arg value="-buildir"/>
            <arg value="${jexpress.installer.dir}"/>
        </exec>
        
        <!-- show the errors log, if it exists -->
        <condition property="subprojects.errors.log.found">
             <available file="${jexpress.dir}/${jexpress.errors.log}"/>
        </condition>
        <antcall target="show.subprojects.errors" />
        
        <!-- fail if a serious error was detected during the build -->
        <condition property="subproject.error">
            <not>
                <equals arg1="${subproject.result}" arg2="0" />
            </not>
        </condition>
        <fail if="subproject.error" message="Critical error building subproject. See errors.log above."/>

    </target>
    
    <target name="installer.build" 
        depends="init,src.build,subproject.build"
        description="Build installer">
        
        <!-- remove the error.log -->
        <delete failonerror="false" quiet="true" file="${jexpress.dir}/error.log"/>
        
        <!-- build the installer -->
        <exec dir="${jexpress.dir}" resultproperty="installer.result" executable="java">

            <arg value="-cp"/>
            <arg path="${jexpress.dir}"/>

            <arg value="com.denova.JExpress.Build.JExpressDeveloper"/>
            <arg value="${src.dir}/${installer.project.name}"/>
            <arg value="-version"/>
            <arg value="${version}"/>
        </exec>
        
        <!-- show the errors log, if it exists -->
        <condition property="installers.errors.log.found">
             <available file="${jexpress.dir}/${jexpress.errors.log}"/>
        </condition>
        <antcall target="show.installer.errors" />
        
        <!-- fail if a serious error was detected during the build -->
        <condition property="installer.error">
            <not>
                <equals arg1="${installer.result}" arg2="0" />
            </not>
        </condition>
        <fail if="installer.error" message="Critical error building installer. See errors.log above."/>        
    </target>
    
    <target name="show.subprojects.errors"
        if="subprojects.errors.log.found"
        description="Display the errors log for the subprojects">

        <antcall target="show.results.log"/>        
    </target>
        
    <target name="show.installer.errors"
        if="installers.errors.log.found"
        description="Display the errors log for the installer">
        
        <antcall target="show.results.log"/>        
   </target>
        
    <target name="show.results.log"
        description="Display the results log from a JExpress build">
        
        <exec dir="${jexpress.dir}" executable="cat">

            <arg value="${jexpress.errors.log}"/>
        </exec>
    </target>
        
    <target name="clean" 
        depends="init,subproject.clean,installer.clean,dist.clean"
        description="Remove buildable files"/>
        
    <target name="subproject.clean" 
        depends="init"
        description="Remove installer's custom class files">
        
        <!-- only delete the classes in this directory; *not* in the subdirectories -->
        <delete quiet="true">
            <fileset dir="${jexpress.installer.dir}">
                <include name="*.class"/>
            </fileset>
        </delete>
        
        <!-- only delete the res in this directory; *not* in the subdirectories -->
        <delete quiet="true">
            <fileset dir="${jexpress.installer.dir}">
                <include name="*.res"/>
            </fileset>
        </delete>
        
        <!-- delete the entire subdirectory tree of my package -->
        <delete quiet="true" dir="${jexpress.installer.dir}/${my.package.dir}"/>
        
    </target>
    
    <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>
        
    <target name="jexpress.required" 
        if="jexpress.not.configured"
        description="JExpress Professional required">

        <fail message="Change the jexpress.dir property to match your installation directory for JExpress Professional."/>
        
    </target>
    
    <!-- This target is intentionally empty.
         Simply running another target, even empty, cancels the current one. --> 
    <target name="cancel" 
        description="Cancel current Ant process"/>
        
</project>

