2
votes

Unfortunately my knowledge of NANT stuff is very limited but embarked today on updating our build to run some code. We use cruisecontrol.

This works fine - however I need to process two folders so was trying to put common code into a traditional c# method.

<echo message="Ensure only 4 directories in the backup directory"/>
    <script language="C#">
      <code>
        <![CDATA[
  public static void ScriptMain(Project project)
  {
      string path = project.Properties["build.ReleasesShareLocation.BackupParent"];
      string [] folders = System.IO.Directory.GetDirectories(path,"*", System.IO.SearchOption.TopDirectoryOnly);

      System.Collections.ArrayList dirlist = new System.Collections.ArrayList();
      foreach (string folder in folders)
      {
          dirlist.Add(new System.IO.DirectoryInfo(folder));
      }
      dirlist.Sort(new DirectoryInfoDateComparer());
      dirlist.Reverse();

      for (int i = 4; i < dirlist.Count; i++)
      {
          string foldername = ((System.IO.DirectoryInfo)dirlist[i]).Name;
          try
          {
              ((System.IO.DirectoryInfo)dirlist[i]).Delete(true);
              project.Log(Level.Info, String.Format("Deleted folder {0}", foldername));

          }
          catch { project.Log(Level.Warning, String.Format("Unable to delete folder {0}", foldername)); }
      }
  }

    internal class DirectoryInfoDateComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            System.IO.DirectoryInfo di1 = (System.IO.DirectoryInfo)x;
            System.IO.DirectoryInfo di2 = (System.IO.DirectoryInfo)y;

            return di1.CreationTime.CompareTo(di2.CreationTime);
        }
    }
    ]]>
      </code>
    </script>

Rather than repeat all the code - I tried to add a static class with a method to do all the processing so that the main only passes the two paths. But in running the build on the server (is there a way for me to "compile" this locally??) it errored.

This is the code that errored - I'm not sure if I've missed a simple syntax error (I did try to copy and paste the code in visual studio to make sure the brackets and stuff matched up) or if there is a different way I need to do it in NANT build..

<echo message="Ensure only 4 directories in the backup directory"/>
    <script language="C#">
      <code>
        <![CDATA[
  public static void ScriptMain(Project project)
  {
      string path = project.Properties["build.ReleasesShareLocation.BackupParent"];
      string path2 = project.Properties["build.DeltaScriptFileShareLocation.BackupParent"];
      Processor.ProcessFolder(path);
      Processor.ProcessFolder(path2);
  }

internal static class Processor
{
    public static void ProcessFolder(string path)
    {
        string [] folders = System.IO.Directory.GetDirectories(path,"*", System.IO.SearchOption.TopDirectoryOnly);

        System.Collections.ArrayList dirlist = new System.Collections.ArrayList();
        foreach (string folder in folders)
        {
            dirlist.Add(new System.IO.DirectoryInfo(folder));
        }
        dirlist.Sort(new DirectoryInfoDateComparer());
        dirlist.Reverse();

        for (int i = 4; i < dirlist.Count; i++)
        {
            string foldername = ((System.IO.DirectoryInfo)dirlist[i]).Name;
            try
            {
                ((System.IO.DirectoryInfo)dirlist[i]).Delete(true);
                project.Log(Level.Info, String.Format("Deleted folder {0}", foldername));

            }
            catch { project.Log(Level.Warning, String.Format("Unable to delete folder {0}", foldername)); }
        }
    }
}

    internal class DirectoryInfoDateComparer : System.Collections.IComparer
    {
        public int Compare(object x, object y)
        {
            System.IO.DirectoryInfo di1 = (System.IO.DirectoryInfo)x;
            System.IO.DirectoryInfo di2 = (System.IO.DirectoryInfo)y;

            return di1.CreationTime.CompareTo(di2.CreationTime);
        }
    }
    ]]>
      </code>
    </script>

Thanks for any assistance :)


Edit:

I went back through the CC logs and re-read the error message - this time understanding what it was trying to tell me. Turns out I needed to pass my new method the Project parameter. :)

Is there a way to be able to compile the code without having to run a build to find errors like this? I tried pasting it into Visual Studio but as I didn't have the Nant library referenced it wasn't highlighting the error effectively. Or is there something you can download and install on a dev machine to reference appropriate dlls?

2

2 Answers

1
votes

Go to command prompt, navigate to the folder where you installed NANT, and type in your NANT build command manually, for example:

 nant -f:MyBuildFile.build

That way you'll see the errors, and we can help further.

1
votes

In re-reading the error logs I found within the wall of red text the answer.

Turns out I needed to pass my new method the Project parameter (as I was referring to Project properties).

So problem solved!