<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="http://nant.sf.net/schemas/nant.xsd" name="SubText">

	<property name="nant.settings.currentframework" value="net-2.0"/>
	<property name="nant.onsuccess" value="OnSuccess" />
	
	<target name="get-publishing-properties">
	
		<if test="${not property::exists('CCNetBuildDate')}"> 
			<fail message="CCNetBuildDate property not set, so can't create distribution files" />
		</if>	

		<if test="${not property::exists('CCNetBuildTime')}"> 
			<fail message="CCNetBuildTime property not set, so can't create distribution files" />
		</if>
		
		<if test="${not property::exists('CCNetArtifactDirectory')}"> 
			<fail message="CCNetArtifactDirectory property not set, so can't create distribution files" />
		</if>

		<if test="${not property::exists('CCNetWorkingDirectory')}"> 
			<fail message="CCNetWorkingDirectory property not set, so can't create distribution files" />
		</if>

		<if test="${not property::exists('CCNetLabel')}"> 
			<fail message="CCNetLabel property not set, so can't create distribution files" />
		</if>

		<if test="${not property::exists('fxcop.exe')}">
			<fail message="fxcop.exe property not set, so cannot test" />
		</if>

		<if test="${not property::exists('msbuild.exe')}">
			<fail message="msbuild.exe property not set, so cannot test" />
		</if>

		<property name="buildtools.dir" value="${CCNetWorkingDirectory}\BuildTools" />
		<property name="solution.dir" value="${CCNetWorkingDirectory}\SubtextSolution" />
		<property name="MbUnit.exe" value="${CCNetWorkingDirectory}\BuildTools\MbUnit\MbUnit.Cons.exe" />
		<property name="ncoverexplorer.exe" value="${buildtools.dir}\NCover\NCoverExplorer.Console.exe" />
		<property name="ncover.exe" value="${buildtools.dir}\NCover\NCover.Console.exe" />
		<property name="NDepend.exe" value="${CCNetWorkingDirectory}\BuildTools\NDepend\NDepend.Console.exe" />
		<property name="NDepend.config" value="${solution.dir}\NDependProject.xml" />
		<property name="subtext.web.bin" value="${solution.dir}\Subtext.Web\bin" />
		<property name="codemetrics.output.dir" value="${CCNetWorkingDirectory}\NDependTemp" />

		<!-- Directory for hosting images for specific builds -->
		<property name="artifacts.assets.dir" value="${CCNetArtifactDirectory}\buildlogs\${CCNetLabel}"/>
		
		<property name="log.dir" value="${CCNetWorkingDirectory}\logs" />

		<property name="publish.dir.latest" value="${CCNetArtifactDirectory}\builds\latest" />
		<property name="publish.dir.archive" value="${CCNetArtifactDirectory}\builds\archive" />
		<property name="publish.dir.latestsource" value="${CCNetArtifactDirectory}\builds\latestsource" />
		
		<property name="fileLabel" value="SubText-${CCNetLabel}" />
		
		<property name="docs.root.dir" value="${CCNetWorkingDirectory}\Docs" />
		<property name="ndoc.output.dir" value="${docs.root.dir}\NDoc" />
		
		<loadtasks path="${buildtools.dir}\NAnt\tasks" failonerror="false" />
	</target>

	
	<!-- //TODO: Consider checking in this file after update the version -->
	<target name="version" depends="get-publishing-properties">
	</target>

	
	<target name="clean" depends="get-publishing-properties">
		<delete dir="${log.dir}" failonerror="false" />
		<call target="clean-vs" />
	</target>

	
	<target name="clean-vs">
		<foreach item="Folder" property="foldername">
			<in>
				<items basedir=".">
					<include name="*\bin" />
					<include name="*\obj" />
				</items>
			</in>
			<do>
				<delete dir="${foldername}" />
			</do>
		</foreach>
		<mkdir dir="Subtext.Web\bin" />
	</target>

	<!--
		Compiles Subtext using the "documentation" configuration. 
		This configuration is the same as DEBUG but it outputs 
		documentation XML files.
	-->
	<target name="compile-debug" depends="clean, version">
		<property name="configuration" value="debug"/>
		<call target="compile" />
	</target>

	
	<target name="compile-release" depends="clean, version">
		<property name="configuration" value="Release"/>
		<call target="compile" />
	</target>

	
	<target name="compile">
		<exec program="${msbuild.exe}">
			<arg value="SubtextSolution.sln"/>
			<arg value="/nologo"/>
			<arg value="/verbosity:normal"/>
			<arg value="/t:Build"/>
			<arg value="/p:Configuration=${configuration}"/>
		</exec>
	</target>


	<!-- 
		Compiles Subtext and runs the MbUnit tests and NCover reports.
	-->
	<target name="testAndCover" depends="get-publishing-properties,compile-debug">
		<exec program="${ncover.exe}" 
			workingdir="UnitTests.Subtext\bin\${configuration}" 
			commandline="&quot;${MbUnit.exe}&quot; /rnf:unittest /rt:xml /rf:${log.dir} UnitTests.Subtext.dll //x ${log.dir}\ncover-temp.xml" 
			failonerror="true"/>

		<call target="util.ncoverexplorer.nant" />
		<!--
			Removing it since it will fail for an error
		<call target="code.metrics" />
		-->
	</target>

	<target name="code.metrics">
		<!-- Uses the pre-existing logs folder -->

		<!--	Put NDepend config xml into a working output directory, 
				since NDepend will output to the same directory as directory 
				used for input.									-->
		<copy todir="${log.dir}" failonerror="false">
			<fileset basedir="${solution.dir}">
				<include name="NDependProject.xml" />
			</fileset>
		</copy>

		<!-- Set the path to our temporary file -->
		<property name="ndepend.input" value="${log.dir}\NDependProject.xml" />

		<!-- Change the value of the target bin directory to point to the output of the build
         (copied binaries to output.dir in a previous task). -->
		<xmlpoke file="${ndepend.input}" xpath="/NDepend/Dirs/Dir" value="${subtext.web.bin}" />
		
		<!-- Change the value of the directory for the NDepend output. -->
		<xmlpoke file="${ndepend.input}" xpath="/NDepend/OutputDir" value="${log.dir}" />

		<!-- Run NDepend Executable -->
		<exec program="${NDepend.exe}" workingdir="${log.dir}" commandline='"${ndepend.input}"' failonerror="false" timeout="500000" />

		<!-- Copy images generated by NDepend to artifacts directory -->
		<mkdir dir="${artifacts.assets.dir}" />

		<copy todir="${artifacts.assets.dir}" failonerror="false">
			<fileset basedir="${log.dir}">
				<include name="*.png" />
				<include name="*.jpg" />
				<include name="*.png" />
			</fileset>
		</copy>
	</target>
	
	
	<target name="reporting" depends="compile-debug">
		<exec program="${fxcop.exe}" 
			commandline="/p:SubtextCodeAnalysis.FxCop /o:${log.dir}\ccnet-fxcop.xml" failonerror="false"/>
	</target>

	
	<target name="util.ncoverexplorer.nant">
		<echo message="Starting NCoverExplorer report generation..."/>

		<!-- Other attributes that can be used on ncoverexplorer element:               -->
		<!--   failMinimum="True" would fail the build if minimum coverage not met.     -->
		<!-- Remove the xmlReportName and/or htmlReportName attributes to not generate. -->
		<!-- Report types are 1=Module, 2=Namespace, 3=Namespace/Module.                -->
		<!-- Valid exclusion 'type' attributes are 'Assembly', 'Namespace' and 'Class'. -->
		<ncoverexplorer program="${ncoverexplorer.exe}"
						projectName="SubText"
						reportType="3"
						outputDir="${log.dir}" 
						xmlReportName="ncover.xml"
						showExcluded="True" >
			<fileset>
				<include name="${log.dir}\ncover-temp.xml"/>
			</fileset>
		</ncoverexplorer>
	</target>

	
	<target name="release" depends="dist.archive">
		<echo message="CI Run for build number ${CCNetLabel} successfully completed" />
	</target>

	
	<target name="dist.publish.latest" depends="compile-release">
		<delete dir="${publish.dir.latest}" failonerror="false" />
		<mkdir dir="${publish.dir.latest}" />
		
		<echo message="deploying binaries to latest" />
		
		<copy todir="${publish.dir.latest}" includeemptydirs="false">
			<fileset basedir="Subtext.Web">
				<include name="**/*" />
				<exclude name="**/*.bmp" />
				<exclude name="ExternalDependencies/*" />
				<exclude name="**/*.cs"/>
				<exclude name="**/*.txt"/>
				<exclude name="**/*.resx"/>
				<exclude name="bin/*.xml"/>
				<exclude name="**/*.csproj"/>
				<exclude name="**/*.csproj.user"/>
				<exclude name="**/.cvsignore"/>
				<exclude name="obj/**"/>
			</fileset>
		</copy>
		
		<copy todir="${publish.dir.latest}" includeemptydirs="false">
			<fileset basedir="Subtext.Web">
				<include name="emoticons.txt" />
			</fileset>
		</copy>
		
		<attrib readonly="false" >
			<fileset basedir="${publish.dir.latest}">
				<include name="**/*" />
			</fileset>
		</attrib>
	</target>

	<target name="dist.archive" depends="dist.publish.latest">
		<mkdir dir="${publish.dir.archive}" />
		<zip zipfile="${publish.dir.archive}\${fileLabel}.zip">
			<fileset basedir="${publish.dir.latest}">
				<include name="**/*" />
				<exclude name="**/*.pdb" />
			</fileset>
		</zip>
	</target>

	<target name="dist.source" depends="get-publishing-properties">
		<delete dir="${publish.dir.latestsource}" failonerror="false" />
		<mkdir dir="${publish.dir.latestsource}" />
		
		<zip zipfile="${publish.dir.latestsource}\${fileLabel}.src.zip">
			<fileset basedir="${solution.dir}">
				<include name="**/*" />
				<exclude name="**/bin/**/*" />
				<exclude name="**/obj/**/*" />
			</fileset>
		</zip>
		
	</target>

	<target name="OnSuccess">
		<echo file="${log.dir}\buildinfo.xml" append="false" failonerror="false">
			<![CDATA[<buildproject>${CCNetProject}</buildproject><buildlabel>${CCNetLabel}</buildlabel>]]>
		</echo>
	</target>
</project>