Search code examples
c#nant

Nant <script> task seems not working


I have a simple nant build file in which I want to delete files in some directory and directories in another directory:

    <?xml version="1.0" ?>
    <project name="maintenance">
      <include buildfile="common.properties" />
    <target name="clean">  
       <script language="C#">              
              <imports>
                 <import namespace="System.Diagnostics" />
              </imports>
    <code>
         <![CDATA[               

   public static void DeleteUnusedFilesAndFolders(Project project) {

    var screensDirectory = new DirectoryInfo(@"${failed.screens.dir}");
    var testsResultsDirectory = new DirectoryInfo(@"${results.dir}");
    var _7daysAgoDate = DateTime.Now.AddDays(-7);
    foreach (var file in screensDirectory.GetFiles())
     {                
         if (file.CreationTime.CompareTo(_7daysAgoDate) == -1)
         {
            project.Log(Level.Info, "Delete file: " + file.Name);
            file.IsReadOnly = false;
            file.Delete();
          }
      }

     foreach (var dir in testsResultsDirectory.GetDirectories())
       {
             if (dir.CreationTime.CompareTo(_7daysAgoDate) == -1)
             { 
                  project.Log(Level.Info, "Delete directory: " + dir.Name);
                  try
                  {
                      dir.Delete(true);
                  }
                  catch (Exception exception)
                  {
                      Console.WriteLine("Caught exception: "+exception.Message);
                  }

             }
    }                   
}
        ]]>
      </code>
    </script> 
  </target>    
</project>

When I run this nant file it writes that 'Build success' but nothing happens. What I missed?


Solution

  • I found a simple solution - use an echo:

    <echo message="Calling function: ${Cleaning::DeleteUnusedFilesAndFolders()}"/>
    

    Before that you should define prefix:

    <project name="maintenance" prefix="Cleaning">