Does anyone know how to find and replace text inside a file with Phing?
If you don't want to copy files and just replace a string in the current folder where your files reside, do a reflexive task:
<reflexive>
<fileset dir=".">
<include pattern="*.js" />
</fileset>
<filterchain>
<replaceregexp>
<regexp pattern="SEARCH" replace="REPLACEMENT"/>
</replaceregexp>
</filterchain>
</reflexive>
I use this on my phing build.xml file
<exec command="find ./ -type f -name '*.php' | xargs sed -i 's|x--Jversion--x|${jversion}|g'" dir="${targetdir}/_package/${extname}.${package.version}" />
The simplest way to achieve this using 'traditional' tools would be sed
:
sed -i 's/old/new/g' myfile.txt
And if it is ant-based then this should help: http://ant.apache.org/manual/Tasks/replace.html
The simplest form would be <replace file="myfile.html" token="OLD" value="NEW"/>
.
And if you really need it, you could run external tools with ant as documented at http://ant.apache.org/manual/Tasks/exec.html, which means that among other things you could, for example, call sed from ant with something like:
<exec executable="sed">
<arg value="s/old/new/g" />
<arg value="$MY_FILE" />
</exec>
You can replace text inside files using filters. Filters are used inside other file operation tasks such as copy.
I believe the main idea behind filters is that you can have template files with tokens instead of real values and you then substitute the tokens as a part of the copy process.
Quick example: have a database configuration template file stored in a template directory. Then you copy it to the target configuration file using:
<copy file="templates/database.config.php.tpl" tofile="config/database.config.php" overwrite="true">
<filterchain>
<replacetokens begintoken="%%" endtoken="%%">
<!-- MySQL TOKENS -->
<token key="dbname" value="${db.mysql.dbname}" />
<token key="dbhost" value="${db.mysql.host}" />
<token key="dbport" value="${db.mysql.port}" />
<token key="dbuser" value="${db.mysql.username}" />
<token key="dbpassword" value="${db.mysql.password}" />
</replacetokens>
</filterchain>
</copy>
There are plenty of other filters (e.g. regex search and replace) available. See more about filters in the documentation: http://phing.info/docs/guide/stable/chapters/appendixes/AppendixD2-CoreFilters.html
The answer given by Acme, is the right one. If you try to copy a file to itself in order to modify it, yells telling that yo cannot self-copy.
<reflexive file="./app/config/config.yml" tofile="./app/config/config.yml">
<filterchain>
<replacetokens begintoken="__" endtoken="__">
<token key="BUILD_VERSION" value="Replace Value" />
</replacetokens>
</filterchain>
</reflexive>
This works well for me.
I was looking for the same thing, and I found out that exists a filter named ExpandProperties which allows to replace properties in the copied file. For example I used it in a apache virtual host template:
<target name="apache-config" description="Generates apache configuration">
<!-- Default value for Debian/Ubuntu -->
<property name="apache.vhost.dir" value="/etc/apache2/sites-available" override="false"/>
<copy file="${application.startdir}/docs/vhost.conf.tpl" todir="${apache.vhost.dir}" overwrite="true">
<filterchain>
<expandproperties/>
</filterchain>
</copy>
<echo message="Apache virtual host configuration copied, reload apache to activate it"/>
</target>
And in the template file
<VirtualHost *:80>
DocumentRoot "${application.startdir}/public"
ServerName ${apache.default.host}
<Directory "${application.startdir}/public">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
In this way you don't need to explicitly list all the tokens you want replaced, pretty useful...