<?xml version="1.0" encoding="utf-8" ?>
<project name="migrator" default="build" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
    <property name="dotnet" value="true" if="${framework::get-target-framework() == 'net-2.0' or framework::get-target-framework() == 'net-3.5'}"/>
    <property name="mono" value="true" if="${framework::get-target-framework() == 'mono-2.0'}"/>
    
    <property name="defines" value="DOTNET2" if="${property::exists('dotnet')}" />
    <property name="defines" value="MONO2" if="${property::exists('mono')}" />

    <property name="version" value="0.8"/>

    <property name="debug" value="true" />

    <!-- Paths to various parts of the build -->
    <property name="dir.bin" value="build" />
    <property name="dir.lib" value="lib" />
    <property name="dir.results" value="results"/>
    <property name="dir.zip" value="${dir.bin}/zip" />
    <property name="dir.src" value="src"/>
    <property name="dir.config" value="${dir.src}/config" />
    <property name="dir.tests" value="${dir.src}/Migrator.Tests" />
    <property name="dir.msbuild" value="${dir.src}/Migrator.MSBuild" />
    <property name="dir.nant" value="${dir.src}/Migrator.NAnt" />
    <property name="dir.framework" value="${dir.src}/Migrator.Framework" />
    <property name="dir.providers" value="${dir.src}/Migrator.Providers" />
    <property name="dir.core" value="${dir.src}/Migrator" />
    <property name="dir.console" value="${dir.src}/Migrator.Console" />
    <property name="dir.tools.ncover" value="${dir.lib}/NCover" />
    <property name="dir.tools.nunit" value="${dir.lib}/NUnit" />
	<property name="dir.lib.postgre" value="${dir.lib}/Npgsql/${framework::get-target-framework()}" />
	<property name="dir.keyfile" value="${dir.src}/MigratorDotNet.snk" />
	
    <property name="test.project.name" value="Migrator.Tests"/>

    <!-- Test related properties -->
    <!-- To exclude tests, provide a comma separated set of exclude categories in your local.properties e.g. Oracle,SqlServer,Postgre -->
    <property name="tests.exclude" value=""/>
    <property name="tests.app.config" value="${dir.config}/app.config"/>

    <!-- Overrideable. Add your own values to a file called local.properties to have them override any above properties -->
    <include buildfile="local.properties" failonerror="false"/>

    <!-- load the NCover NAnt tasks -->
    <loadtasks assembly="${dir.tools.ncover}/Explorer/NCoverExplorer.NAntTasks.dll" />

    <!--
        Once we setup to build from sln, we can specify build config to target
        which is ideal to output release assemblies over debug. the debug, release
        targets are not used at the moment

        <target name="debug">
            <property name="build.config" value="Debug" />
            <call target="build" />
        </target>
        <target name="release">
            <property name="build.config" value="Release" />
            <call target="build.common" />
        </target>
    -->

    <target name="build" description="Builds the app" depends="prepare">	    
        <csc target="library" output="${dir.bin}/Migrator.Framework.dll" define="${defines}" debug="${debug}" keyfile="${dir.keyfile}">
            <sources>
                <include name="${dir.config}/AssemblyInfo.cs" />
                <include name="${dir.framework}/**/*.cs" />
            </sources>
            <references>
                <include name="System.dll" />
                <include name="System.Data.dll" />
            </references>
        </csc>
        <csc target="library" output="${dir.bin}/Migrator.Providers.dll" define="${defines}" debug="${debug}" keyfile="${dir.keyfile}">
            <sources>
                <include name="${dir.config}/AssemblyInfo.cs" />
                <include name="${dir.providers}/**/*.cs" />
            </sources>
            <references>
                <include name="System.dll" />
                <include name="System.Data.dll" />
                <include name="Mono.Data.Sqlite.dll" if="${property::exists('mono')}"/>
                <include name="${dir.bin}/System.Data.SQLite.DLL" if="${property::exists('dotnet')}"/>
                <include name="${dir.bin}/Migrator.Framework.dll" />
                <include name="${dir.bin}/MySql.Data.dll" />
                <include name="${dir.bin}/Npgsql.dll" />
                <include name="${dir.lib}/Oracle.DataAccess.dll" />
				<include name="${dir.lib}/System.Data.SqlServerCe.dll" />
			</references>
		</csc>
		<csc target="library" output="${dir.bin}/Migrator.dll" define="${defines}" debug="${debug}" keyfile="${dir.keyfile}">
			<sources>
				<include name="${dir.config}/AssemblyInfo.cs" />
				<include name="${dir.core}/**/*.cs" />
			</sources>
			<references>
				<include name="System.dll" />
				<include name="System.Data.dll" />
				<include name="System.Configuration.dll" />
				<include name="${dir.bin}/Migrator.Framework.dll" />
                <include name="${dir.bin}/Migrator.Providers.dll" />
			</references>
		</csc>
		<csc target="library" output="${dir.bin}/Migrator.NAnt.dll" define="${defines}" debug="${debug}">
			<sources>
				<include name="${dir.config}/AssemblyInfo.cs" />
				<include name="${dir.nant}/**/*.cs" />
			</sources>
			<references>
				<include name="System.dll" />
				<include name="${dir.bin}/NAnt.Core.dll" />
				<include name="${dir.bin}/Migrator.Framework.dll" />
				<include name="${dir.bin}/Migrator.dll" />
			</references>
		</csc>
		<csc target="library" output="${dir.bin}/Migrator.MSBuild.dll" define="${defines}" debug="${debug}" if="${framework::get-target-framework() == 'net-2.0'}" keyfile="${dir.keyfile}">
			<sources>
				<include name="${dir.config}/AssemblyInfo.cs" />
				<include name="${dir.msbuild}/**/*.cs" />
			</sources>
			<references>
				<include name="System.dll" />
				<include name="Microsoft.Build.Framework.dll" />
				<include name="Microsoft.Build.Utilities.dll" />
				<include name="${dir.bin}/Migrator.Framework.dll" />
				<include name="${dir.bin}/Migrator.dll" />
			</references>
		</csc>
		<csc target="exe" output="${dir.bin}/Migrator.Console.exe" define="${defines}" debug="${debug}" keyfile="${dir.keyfile}">
			<sources>
				<include name="${dir.config}/AssemblyInfo.cs" />
				<include name="${dir.console}/**/*.cs" />
			</sources>
			<references>
				<include name="System.dll" />
				<include name="${dir.bin}/Migrator.Framework.dll" />
				<include name="${dir.bin}/Migrator.dll" />
			</references>
		</csc>
	</target>

    <target name="rebuild" description="Rebuilds the app from scratch" depends="clean, build" />

    <target name="build.tests" description="Builds the tests" depends="build, prepare.tests">
        <copy file="${tests.app.config}" tofile="${dir.bin}/${test.project.name}.dll.config" />
        <csc target="library" output="${dir.bin}/${test.project.name}.dll" define="${defines}" debug="${debug}">
            <sources>
                <include name="${dir.tests}/**/*.cs" />
            </sources>
            <references>
                <include name="System.dll" />
                <include name="System.Data.dll" />
                <include name="System.Configuration.dll" />
                <include name="${dir.lib}/System.Data.SqlServerCe.dll" />
                <include name="${dir.bin}/Migrator.Framework.dll" />
                <include name="${dir.bin}/Migrator.dll" />
                <include name="${dir.bin}/Migrator.Providers.dll" />
                <include name="${dir.tools.nunit}/nunit.mocks.dll" />
                <include name="${dir.tools.nunit}/nunit.framework.dll" />
            </references>
        </csc>
    </target>

    <target name="prepare">
        <mkdir dir="${dir.bin}" />
        <copy file="${dir.msbuild}/Migrator.Targets" todir="${dir.bin}" />
        <copy todir="${dir.bin}" flatten="true">
            <fileset>
                <include name="${dir.lib}/NAnt.Core.dll" />
                <include name="${dir.lib}/log4net.dll" />
                <include name="${dir.lib}/MySql.Data.dll" />
                <include name="${dir.lib}/System.Data.SQLite.DLL" />
                <include name="${dir.lib.postgre}/Npgsql.dll" />
                <include name="${dir.lib.postgre}/Mono.Security.dll" />
                <include name="${dir.lib}/Oracle.DataAccess.dll" />
                <include name="${dir.lib}/System.Data.SqlServerCe.dll" />
            </fileset>
        </copy>
    </target>

    <target name="prepare.tests">
        <mkdir dir="${dir.bin}" />
        <mkdir dir="${dir.results}"/>
        <copy todir="${dir.bin}" flatten="true">
            <fileset>
                <include name="${dir.tools.nunit}/nunit.mocks.dll" />
                <include name="${dir.tools.nunit}/nunit.framework.dll" />
            </fileset>
        </copy>
    </target>

    <target name="clean" description="Sweep it yeah!">
        <delete dir="${dir.bin}"/>
        <delete dir="${dir.results}"/>
    </target>

    <target name="test" description="Run tests" depends="build.tests">
        <nunit2> 
            <formatter type="Plain" />
            <test
                assemblyname="${dir.bin}/Migrator.Tests.dll"
                appconfig="${tests.app.config}">
                <categories>
                    <exclude name="${tests.exclude}"/>
                </categories>
            </test>
        </nunit2>	
    </target>

    <target name="test.coverage" description="Run the tests with code coverage. Only works on Windows/.NET" depends="build.tests">

        <ncover program="${dir.tools.ncover}/NCover.Console.exe"
            commandLineExe="${dir.tools.nunit}/nunit-console.exe"
            commandLineArgs="${dir.bin}/${test.project.name}.dll /exclude=${tests.exclude} /labels /nologo /noshadow"
            coverageFile="${dir.results}/coverage.xml"
            logFile="${dir.results}/coverage.log">
            <assemblies basedir="${dir.bin}">
                <include name="Migrator.*.dll" />
                <include name="Migrator.dll" />
                <exclude name="*.Tests.dll" />
            </assemblies>
        </ncover>

        <ncoverexplorer program="${dir.tools.ncover}/Explorer/NCoverExplorer.Console.exe"
            projectName="Migrator"
            outputDir="${dir.results}"
            satisfactoryCoverage="80"
            reportType="ModuleClassFunctionSummary"
            htmlReportName="CoverageSummary.html"
            showExcluded="True">
            <fileset>
                <include name="${dir.results}/coverage.xml" />
            </fileset>
        </ncoverexplorer>
    </target>


    <target name="zip" description="Creates a zip file of the entire project including examples, source and docs" depends="build">
        <mkdir dir="${dir.zip}" />

        <property name="zipcontainer" value="${dir.zip}/migratordotnet-${version}"/>

        <mkdir dir="${zipcontainer}" />
        <mkdir dir="${zipcontainer}/lib" />

        <copy todir="${zipcontainer}/lib">
            <fileset basedir="${dir.bin}">
                <include name="**/*" />
            </fileset>
        </copy>

        <copy todir="${zipcontainer}">
            <fileset>
                <include name="doc/**/*" />
            </fileset>
        </copy>

        <zip zipfile="migratordotnet-${version}.zip">
            <fileset basedir="${dir.zip}">
                <include name="**/*" />
            </fileset>
        </zip>
        <delete dir="${dir.zip}"/>
    </target>

</project>
