How to change one line in a file using NAnt?
Asked Answered
C

3

6

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

Cannon answered 26/7, 2011 at 11:37 Comment(3)
Have you considered whether "hard" coding the server path into a js file is the best solution (to whatever problem it solves)? Obtaining the value from the whole URL in client side javascript, or emitting a small piece of inline js from the server side page (based on the actual host header) might be some alternatives.Sphinx
Yes, it is necessary unfortunately. We are hosting our appliaction as a plugin within a third party application, and this line in the JS is needed for the third party system to find us.Cannon
If string::replace doesn't work <regex> can do the job. I Need to work this out. Stay tuned...Chausses
C
12

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>
Chausses answered 29/7, 2011 at 13:13 Comment(1)
I had the same problem as Aedna. Using (?'BEFORE'[\w\s\W]*) instead of (?'BEFORE'.*) (and similar for the AFTER matching group) worked for me. I'm wondering if it's an encoding thing.Arsenious
C
5

shouldn't it be [\w\s\W]* instead of .* in AFTER and BEFORE to be able to capture all the lines?

in my case .* was capturing only line, whereas [\w\s\W]* worked for entire file

Catalyst answered 13/11, 2013 at 19:6 Comment(0)
A
1

can also use the copy task along with filterchain and replacetokens filter.

Here's an example:

            <token key="WebConfig.EnvironmentName" value="${env_webconfig_EnvironmentName}" />
            <token key="WebConfig.SMTPServerName" value="${env_webconfig_SMTPServerName}" />
            <token key="WebConfig.DatabaseConnectionString" value="${env_drmportal_webconfig_DatabaseConnectionString}" />

        </replacetokens>
    </filterchain>
</copy>

I retain all my template files in a /config/ folder (e.g. web.config.template) and my use of the copy task replaces the values when copying to the same /config/ folder but without the ".template" file extension. I then do what's needed afterwards...\

I will admit that it is a bit cumbersome using properties in the way that you have to for this, but you have flexibility in that you can load different sets of property values by environment (e.g. local, staging, production, etc.) but that's a little more than I think you're asking.

Astroid answered 15/8, 2011 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.