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 look something like this:

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

I need a way to update the server path portion of this line using the destination server.
ReplaceString is not suitable, as I will not know what the path in the file is when I update it.

Any help?

Thanks in advance

+6
source share
3 answers

If string::replace does not work, <regex> can do the job. It's him:

 <?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> 
+12
source

there should not be [\ w \ s \ W] * instead. * in AFTER and BEFORE to be able to commit all rows?

in my case. * recorded only a line, while [\ w \ s \ W] * worked for the whole file

+4
source

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

Here is 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 save all the template files in the / config / folder (for example, web.config.template), and my use of the copy task replaces the values ​​when copying to the same / config / folder, but without the ".template" extension. Then I do what I need afterwards ... \

I admit that it is a bit cumbersome, using properties the way you need it, but you have the flexibility that you can load different sets of property values ​​across environments (e.g. local, setting, production, etc.) but it is a little more than I think you ask.

+1
source

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


All Articles