Search code examples
.netnant

How to change one line in a file using NAnt?


I need to use NAnt to update one specific line in a .js file. The line will be something like:

global.ServerPath = 'http://server-path/';

I need a way to update the "server-path" part of that line with that of the destination server.
ReplaceString is no good, since I won't know what the path in the file is when I update it.

Any help?

Thanks in advance


Solution

  • If string::replace doesn't work <regex> can do the job. This is it:

    <?xml version="1.0" encoding="utf-8" ?>
    <project name="replace.line" default="replace">
      <target name="replace" descripton="replaces a line">
        <property
          name="js.file"
          value="C:\foo.js" />
        <loadfile file="${js.file}" property="js.file.content" />
        <regex
          input="${js.file.content}"
          pattern="(?'BEFORE'.*)global\.ServerPath\s*=\s*'[^']*';(?'AFTER'.*)" />
        <echo
          file="${js.file}"
          message="${BEFORE}global.ServerPath = 'http://bla/';${AFTER}"
          append="false" />
      </target>
    </project>