Nant equivalent of Ant task Replace

Look for the Ant equivalent Replace task in Nant

+3
source share
3 answers

As far as I can tell, the filter functions in NAnt are exactly what you are looking for. Supported by tasks <copy>and <move>, you define token replacement expressions that transform the contents of a file during file copy and move operations.

+3
source

Nantes has no direct equivalent.

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

0
source

You can write your own 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
source

Source: https://habr.com/ru/post/1744447/


All Articles