nant脚本c#编译错误

时间:2011-05-31 07:06:23

标签: c# cruisecontrol.net nant

不幸的是,我对NANT的知识非常有限,但今天开始更新我们的构建以运行一些代码。我们使用巡航控制。

这很好 - 但是我需要处理两个文件夹,所以试图将常用代码放入传统的c#方法中。

<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>

而不是重复所有代码 - 我尝试添加一个带有方法的静态类来执行所有处理,以便main只传递两个路径。但是在运行服务器上的构建时(有没有办法让我在本地“编译”它?)它会出错。

这是错误的代码 - 我不确定我是否错过了一个简单的语法错误(我确实尝试在Visual Studio中复制并粘贴代码以确保括号和内容匹配)或者是否存在在NANT构建中我需要采用不同的方式..

<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>

感谢您的帮助:)


编辑:

我回过头来看日志并重新阅读错误信息 - 这次是了解它试图告诉我的内容。结果我需要传递我的新方法Project参数。 :)

有没有办法能够编译代码而无需运行构建来查找这样的错误?我尝试将其粘贴到Visual Studio中,但由于我没有引用Nant库,因此没有有效地突出显示错误。或者您是否可以在开发机器上下载并安装以引用适当的dll?

2 个答案:

答案 0 :(得分:1)

转到命令提示符,导航到安装NANT的文件夹,然后手动输入NANT构建命令,例如:

 nant -f:MyBuildFile.build

这样你就会看到错误,我们可以进一步提供帮助。

答案 1 :(得分:1)

在重新阅读错误日志时,我在红色文本墙内找到了答案。

原来我需要传递我的新方法Project参数(因为我指的是Project属性)。

问题解决了!