<?xml version="1.0" encoding="utf-8" ?>
<project name="Compiler" default="go">
  <!-- Project UppercuT - http://projectuppercut.org -->
  <!-- DO NOT EDIT THIS FILE - Add custom tasks in BuildTasks.Custom folder with file by the same name - find out more at http://uppercut.pbwiki.com -->
  <property name="build.config.settings" value="__NONE__" overwrite="false" />
  <include buildfile="${build.config.settings}" if="${file::exists(build.config.settings)}" />
  <property name="file.current.no_extension" value="compile" />
  <property name="dirs.current" value="${directory::get-parent-directory(project::get-buildfile-path())}" />
  <property name="path.to.toplevel" value=".." />
  <property name="folder.build_scripts" value="build" overwrite="false" />
  <property name="folder.build_scripts_custom" value="build.custom" overwrite="false" />
  <property name="dirs.build_scripts_custom" value="${dirs.current}\${path.to.toplevel}\${folder.build_scripts_custom}" />
  <property name="folder.code_build" value="build_output" overwrite="false" />
  <property name="dirs.build" value="${dirs.current}\${path.to.toplevel}\${folder.code_build}" />
  <property name="folder.documentation" value="docs" overwrite="false" />
  <property name="dirs.docs" value="${dirs.current}\${path.to.toplevel}\${folder.documentation}" />
  <property name="folder.database" value="__DATABASE_FOLDER_NAME__" overwrite="false" />
  <property name="dirs.db" value="${dirs.current}\${path.to.toplevel}\${folder.database}" />
  <property name="project.name" value="__SOLUTION_NAME_WITHOUT_SLN_EXTENSION__" overwrite="false" />
  <property name="folder.app.drop" value="${project.name}" overwrite="false" />
  <property name="path_to_solution" value="." overwrite="false" />
  <property name="microsoft.framework" value="net-3.5" overwrite="false" />
  <property name="msbuild.configuration" value="Release" overwrite="false" />
  <property name="msbuild.platform" value="Any CPU" overwrite="false" />
  <property name="msbuild.override_output_path" value="true" overwrite="false" />
  <property name="msbuild.outputpath" value="${dirs.build}\${folder.app.drop}" overwrite="false" />
  <property name="solution.path" value="${dirs.current}\${path.to.toplevel}\${path_to_solution}\${project.name}.sln" />
  <property name="solution.path" value="${dirs.current}\${path.to.toplevel}\${path_to_solution}\${project.name}.vbp" if="${microsoft.framework =='vb6'}" />
  <property name="nant.settings.currentframework" value="net-3.5" if="${microsoft.framework !='vb6'}" />
  <property name="framework.multitargeting" value="false" />
  <property name="framework.multitargeting.delimiter" value="," />
  <property name="file.custom.step.before" value="${dirs.build_scripts_custom}\${file.current.no_extension}.pre.step" />
  <property name="file.custom.step.after" value="${dirs.build_scripts_custom}\${file.current.no_extension}.post.step" />
  <property name="file.custom.step.replace" value="${dirs.build_scripts_custom}\${file.current.no_extension}.replace.step" />
  <property name="is.replaced" value="false" />
  <property name="fail.build.on.error" value="true" />

  <target name="go" depends="run_tasks" />

  <target name="run_tasks">
    <echo message="Running ${project::get-name()} tasks." />
    <call target="prepare" if="${target::exists('prepare')}" />
    <call target="check_for_multitargeting" if="${target::exists('check_for_multitargeting')}" />
    <call target="custom_tasks_before" if="${target::exists('custom_tasks_before')}"  />
    <call target="custom_tasks_replace" if="${target::exists('custom_tasks_replace')}"  />
    <call target="run_normal_tasks" if="${not is.replaced}" />
    <call target="custom_tasks_after" if="${target::exists('custom_tasks_after')}" />
  </target>

  <target name="run_normal_tasks"
          depends="error_check, build_code, copy_documentation, copy_db"
          description="Compiling project." />

  <target name="custom_tasks_before">
    <echo message="Running custom tasks if ${file.custom.step.before} exists." />
    <nant buildfile="${file.custom.step.before}" inheritall="true" if="${file::exists(file.custom.step.before)}" failonerror="${fail.build.on.error}" />
    <exec program="powershell.exe" if="${file::exists(file.custom.step.before + '.ps1')}" failonerror="${fail.build.on.error}">
      <arg value="${path::get-full-path(file.custom.step.before + '.ps1')}" />
    </exec>
    <exec program="ruby.exe" if="${file::exists(file.custom.step.before + '.rb')}" failonerror="${fail.build.on.error}">
      <arg value="${path::get-full-path(file.custom.step.before + '.rb')}" />
    </exec>
  </target>

  <target name="custom_tasks_replace">
    <echo message="Running custom tasks instead of normal tasks if ${file.custom.step.replace} exists." />
    <property name="is.replaced" value="true" if="${file::exists(file.custom.step.replace)}" />
    <nant buildfile="${file.custom.step.replace}" inheritall="true" if="${file::exists(file.custom.step.replace)}" failonerror="${fail.build.on.error}" />
    <property name="is.replaced" value="true" if="${file::exists(file.custom.step.replace + '.ps1')}" />
    <exec program="powershell.exe" if="${file::exists(file.custom.step.replace + '.ps1')}" failonerror="${fail.build.on.error}" >
      <arg value="${path::get-full-path(file.custom.step.replace + '.ps1')}" />
    </exec>
    <property name="is.replaced" value="true" if="${file::exists(file.custom.step.replace + '.rb')}" />
    <exec program="ruby.exe" if="${file::exists(file.custom.step.replace + '.rb')}" failonerror="${fail.build.on.error}" >
      <arg value="${path::get-full-path(file.custom.step.replace + '.rb')}" />
    </exec>
  </target>

  <target name="error_check">
    <fail message="You must fill out the project.name, repository.path, and company.name settings in the settings\UppercuT.config file. Please do that and retry the build."
      if="${project.name=='__SOLUTION_NAME_WITHOUT_SLN_EXTENSION__'}" />
  </target>

  <target name="check_for_multitargeting">
    <if test="${string::contains(microsoft.framework,',')}">
      <echo message="You want to multi target, eh? You passed a comma into the framework: ${microsoft.framework}" />
      <property name="framework.multitargeting" value="true" />
      <property name="framework.multitargeting.delimiter" value="," />
    </if>
    <if test="${string::contains(microsoft.framework,';')}">
      <echo message="You want to multi target, eh? You passed a semicolon into the framework: ${microsoft.framework}" />
      <property name="framework.multitargeting" value="true" />
      <property name="framework.multitargeting.delimiter" value=";" />
    </if>
  </target>
  
  <target name="build_code" depends="" description="Building Library">
    <echo message="Compiling ${solution.path}."/>
    <call target="build_dotNET_code" if="${microsoft.framework !='vb6'}" />
    <call target="build_vb6_code" if="${microsoft.framework =='vb6'}" />
  </target>

  <target name="build_dotNET_code" >
    <if test="${framework.multitargeting}">
      <foreach item="String" in="${microsoft.framework}" delim="${framework.multitargeting.delimiter}" property="framework.specific">
        <property name="msbuild.outputpath" value="${dirs.build}\${folder.app.drop}\${framework.specific}" />
        <property name="microsoft.framework.specific" value="${string::trim(framework.specific)}" />
        <call target="build_dotNet_code_framework" />
      </foreach>
    </if>
    <if test="${not framework.multitargeting}">
      <property name="microsoft.framework.specific" value="${string::trim(microsoft.framework)}" />
      <call target="build_dotNet_code_framework" />
    </if>
  </target>
  
  <target name="build_dotNet_code_framework">
    <echo message="Building on ${framework::get-version(microsoft.framework.specific)}" />
    <property name="nant.settings.currentframework" value="${string::trim(microsoft.framework.specific)}" />
    <!-- nant is retarded - I had to have two of these instead of throwing an if on the outputpath -->
    <if test="${msbuild.override_output_path}">
      <if test="${framework.multitargeting}">
        <msbuild project="${solution.path}" verbosity="minimal">
          <property name="Configuration" value="${msbuild.configuration}" />
          <property name="OutputPath" value="${msbuild.outputpath}" />
          <property name="Platform" value="${msbuild.platform}" />
          <property name="TargetFrameworkVersion" value="v${framework::get-version(microsoft.framework.specific)}" />
          <property name="ToolsVersion" value="${framework::get-version(microsoft.framework.specific)}" />
        </msbuild>
      </if>
      <if test="${not framework.multitargeting}">
        <msbuild project="${solution.path}" verbosity="minimal">
          <property name="Configuration" value="${msbuild.configuration}" />
          <property name="OutputPath" value="${msbuild.outputpath}" />
          <property name="Platform" value="${msbuild.platform}" />
          <property name="TargetFrameworkVersion" value="v${framework::get-version(microsoft.framework.specific)}" />
        </msbuild>
      </if>
    </if>
    <if test="${not msbuild.override_output_path}">
      <if test="${framework.multitargeting}">
        <msbuild project="${solution.path}" verbosity="minimal">
          <property name="Configuration" value="${msbuild.configuration}" />
          <property name="Platform" value="${msbuild.platform}" />
          <property name="TargetFrameworkVersion" value="v${framework::get-version(microsoft.framework.specific)}" />
          <property name="ToolsVersion" value="${framework::get-version(microsoft.framework.specific)}" />
        </msbuild>
      </if>
      <if test="${not framework.multitargeting}">
        <msbuild project="${solution.path}" verbosity="minimal">
          <property name="Configuration" value="${msbuild.configuration}" />
          <property name="Platform" value="${msbuild.platform}" />
          <property name="TargetFrameworkVersion" value="v${framework::get-version(microsoft.framework.specific)}" />
        </msbuild>
      </if>
    </if>
  </target>

  <target name="build_vb6_code">
    <vb6 project="${solution.path}" outdir="${dirs.build}\${folder.app.drop}" />
  </target>

  <target name="copy_documentation">
    <echo message="Building documentation files to ${dirs.build}\${folder.documentation}."/>
    <copy todir="${dirs.build}\${folder.documentation}">
      <fileset basedir="${dirs.docs}">
        <include name="**/*.*" />
      </fileset>
    </copy>
  </target>

  <target name="copy_db">
    <echo message="Building database files to ${dirs.build}\${folder.database}."/>
    <copy todir="${dirs.build}\${folder.database}">
      <fileset basedir="${dirs.db}">
        <include name="**/*.*" />
      </fileset>
    </copy>
  </target>

  <target name="custom_tasks_after">
    <echo message="Running custom tasks if ${file.custom.step.after} exists." />
    <nant buildfile="${file.custom.step.after}" inheritall="true" if="${file::exists(file.custom.step.after)}" failonerror="${fail.build.on.error}" />
    <exec program="powershell.exe" if="${file::exists(file.custom.step.after + '.ps1')}" failonerror="${fail.build.on.error}" >
      <arg value="${path::get-full-path(file.custom.step.after + '.ps1')}" />
    </exec>
    <exec program="ruby.exe" if="${file::exists(file.custom.step.after + '.rb')}" failonerror="${fail.build.on.error}" >
      <arg value="${path::get-full-path(file.custom.step.after + '.rb')}" />
    </exec>
  </target>

</project>