<?xml version="1.0"?>
<project name="Qpid.NET" default="build">

    <!-- Determines the formatter to use to format output of test results. -->
    <property name="nant.formatter" value="Plain" />

    <!-- Determines whether a 'debug' or 'release' build is to be done. Defaults to 'debug' -->
    <property name="build.config" value="debug" />

    <!-- Sets build properties consistently accross all assemblies in the project. -->
    <property name="build.version.major" value="0"/>
    <property name="build.version.minor" value="3"/>
    <property name="build.version.build" value="0"/>
    <property name="build.version.revision" value="0"/>
    <property name="build.company" value="Apache Software Foundation"/>
    <property name="build.copyright" value="Apache Software Foundation"/>
    <property name="build.description" value="Built from svn revision number: "/>

    <!-- Fileset with build files for each 'core' assembly. -->
    <fileset id="src.builds">
        <include name="Qpid.Buffer/default.build" />
        <include name="Qpid.Sasl/default.build" />
        <include name="Qpid.Messaging/default.build" />
        <include name="Qpid.Codec/default.build" />
        <include name="Qpid.Common/default.build" />        
        <include name="Qpid.Client/default.build" />	
    </fileset>
    
    <!-- Fileset with build files for 'pure unit' test assemblies. -->
    <fileset id="tests.pure.builds">
        <include name="Qpid.Buffer.Tests/default.build" />
        <include name="Qpid.Sasl.Tests/default.build" />
        <include name="Qpid.Common.Tests/default.build" />        
        <include name="Qpid.Client.Tests/default.build" />	
    </fileset>

    <!-- Fileset with build files for 'integration' test assemblies. -->
    <fileset id="tests.integration.builds">
        <include name="Qpid.Integration.Tests/default.build" />	
    </fileset>

    <!-- Other test or utility assemblies. -->
    <fileset id='other.builds'>
        <include name="TopicListener/default.build" />	
        <include name="TopicPublisher/default.build" />	
        <include name="TestClient/default.build" />	
    </fileset>

    <!-- Prepare environment for a debug build. -->
    <target name="debug">
        <property name="build.debug" value="true" />
        <property name="build.defines" value="DEBUG;TRACE"/>
    </target>

    <!-- Prepare environment for a release build. -->
    <target name="release">
        <property name="build.debug" value="false" />
        <property name="build.defines" value=""/>
    </target>
    
    <!-- Prepare environment for build. -->
    <target name="init">
        <property name="base.dir" value="${project::get-base-directory()}" />
        <property name="build.dir" value="${base.dir}/bin/${framework::get-target-framework()}/${build.config}" />
        <call target="${build.config}" />	
    </target>   
    
    <!-- Cleans up the build output directory. -->
    <target name="clean" depends="init">
        <delete dir="${build.dir}" failonerror="false" />
    </target>

    <!-- Runs 'svnversion' to get the repository revision into the build property 'build.svnversion'. -->
    <target name="svnversion" description="Runs svnversion to get the current repository version into a build script property.">
        <exec program="svnversion" output="svnversion_tmp.txt">
            <arg value="-n"/>
        </exec>

        <loadfile file="svnversion_tmp.txt" property="build.svnversion"/>
        <delete file="svnversion_tmp.txt"/>       

        <!-- For some competely retarted reason the '-n' parameter to svnversion doesn't really work under windows... 
             Here is some code to strip the unwanted newlines. -->
        <script language="C#">
            <code><![CDATA[
            public static void ScriptMain(Project project)
            {
                project.Properties["build.svnversion"] = project.Properties["build.svnversion"].Trim("\n\r".ToCharArray());
            }
            ]]>
            </code>
        </script>

    </target>

    <!-- Performs a regex find-and-replace on assembly info files, substituting fields defined as build properties. -->
    <target name="setversion" description="Stamp the version info onto assemblyinfo.cs files" depends="svnversion">

        <echo>build.svnversion = ${build.svnversion}</echo>

        <foreach item="File" property="filename">
            <in>
                <items basedir=".">
                    <include name="**\AssemblyInfo.cs"></include>
                </items>
            </in>
            <do>
                <script language="C#">
                    <code><![CDATA[
                    public static void ScriptMain(Project project)
                    {
                        // Read in the entire file to perform the substitution in.
                        StreamReader reader = new StreamReader(project.Properties["filename"]);
                        string contents = reader.ReadToEnd();
                        reader.Close();

                        // Substitute the version numbers.
                        string replacement = string.Format("[assembly: AssemblyVersion(\"{0}.{1}.{2}.{3}\")]",
                                                           project.Properties["build.version.major"],
                                                           project.Properties["build.version.minor"],
                                                           project.Properties["build.version.build"],
                                                           project.Properties["build.version.revision"]);
                        contents = Regex.Replace(contents, @"\[assembly: AssemblyVersion\("".*""\)\]", replacement);

                        // Substitute the company name and copyright.
                        replacement = string.Format("[assembly: AssemblyCompany(\"{0}\")]",
                                                    project.Properties["build.company"]);
                        contents = Regex.Replace(contents, @"\[assembly: AssemblyCompany\("".*""\)\]", replacement);

                        replacement = string.Format("[assembly: AssemblyCopyright(\"{0}\")]",
                                                    project.Properties["build.copyright"]);
                        contents = Regex.Replace(contents, @"\[assembly: AssemblyCopyright\("".*""\)\]", replacement);

                        // Update the description.
                        //replacement = string.Format("[assembly: AssemblyDescription(\"{0} {1}\")]",
                        //                            project.Properties["build.description"],
                        //                            project.Properties["build.svnversion"]);
                        replacement = string.Format("[assembly: AssemblyDescription(\"{0}\")]",
                                                    project.Properties["build.description"]);
                        contents = Regex.Replace(contents, @"\[assembly: AssemblyDescription\("".*""\)\]", replacement);

                        // Write out the file with the substituted version.
                        StreamWriter writer = new StreamWriter(project.Properties["filename"], false);
                        writer.Write(contents);
                        writer.Close();
                    }
                    ]]>
                    </code>
                </script>
            </do>
        </foreach>
    </target>

    <!-- Do the build. -->
    <target name="build" depends="init, setversion">
        <echo message="Building all modules including tests."/>

        <!-- Make sure output folder exists. -->
        <mkdir dir="${build.dir}" />

        <!-- copy reference assemblies over to the output dir -->
        <copy todir="${build.dir}" file="Qpid.Common/lib/seclib-1.0.0/Org.Mentalis.Security.dll"/>
        <copy todir="${build.dir}" file="Qpid.Common/lib/log4net/log4net.dll"/>
        <copy todir="${build.dir}" file="Qpid.Client.Tests/lib/nunit/nunit.framework.dll"/>

        <!-- Compile assemblies. -->
        <nant target="build">
            <buildfiles refid="src.builds" />
        </nant>

        <!-- Compile test assemblies. -->
        <nant target="build">
            <buildfiles refid="tests.pure.builds" />
        </nant>
        <nant target="build">
            <buildfiles refid="tests.integration.builds" />
        </nant>

        <!-- Compile test assemblies. -->
        <nant target="build">
            <buildfiles refid="other.builds" />
        </nant>
    </target>
    
    <!-- Runs all 'pure unit' tests. -->
    <target name="test" depends="build">
        <echo message="Running all pure unit tests."/>
        <nant target="test">
            <buildfiles refid="tests.pure.builds" />
        </nant>
    </target>

    <!-- Runs all 'integration' tests. -->
    <target name="integrationtest" depends="build">
        <echo message="Running all integration tests."/>
        <nant target="test">
            <buildfiles refid="tests.integration.builds" />
        </nant>
    </target>

    <!-- Creates a release package. -->
    <target name="release-pkg">
        <echo message="Building and packaging a release."/>

        <call target="clean"/>
        <call target="build"/>

        <property name="build.date" value="${datetime::now()}"/>

        <zip zipfile="${build.dir}/Qpid.NET-${framework::get-target-framework()}-${datetime::get-year(build.date)}${datetime::get-month(build.date)}${datetime::get-day(build.date)}.zip">
            <fileset basedir="${build.dir}">
                <include name="**/*.*"/>
                <exclude name="**/*.Tests.*"/>
                <exclude name="**/nunit.framework.dll"/>
                <exclude name="**/*.exe"/>            
            </fileset>
            
            <fileset basedir="${base.dir}">
                <include name="LICENSE.txt"/>
                <include name="NOTICE.txt"/>
                <include name="README.txt"/>
                <include name="RELEASE_NOTES.txt"/>
                <include name="DISCLAIMER"/>                    
            </fileset>
        </zip>
    </target>
    
</project>


