<?xml version="1.0"?>
<project name="OpenCover" >
 
  <property name="configuration" value="debug" unless="${property::exists('configuration')}" />
  <property name="platform" value="x86" unless="${property::exists('platform')}" />
  <property name="nunit.path" value="${solution.folder}/packages/NUnit.Runners.2.6.4/tools"/>
  <property name="windows-path" value="${string::to-lower(environment::get-variable('PATH'))}"/>
  <property name="nunit-in-path" value="${string::contains(windows-path, 'nunit')}"/>
  <property name="open-cover-test-console" value="${solution.folder}/bin/${configuration}/OpenCover.Test.Profiler.exe"/>
  <property name="curl.exe" value="${solution.folder}/packages/curl.7.30.0.2/tools/native/v110/Win32/Release/static/curl.exe" />
  <property name="coveralls.exe" value="${solution.folder}/packages/coveralls.io.1.3.4/tools/coveralls.net.exe"/>
  <property name="appveyor" value="" />
  <property name="appveyor" value="${environment::get-variable('appveyor')}" if="${environment::variable-exists('appveyor')}"/>
  <property name="exclude-admin-only" value="/exclude:AdminOnly" />
  <property name="exclude-admin-only" value="" if="${appveyor=='True'}" />

	<property name="msbuild.exe" value="${program.files.x86}/MSBuild/12.0/Bin/MSBuild.exe" if="${directory::exists(program.files.x86 + '/MSBuild/12.0/Bin')}" />
	<property name="msbuild.exe" value="${netfx4.folder}/MSBuild.exe" unless="${property::exists('msbuild.exe')}"/>
  		
	<choose>
		<when test="${nunit-in-path}">
			<property name="nunit-console" value="nunit-console" if="${platform=='x64'}" />
			<property name="nunit-console" value="nunit-console-x86" if="${platform=='x86'}" />
		</when>
		<otherwise>
			<property name="nunit-console" value="${nunit.path}/nunit-console.exe" if="${platform=='x64'}" />
			<property name="nunit-console" value="${nunit.path}/nunit-console-x86.exe" if="${platform=='x86'}" />
		</otherwise>
	</choose>
	
  <target name="clean" description="Delete all previously compiled binaries.">
    <delete>
      <fileset>
        <include name="${solution.folder}/**/bin/**" />
        <include name="${solution.folder}/**/obj/**" />
        <include name="${solution.folder}/**/*.suo" />
        <include name="${solution.folder}/**/*.user" />
        <include name="${solution.folder}/**/x86/**" />
        <include name="${solution.folder}/**/x64/**" />
        <include name="${solution.folder}/**/Win32/**" />
        <include name="${solution.folder}/Docs/**" />
        <include name="${solution.folder}/**/Debug/**" />
        <include name="${solution.folder}/**/Release/**" />
      </fileset>
    </delete>
  </target>
    
  <target name="clean-all" description="Delete all other files" depends="clean">
    <delete>
      <fileset>
        <include name="${solution.folder}/ipch/**" />
        <include name="${solution.folder}/opencover.sdf" />
      </fileset>
    </delete>
  </target>
    
  <target name="build" description="Build all targets.">
    <property name="configuration" value="debug" />
    <property name="platform" value="x64" />
    <call target="build.opencover" />
    <property name="configuration" value="debug" />
    <property name="platform" value="x86" />
    <call target="build.opencover" />
  </target>

  <target name="build-all-platforms"
          depends="build-debug-platforms, build-release-platforms"
          description="Build all flavours i.e. all configurations and all platforms" />

  <target name="build-release-platforms" description="Build all release platform flavours">
    <property name="configuration" value="release" />
    <property name="platform" value="x64" />
    <call target="build.opencover" />
    <property name="configuration" value="release" />
    <property name="platform" value="x86" />
    <call target="build.opencover" />
  </target>

  <target name="build-release-platforms-x64" description="Build x64 release platform">
    <property name="configuration" value="release" />
    <property name="platform" value="x64" />
    <call target="build.opencover" />
  </target>
  
  <target name="build-debug-platforms" description="Build all debug platform flavours">
    <property name="configuration" value="debug" />
    <property name="platform" value="x64" />
    <call target="build.opencover" />
    <property name="configuration" value="debug" />
    <property name="platform" value="x86" />
    <call target="build.opencover" />
  </target>

  <target name="fetch.solution.packages" description="pull solution nuget packages">
    <exec program="${nuget.exe}">
      <arg value='restore "${solution.folder}/.nuget/packages.config" -packagesdirectory "${solution.folder}/packages"' />
    </exec>
  </target>
  
  <target name="build.opencover" depends="fetch.solution.packages">
		
		<exec program="${msbuild.exe}">
		  <arg value="${solution.folder}/opencover.sln" />
		  <arg value="/t:Clean,Rebuild" />
		  <arg value="/p:Configuration=${configuration},Platform=${platform}" />
		</exec>
      
    <property name="expected.output" value="${solution.folder}/bin/${configuration}/OpenCover.Console.exe"/>
    <fail unless="${file::exists(expected.output)}">Output file doesn't exist in ${expected.output}</fail>
    
  </target>
    	
  <target name="test" description="Run unit tests" >
                
		<echo message="test: ${solution.folder}/bin/${configuration}/OpenCover.Test.dll" />
		
    <exec program="${nunit-console}">
      <arg value="${solution.folder}/bin/${configuration}/OpenCover.Test.dll" />
      <arg value="/framework=4.5.2" />
      <arg value="${exclude-admin-only}" />
      <arg value="/xml=${solution.folder}/bin/${configuration}/TestResult.xml" />
    </exec>
        
  </target>

  <target name="test-bdd" description="Run BDD tests" >
        
		<echo message="test-bdd: ${solution.folder}/OpenCover.Specs/bin/${configuration}/OpenCover.Specs.dll" />
        
    <exec program="${nunit-console}">
      <arg value="${solution.folder}/OpenCover.Specs/bin/${configuration}/OpenCover.Specs.dll" />
      <arg value="/framework=4.5.2" />
      <arg value="${exclude-admin-only}" />
      <arg value="/xml=${solution.folder}/bin/${configuration}/TestResult.xml" />
    </exec>
        
  </target>
    
  <target name="test-cpp" depends="register, test-cpp-internal, unregister" />
    
  <target name="test-cpp-internal" description="Run C++ tests" >
    <exec program="${open-cover-test-console}" if="${file::exists(open-cover-test-console)}" />
  </target>
    
  <target name="unregister" description="Unregister the profiler assembly (per-user)" >
    <echo message="unregister: ${solution.folder}/bin/${configuration}/${platform}/OpenCover.Profiler.dll" />
    <exec program="regsvr32" >	
      <arg value="/u" />
      <arg value="/n" />
      <arg value="/i:user" />
      <arg value="/s" />
      <arg value="${solution.folder}/bin/${configuration}/${platform}/OpenCover.Profiler.dll" />	
    </exec>
  </target>

  <target name="register" description="Register the profiler assembly (per-user)" >
	  <echo message="register: ${solution.folder}/bin/${configuration}/${platform}/OpenCover.Profiler.dll" />
    <exec program="regsvr32" >	
      <arg value="/n" />
      <arg value="/i:user" />
      <arg value="/s" />
      <arg value="${solution.folder}/bin/${configuration}/${platform}/OpenCover.Profiler.dll" />	
    </exec>
  </target>
    
  <target name="dogfood">

    <exec program="${solution.folder}/bin/${configuration}/dogfood.cmd" 
      workingdir="${solution.folder}/bin/${configuration}"	/>

    <exec program="${solution.folder}/bin/${configuration}/uitest.opencover.cmd"
      workingdir="${solution.folder}/bin/${configuration}"	/>

    <exec program="${coveralls.exe}" workingdir="${solution.folder}/bin/${configuration}">      
      <arg value="--opencover" />
      <arg value="opencovertests.xml" />
    </exec>
        
  </target>

  <target name="dogfood-release">

    <property name="configuration" value="release" />
    
    <exec program="${solution.folder}/bin/${configuration}/dogfood_exattr.cmd"
      workingdir="${solution.folder}/bin/${configuration}"	/>

    <exec program="${solution.folder}/bin/${configuration}/uitest.opencover.cmd"
      workingdir="${solution.folder}/bin/${configuration}"	/>

    <exec program="${coveralls.exe}" workingdir="${solution.folder}/bin/${configuration}">      
      <arg value="--opencover" />
      <arg value="opencovertests.xml" />
    </exec>
        
  </target>
    
</project>