User Tools

Site Tools


nant:replace-all-in-file

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

nant:replace-all-in-file [2019/08/23 10:40] (current)
odefta created
Line 1: Line 1:
 +====== Replace a string in a file with nant ======
 +
 +File content:
 +<file cpp version.cpp>​
 +  #include "​stdio.n"​
 +  #define VERSION "​x.x.x.x"​
 +  void main() {
 +  }
 +</​file>​
 +
 +Create replace.build file to replace version with $BuildNumber:​
 + <​file xml replace.build>​
 +<​version="​1.0"​ encoding="​utf-8"​ ?>
 +<project name="​replace.line"​ default="​replace">​
 +  <target name="​replace"​ descripton="​replaces a line">​
 +    ​
 +<​loadfile file="​${version.file}"​ property="​version.file.content"​ />
 +  <regex
 +      input="​${version.file.content}"​
 +      pattern="​(?'​BEFORE'​[\w\s\W]*)#​define\sVERSION\s*&​quot;​[x][\.][x][\.][x][\.][x]&​quot;​(?'​AFTER'​[\w\s\W]*)"​ />
 +   <​echo
 +      file="​${version.file}"​
 +      message="​${BEFORE}#​define VERSION &​quot;​${BuildNumber}&​quot;​${AFTER}"​
 +      append="​false"​ />
 +
 +</​target>​
 +
 +</​project>​
 + </​file>​
 +
 +
 +Run it with: 
 +  nant -buildfile:​replace.build -D:​version.file=version.cpp -D:​BuildNumber=9.1.0.1
 +
  
nant/replace-all-in-file.txt ยท Last modified: 2019/08/23 10:40 by odefta