<?xml version="1.0"?>
<project name="antlr.runtime" default="usage">
	<!-- =================================================================== -->
	<!-- Initialization target                                               -->
	<!-- =================================================================== -->

	<target name="init">
    	<tstamp/>
    	<property name="debug" value="true"/>
    	<property name="project.name" value="antlr.runtime"/>
    	<property name="project.FileName" value="antlr.runtime"/>
    	<property name="astframe.FileName" value="antlr.astframe"/>

    	<property name="src.dir" value="antlr.runtime"/>
    	<property name="build.dir" value="antlr.runtime/bin"/>
    	<property name="astframe.src.dir" value="antlr.astframe"/>
    	<property name="astframe.build.dir" value="antlr.astframe/bin"/>
    	<property name="dist.dir" value="release"/>
    	<property name="compile.dir" value="${build.dir}"/>
    	<property name="astframe.compile.dir" value="${astframe.build.dir}"/>

    	<property name="backup.name" value="${project.name}-backup-${tstamp.date}-${tstamp.time}.zip" />
    	<property name="project.version" value="x.x"/>
    	<property name="year" value="2005"/>
    	
		<!-- Use script task to get this from AssemblyInfo.cs using a regular expression -->
		<script language="C#">
			<code><![CDATA[
				public static void ScriptMain(Project project) {
					string assemblyInfoFileName = Path.Combine(project.BaseDirectory, "antlr.runtime/AssemblyInfo.cs");
					StreamReader reader = File.OpenText(assemblyInfoFileName);

					try {
						// Regular expression built with Regex: http://www.organicbit.com/regex/
						Regex expression = new Regex(@"^\[assembly: AssemblyVersion\(\""([0-9]+)\.([0-9]+)\.([0-9]+)");
						string line = reader.ReadLine();
						while (line != null) {
							Match match = expression.Match(line);
							if (match.Success) {
								project.Properties["project.version"] = match.Result("$1.$2.$3");
								break;
							}
							line = reader.ReadLine();
						}
					} finally {
						// must remember to close the file or the compile may not work
						reader.Close();
					}
				}
			]]></code>
		</script>

    	<echo message="------------------- ${project.name} ${project.version} [${year}] ----------------"/>
  	</target>


  	<!-- =================================================================== -->
  	<!-- Help on usage                                                       -->
  	<!-- =================================================================== -->
  	
  	<target name="usage" depends="init">
    	<echo message="${project.name} - Nant Build file"/>
    	<echo message="-------------------------------------------------------------"/>
    	<echo message=""/>
    	<echo message=" available targets are:"/>
    	<echo message=""/>
    	<echo message="   build        --> build a developer/debug version under '${build.dir}'"/>
    	<echo message="   clean        --> cleans up the '${build.dir}' directory"/>
    	<echo message="   release 		 --> build a release version under '${dist.dir}'"/>
    	<echo message="   docs         --> builds the compiled documentation in '${dist.dir}'"/>
    	<echo message="   cleanrel     --> cleans up the '${dist.dir}' directory"/>
    	<echo message="   usage        --> show this message (default)"/>
    	<echo message=""/>
    	<echo message=" See comments in '${project.FileName}.build' file for more details."/>
    	<echo message="-------------------------------------------------------------"/>
  	</target>



	<target name="build" depends="init" description="makes a debug build, usage: 'nant clean build'">
		<property name="debug" value="true"/>
		<property name="compile.dir" value="${build.dir}"/>
		<property name="astframe.compile.dir" value="${astframe.build.dir}"/>

		<property name="dist.name" value="${project.name}-src-${project.version}.zip"/>
		<echo message="Building ${project.name}-${project.version} (debug)"/>
		<call target="compile"/>
	</target>

	<target name="release" depends="init" description="makes a release build, usage: 'nant clean release'">
		<property name="debug" value="false"/>
		<property name="compile.dir" value="${dist.dir}"/>
		<property name="astframe.compile.dir" value="${dist.dir}"/>

		<property name="dist.name" value="${project.name}-src-${project.version}.zip"/>
		<echo message="Building ${project.name}-${project.version} (release)"/>
		<call target="compile"/>
	</target>

    <target name="clean" depends="init" description="cleans up 'build' directory">
        <delete dir="${build.dir}" failonerror="false"/>
    </target>

    <target name="cleanrel" depends="init" description="cleans up 'release' directory">
        <delete dir="${dist.dir}" failonerror="false"/>
    </target>

    <target name="compile" depends="init" description="compiles the source code">
        <mkdir dir="${compile.dir}"/>
        <mkdir dir="${astframe.compile.dir}"/>

        <!-- compile antlr.runtime.dll -->
        <csc target="library" output="${compile.dir}/${project.FileName}.dll" debug="${debug}" doc="${build.dir}/${project.FileName}.xml">
            <sources basedir="${src.dir}">
                <include name="**/*.cs"/>
                <exclude name="NAnt.cs"/>
            </sources>
				<references>              
            	<include name="System.dll" />
				</references>              					
				<arg value="/nowarn:1591"/>
        </csc>

        <!-- copy the antlr.runtime assembly to the astframe build folder -->
        <copy file="${compile.dir}\${project.FileName}.dll" tofile="${astframe.compile.dir}\${project.FileName}.dll" />

        <!-- compile antlr.astframe.dll -->
        <csc target="library" output="${astframe.compile.dir}/${astframe.FileName}.dll" debug="${debug}" doc="${astframe.build.dir}/${astframe.FileName}.xml">
            <sources basedir="${astframe.src.dir}">
                <include name="**/*.cs"/>
                <exclude name="NAnt.cs"/>
            </sources>
				<references>              
	            	<include name="System.dll" />
					<include name="System.Drawing.dll" />
					<include name="System.Windows.Forms.dll" />
					<include name="${astframe.compile.dir}\${project.FileName}.dll" />
				</references>              					
				<arg value="/nowarn:1591"/>
        </csc>
    </target>

    <target name="docs" depends="build" description="build documentation">
        <echo message="Requires HtmlHelp compiler (hhc.exe) to be in the system path"/>
        <ndoc failonerror="false">
            <assemblies basedir="${build.dir}">
                <include name="${project.FileName}.dll"/>
            </assemblies>
            <documenters>
                <documenter name="MSDN">
                    <property name="OutputDirectory" value="${build.dir}/docs/MSDN" />
                    <property name="HtmlHelpName" value="${project.FileName}" />
                    <property name="HtmlHelpCompilerFilename" value="hhc.exe" />
                    <property name="IncludeFavorites" value="False" />
                    <property name="Title" value="ANTLR Runtime for .NET" />
                    <property name="SplitTOCs" value="False" />
                    <property name="DefaulTOC" value="" />
                    <property name="ShowVisualBasic" value="True" />
                    <property name="ShowMissingSummaries" value="True" />
                    <property name="ShowMissingRemarks" value="True" />
                    <property name="ShowMissingParams" value="True" />
                    <property name="ShowMissingReturns" value="True" />
                    <property name="ShowMissingValues" value="True" />
                    <property name="DocumentInternals" value="False" />
                    <property name="DocumentProtected" value="True" />
                    <property name="DocumentPrivates" value="False" />
                    <property name="DocumentEmptyNamespaces" value="False" />
                    <property name="IncludeAssemblyVersion" value="False" />
                    <property name="CopyrightText" value="" />
                    <property name="CopyrightHref" value="" />
                 </documenter>             
            </documenters> 
        </ndoc>
        <copy file="${build.dir}/docs/MSDN/${project.FileName}.chm" todir="${dist.dir}"/>
        <!-- <delete dir="${build.dir}/docs/MSDN" failonerror="false"/> -->
    </target>

    <target name="backup" depends="init" description="make local backup">
        <echo message="Creating backup ${backup.name}"/>
        <zip zipfile="${backup.name}">
            <fileset basedir=".">
                <include name="**"/>
                <exclude name="**/*.suo"/>
                <exclude name="**/*.csproj.user"/>
                <exclude name="bin/**"/>
                <exclude name="obj/**"/>
                <exclude name="build/**"/>
            </fileset>
        </zip>
    </target>
</project>
