<?xml version="1.0" encoding="utf-8" ?>
<project name="AssemblyDropper" default="go">
  <!-- Project UppercuT - http://projectuppercut.org -->
  <!-- DO NOT EDIT THIS FILE - This moves output files from your project to a folder to be consumed by other applications through something like externals - 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="updateAssemblies" />
  <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}\..\${folder.build_scripts_custom}" />
  <property name="folder.code_drop" value="code_drop" overwrite="false" />
  <property name="dirs.drop" value="${dirs.current}\${path.to.toplevel}\${folder.code_drop}" overwrite="false" />
  <property name="folder.app.drop" value="${project.name}" overwrite="false" />
  <property name="project.name" value="__SOLUTION_NAME_WITHOUT_SLN_EXTENSION__" overwrite="false" />
  <property name="file.uppercut.assembly" value="uppercut.tasks.dll" />
  <property name="path.uppercut.assembly" value="${dirs.current}\${file.uppercut.assembly}" />
  <property name="dirs.assemblies" value="${dirs.current}\${path.to.toplevel}\assemblies" overwrite="false" />
  <property name="allow.powershell.unrestricted" value="false" overwrite="false" />
  <property name="file.custom.step.before" value="${dirs.build_scripts_custom}\${file.current.no_extension}.pre.build" />
  <property name="file.custom.step.after" value="${dirs.build_scripts_custom}\${file.current.no_extension}.post.build" />
  <property name="file.custom.step.replace" value="${dirs.build_scripts_custom}\${file.current.no_extension}.replace.build" />
  <property name="is.replaced" value="false" />
  <property name="fail.build.on.error" value="true" />

  <target name="go" depends="load_uppercut_assemblies, allow_powershell_unrestricted, run_tasks" />

  <target name="run_tasks">
    <echo message="Running ${project::get-name()} tasks." />
    <call target="prepare" if="${target::exists('prepare')}" />
    <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, update_assemblies"
          description="Moving assemblies to make available for Externals." />

  <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 provide arguments to the command line like this updateAssemblies.build -D:project.name='SOLUTION_NAME_WITHOUT_EXTENSION' or pass the build configuration settings file updateAssemblies.build -D:build.config.settings='Settings\Uppercut.config"
      if="${project.name=='__SOLUTION_NAME_WITHOUT_SLN_EXTENSION__'}" />
  </target>

  <target name="update_assemblies">
    <echo message="Moving files to ${dirs.assemblies}." />
    <copy todir="${dirs.assemblies}">
      <fileset basedir="${dirs.drop}\${folder.app.drop}">
        <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>

  <target name="load_uppercut_assemblies">
    <loadtasks assembly="${path.uppercut.assembly}" if="${file::exists(path.uppercut.assembly)}" />
  </target>
  
  <target name="allow_powershell_unrestricted">
    <!-- powershell 1.0 -->
    <exec program="powershell.exe" if="${allow.powershell.unrestricted}" failonerror="false">
      <arg value="set-executionpolicy unrestricted" />
    </exec>
    <!-- powershell 2.0 -->
    <exec program="powershell.exe" if="${allow.powershell.unrestricted}" failonerror="false">
      <arg value="set-executionpolicy unrestricted -force -scope CurrentUser" />
    </exec>
  </target>
    
</project>