<?xml version="1.0"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<project name="dotnet" basedir="." default="init"
  xmlns:dn="antlib:org.apache.ant.dotnet"
  xmlns:au="antlib:org.apache.ant.antunit">

  <property environment="env"/>
  <property name="build.dir" location="build"/>
  <property name="src.dir" location="src"/>

  <property name="out.csc" location="${src.dir}/out.cs"/>
  <property name="out.app" location="${build.dir}/out.exe"/>
  <property name="out.type" value="exe"/>

  <target name="probe_for_apps" >
   <condition property="ilasm.found">
      <or>
        <available file="ilasm"     filepath="${env.PATH}" />
        <available file="ilasm.exe" filepath="${env.PATH}" />
        <available file="ilasm.exe" filepath="${env.Path}" />
      </or>
    </condition>
   <echo> ilasm.found=${ilasm.found}</echo>
   <condition property="csc.found">
      <or>
        <available file="csc"     filepath="${env.PATH}" />
        <available file="csc.exe" filepath="${env.PATH}" />
        <available file="csc.exe" filepath="${env.Path}" />
      </or>
    </condition>
   <echo> csc.found=${csc.found}</echo>
   <!-- visual basic compiler -->
   <condition property="vbc.found">
      <or>
        <available file="vbc"     filepath="${env.PATH}" />
        <available file="vbc.exe" filepath="${env.PATH}" />
        <available file="vbc.exe" filepath="${env.Path}" />
      </or>
    </condition>
   <echo> vbc.found=${vbc.found}</echo>
   <!-- visual J# compiler -->
   <condition property="jsharp.found">
      <or>
        <available file="vjc"     filepath="${env.PATH}" />
        <available file="vjc.exe" filepath="${env.PATH}" />
        <available file="vjc.exe" filepath="${env.Path}" />
      </or>
    </condition>
   <echo> jsharp.found=${jsharp.found}</echo>

   <!-- Mono C# compiler -->
   <condition property="gmcs.found">
      <available file="gmcs"     filepath="${env.PATH}" />
    </condition>
   <echo> gmcs.found=${gmcs.found}</echo>

   <!-- any C# compiler -->
   <condition property="c#.found">
      <or>
        <isset property="csc.found"/>
        <isset property="gmcs.found"/>
      </or>
   </condition>

   <!-- Mono's ilasm -->
   <condition property="mono.ilasm.found">
      <available file="ilasm"     filepath="${env.PATH}" />
    </condition>
   <echo> mono.ilasm.found=${mono.ilasm.found}</echo>

   <condition property="ildasm.found">
      <or>
        <available file="ildasm"     filepath="${env.PATH}" />
        <available file="ildasm.exe" filepath="${env.PATH}" />
        <available file="ildasm.exe" filepath="${env.Path}" />
      </or>
    </condition>
   <echo> ildasm.found=${ildasm.found}</echo>

   <condition property="dotnetapps.found">
      <or>
        <and>
          <isset property="gmcs.found"/>
          <isset property="mono.ilasm.found"/>
        </and>
        <and>
          <isset property="csc.found"/>
<!--         <isset property="vbc.found"/> -->
          <isset property="ilasm.found"/>
        </and>
      </or>
    </condition>
   <echo> dotnetapps.found=${dotnetapps.found}</echo>

   <condition property="mono.executable" value="mono">
        <or>
          <available file="mono"     filepath="${env.PATH}" />
          <available file="mono.exe" filepath="${env.PATH}" />
        </or>
   </condition>
   <property name="mono.executable" value="mint"/>

   <!-- now set a prop of the compiler name to whatever we found -->
   <condition property="cs.compiler" value="csc">
      <isset property="csc.found"/>
   </condition>

   <condition property="cs.compiler" value="gmcs">
      <isset property="gmcs.found"/>
   </condition>

    <condition property="ms.net20+">
      <or>
        <available file="aspnet_compiler.exe" filepath="${env.PATH}"/>
        <available file="aspnet_compiler.exe" filepath="${env.Path}"/>
      </or>
    </condition>
  </target>

  <target name="init" depends="probe_for_apps">
    <mkdir dir="${build.dir}"/>
  </target>

  <target name="tearDown">
    <delete dir="${build.dir}"/>
  </target>

  <target name="validate_csc" depends="init">
    <fail unless="c#.found">Needed C# compiler is missing</fail>
  </target>

  <target name="validate_ilasm" depends="init">
    <fail unless="ilasm.found">Needed ilasm is missing</fail>
  </target>

  <target name="validate_jsharp" depends="init">
    <fail unless="jsharp.found">No vjc on the path</fail>
  </target>


  <target name="testCSC" depends="CSC-Mono,CSC-MS"/>

  <target name="CSC-MS" depends="validate_csc" if="csc.found">
    <property name="testCSC.exe"
      location="${build.dir}/ExampleCsc.exe" />
    <dn:csc
      destFile="${testCSC.exe}"
      targetType="exe"
      srcDir="${src.dir}"
      >
    </dn:csc>
    <au:assertFileExists file="${testCSC.exe}"/>
    <exec executable="${testCSC.exe}" failonerror="true" />
    <delete file="${testCSC.exe}"/>
  </target>

  <target name="CSC-Mono" depends="validate_csc" if="gmcs.found">
    <property name="testCSC.exe"
      location="${build.dir}/ExampleCsc.exe" />
    <dn:csc
      destFile="${testCSC.exe}"
      targetType="exe"
      includedefaultreferences="true"
      srcDir="${src.dir}"
      >
    </dn:csc>
    <au:assertFileExists file="${testCSC.exe}"/>
    <exec executable="${mono.executable}" failonerror="true">
      <arg value="${testCSC.exe}"/>
    </exec>
    <delete file="${testCSC.exe}"/>
  </target>

  <target name="testCSCResources" depends="CSCResources-Mono,CSCResources-MS"/>

  <target name="CSCResources-MS" depends="validate_csc" if="csc.found">
    <property name="testCSCRes.exe"
      location="${build.dir}/ExampleCscRes.exe" />
    <dn:csc
      destFile="${testCSCRes.exe}"
      targetType="exe"
      srcDir="${src.dir}"
      >
      <resource file="${src.dir}/res.resources"/>
    </dn:csc>
    <au:assertFileExists file="${testCSCRes.exe}"/>
    <exec executable="${testCSCRes.exe}" failonerror="true" />
    <delete file="${testCSCRes.exe}"/>
    <dn:csc
      destFile="${testCSCRes.exe}"
      targetType="exe"
      srcDir="${src.dir}"
      >
      <resource namespace="some.namespace" embed="true">
        <fileset file="${src.dir}/res.resources"/>
      </resource>
    </dn:csc>
    <au:assertFileExists file="${testCSCRes.exe}"/>
    <exec executable="${testCSCRes.exe}" failonerror="true" />
    <delete file="${testCSCRes.exe}"/>
  </target>

  <target name="CSCResources-Mono" depends="validate_csc" if="gmcs.found">
    <property name="testCSCRes.exe"
      location="${build.dir}/ExampleCscRes.exe" />
    <dn:csc
      destFile="${testCSCRes.exe}"
      targetType="exe"
      includedefaultreferences="true"
      srcDir="${src.dir}"
      >
      <resource file="${src.dir}/res.resources"/>
    </dn:csc>
    <au:assertFileExists file="${testCSCRes.exe}"/>
    <exec executable="${mono.executable}" failonerror="true">
      <arg value="${testCSCRes.exe}"/>
    </exec>
    <delete file="${testCSCRes.exe}"/>
  </target>

  <target name="testCSCintrinsicFileset"
          depends="CSCintrinsicFileset-MS,CSCintrinsicFileset-Mono"/>

  <target name="CSCintrinsicFileset-MS" depends="validate_csc" if="csc.found">
    <property name="testCSC.exe"
      location="${build.dir}/ExampleCsc.exe"/>
    <dn:csc
      destFile="${testCSC.exe}"
      targetType="exe"
      srcDir="${src.dir}"
      includes="**/*.cs"
      >
    </dn:csc>
    <au:assertFileExists file="${testCSC.exe}"/>
    <exec executable="${testCSC.exe}" failonerror="true" />
    <delete file="${testCSC.exe}"/>
  </target>

  <target name="CSCintrinsicFileset-Mono" depends="validate_csc" if="gmcs.found">
    <property name="testCSC.exe"
      location="${build.dir}/ExampleCsc.exe"/>
    <dn:csc
      destFile="${testCSC.exe}"
      targetType="exe"
      srcDir="${src.dir}"
      includes="**/*.cs"
      includedefaultreferences="true"
      >
    </dn:csc>
    <au:assertFileExists file="${testCSC.exe}"/>
    <exec executable="${mono.executable}" failonerror="true">
      <arg value="${testCSC.exe}"/>
    </exec>
    <delete file="${testCSC.exe}"/>
  </target>

  <target name="testCSCdll" depends="CSCdll-MS,CSCdll-Mono"/>

  <target name="CSCdll-MS" depends="validate_csc" if="csc.found">
    <property name="testCSC.dll"
      location="${build.dir}/Example2.dll" />
    <dn:csc
      destFile="${testCSC.dll}"
      targetType="library"
      executable="csc"
      >
      <src dir="${src.dir}" includes="example2.cs"/>
    </dn:csc>
    <au:assertFileExists file="${testCSC.dll}"/>
    <property name="testCSC2.dll"
      location="${build.dir}/folder  with dir/Example3.dll" />
    <mkdir dir="${build.dir}/folder  with dir"/>
    <dn:csc
      destFile="${testCSC2.dll}"
      targetType="library"
      executable="csc"
      >
      <src dir="${src.dir}" includes="example3.cs"/>
    </dn:csc>
    <au:assertFileExists file="${testCSC2.dll}"/>
  </target>

  <target name="CSCdll-Mono" depends="validate_csc" if="gmcs.found">
    <property name="testCSC.dll"
      location="${build.dir}/Example2.dll" />
    <dn:csc
      destFile="${testCSC.dll}"
      targetType="library"
      includedefaultreferences="true"
      >
      <src dir="${src.dir}" includes="example2.cs"/>
    </dn:csc>
    <au:assertFileExists file="${testCSC.dll}"/>
  </target>

  <target name="CscReferences"
    depends="CscReferences-MS,CscReferences-Mono"/>

  <target name="CscReferences-MS" depends="validate_csc,CSCdll-MS"
          if="csc.found">
    <property name="testCscReferences.exe"
      location="${build.dir}/ExampleCsc2.exe" />
    <dn:csc
      destFile="${testCscReferences.exe}"
      targetType="exe"
      >
      <src file="${src.dir}/example.cs"/>
      <reference file="${testCSC.dll}" />
      <reference file="${testCSC2.dll}" />
      <define name="RELEASE" />
      <define name="DEBUG" if="undefined.property"/>
      <define name="def3" unless="undefined.property"/>
    </dn:csc>
    <au:assertFileExists file="${testCscReferences.exe}"/>
    <copy file="${testCSC2.dll}" todir="${build.dir}"/>
    <exec executable="${testCscReferences.exe}" failonerror="true" />
  </target>

  <target name="CscReferences-Mono" depends="validate_csc,CSCdll-Mono"
          if="gmcs.found">
    <property name="testCscReferences.exe"
      location="${build.dir}/ExampleCsc2.exe" />
    <dn:csc
      destFile="${testCscReferences.exe}"
      targetType="exe"
      includedefaultreferences="true"
      >
      <src file="${src.dir}/example.cs"/>
      <reference file="${testCSC.dll}" />
      <define name="RELEASE" />
      <define name="DEBUG" if="undefined.property"/>
      <define name="def3" unless="undefined.property"/>
    </dn:csc>
    <au:assertFileExists file="${testCscReferences.exe}"/>
    <exec executable="${mono.executable}" failonerror="true">
      <arg value="${testCscReferences.exe}"/>
    </exec>
  </target>

  <target name="ILASM-deps" depends="ILASM-Mono,ILASM-MS"/>

  <target name="testILASM"
    depends="ILASM-deps"
    if="ilasm.found"/>

  <target name="ilasm" depends="validate_ilasm"
      if="ilasm.found">
    <property name="testILASM.exe"
      location="${build.dir}/ExampleIlasm.exe" />
    <dn:ilasm
      destFile="${testILASM.exe}"
      targetType="exe"
      >
      <src dir="${src.dir}" includes="*.il"/>
    </dn:ilasm>
    <au:assertFileExists file="${testILASM.exe}"/>
  </target>

  <target name="ILASM-MS" depends="ilasm"
    if="ilasm.found" unless="mono.ilasm.found">
    <exec executable="${testILASM.exe}"
      failonerror="true"/>
  </target>

  <target name="ILASM-Mono" depends="ilasm"
    if="mono.ilasm.found">
    <exec executable="${mono.executable}"
      failonerror="true">
      <arg value="${testILASM.exe}"/>
    </exec>
  </target>

  <!-- not including this in the test as it creates an exe in the src dir -->

  <target name="XtestIlasmNoDestFile"  depends="validate_ilasm">
    <dn:ilasm
      targetType="exe"
      >
      <src dir="${src.dir}" includes="**/*.il"/>
    </dn:ilasm>
  </target>

  <!-- just here to look at fileset refid conversion by hand -->
  <target name="echoFileset">
    <fileset id="ilasm" dir="${src.dir}" includes="**/*.il" />
    <property name="ilasm.string" refid="ilasm"/>
    <echo>${ilasm.string}</echo>
  </target>

  <target name="testILDASM"   depends="ILASM-deps" if="ildasm.found"
    unless="ms.net20+">
    <property name="testILDASM.il"
      location="${build.dir}/ExampleIldasm.il" />
    <dn:ildasm
      srcFile="${testILASM.exe}"
      destFile="${testILDASM.il}"
      metadata="true"
      header="true"
      linenumbers="true"
      encoding="ascii"
      />
    <au:assertFileExists file="${testILDASM.il}"/>
  </target>

  <!-- this is an error -->
  <target name="testILDASM_empty"   depends="validate_ilasm" >
    <au:expectfailure>
      <dn:ildasm/>
    </au:expectfailure>
  </target>

  <target name="testJsharp" depends="init" if="jsharp.found" >
    <property name="jsharp.exe"
      location="${build.dir}/jsharp.exe" />
    <dn:jsharpc
        destFile="${jsharp.exe}"
        targetType="exe"
        >
      <src dir="${src.dir}" includes="*.java"/>
    </dn:jsharpc>
    <exec executable="${jsharp.exe}" failonerror="true" />
  </target>

  <target name="testCSCresponseFile" depends="validate_csc" >
    <property name="testCSCresponseFile.exe"
      location="${build.dir}/testCSCresponseFile.exe" />
    <dn:csc
      destFile="${testCSCresponseFile.exe}"
      targetType="exe"
      executable="${cs.compiler}"
      useResponseFile="true"
      srcDir="${src.dir}"
      >
    </dn:csc>
    <au:assertFileExists file="${testCSCresponseFile.exe}"/>
    <delete file="${testCSCresponseFile.exe}"/>
  </target>


</project>

