Nant相当于Ant替换任务

时间:2010-05-07 04:55:23

标签: ant nant

在Nant中寻找Ant替换任务的等价物

3 个答案:

答案 0 :(得分:3)

据我所知,the filter features in NAnt正是您所寻找的。在<copy><move>任务的支持下,您可以定义将在文件复制/移动操作期间转换文件内容的令牌替换表达式。

答案 1 :(得分:0)

Nant没有直接的等价物。

此链接http://maonet.wordpress.com/2008/08/12/replace-file-content-in-nant-task/有两个选项。

答案 2 :(得分:0)

您可以编写自定义任务:

<target name="deftask-replregexp"
        description="define custum task like ants task replaceregexp">
  <script language="C#">
    <!-- NAnt 0.92 reference states that this is loaded by default. Do not 
         know why this is required. --> 
    <references>
      <include name="System.dll"/>
    </references>
    <imports> 
      <import namespace="System.Text.RegularExpressions" />
    </imports>
    <code>               
      <![CDATA[
        [TaskName("replaceregexp")]
        public class ReplaceRegExp : Task 
        {

          #region Public Instance Properties

          [TaskAttribute("file", Required=true)]
          public string InFile { get; set; }

          [TaskAttribute("match", Required=true)]
          public string Match { get; set; }

          [TaskAttribute("replace", Required=true)]
          public string Replace { get; set; }

          #endregion Public Instance Properties

          #region Override implementation of Task

          protected override void ExecuteTask() 
          {
            if ( !File.Exists(this.InFile) )
            {
              throw new BuildException("The file \"" + this.InFile 
                                       + "\" does " + "not exist.");              
            }
            doReplaceRegExp( this.InFile, this.Match, this.Replace);
          }
          private static void doReplaceRegExp(String inFile, 
                                              String pattern, 
                                              String replacement)
          {
            String outFile = Path.GetTempFileName();
            Regex regex = new Regex(pattern, RegexOptions.Compiled
                                             | RegexOptions.IgnoreCase);
            bool replaceDone = false;
            try
            {
              using (StreamReader input = new StreamReader(inFile, true))
              using (TextWriter output = new StreamWriter(
                                              new FileStream(outFile,
                                                             FileMode.Create),
                                              input.CurrentEncoding))
              {
                  String line;
                  do
                  {
                      line = input.ReadLine();
                      if (line != null)
                      {
                          System.Text.RegularExpressions.Match m 
                              = regex.Match(line);
                          if (m.Success)
                          {
                              replaceDone = true;
                              line = regex.Replace(line, replacement, 1);
                          }
                          output.WriteLine(line);
                      }
                  } while (line != null);
                  input.Close();
                  output.Close();
              }
              if (replaceDone)
              {
                  File.Copy(outFile, inFile, true);
              }
            }
            finally
            {
              if (outFile != null && File.Exists(outFile))
              {
                  File.Delete(outFile);
              }
            }
          }

          #endregion Override implementation of Task
        }
      ]]>
      </code>
    </script>
</target>