开发者

Nant equivalent of the Ant Replace task

开发者 https://www.devze.com 2022-12-29 05:38 出处:网络
Looking for the equivalent of A开发者_Go百科nt Replace task in NantAs far as I can tell, the filter features in NAnt is exactly what you\'re looking for. Supported by the <copy> and <move>

Looking for the equivalent of A开发者_Go百科nt Replace task in Nant


As far as I can tell, the filter features in NAnt is exactly what you're looking for. Supported by the <copy> and <move> tasks, you define token replacement expressions that will transform file content during file copy/move operations.


There is no direct equivalent in Nant.

This link http://maonet.wordpress.com/2008/08/12/replace-file-content-in-nant-task/ has a couple of options.


You can write a custom task:

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

精彩评论

暂无评论...
验证码 换一张
取 消