Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I carry out math functions in the Ant 'ReplaceRegExp' task?

Tags:

regex

ant

I need to increment a number in a source file from an Ant build script. I can use the ReplaceRegExp task to find the number I want to increment, but how do I then increment that number within the replace attribute?

Heres what I've got so far:

<replaceregexp file="${basedir}/src/path/to/MyFile.java"
    match="MY_PROPERTY = ([0-9]{1,});"
    replace="MY_PROPERTY = \1;"/>

In the replace attribute, how would I do

replace="MY_PROPERTY = (\1 + 1);"

I can't use the buildnumber task to store the value in a file since I'm already using that within the same build target. Is there another ant task that will allow me to increment a property?

like image 452
roryf Avatar asked Sep 25 '08 12:09

roryf


2 Answers

You can use something like:

<propertyfile file="${version-file}"> <entry key="revision" type="string" operation="=" value="${revision}" /> <entry key="build" type="int" operation="+" value="1" />

so the ant task is propertyfile.

like image 166
Yuval F Avatar answered Nov 18 '22 16:11

Yuval F


In ant, you've always got the fallback "script" tag for little cases like this that don't quite fit into the mold. Here's a quick (messy) implementation of the above:

    <property name="propertiesFile" location="test-file.txt"/>

    <script language="javascript">
        regex = /.*MY_PROPERTY = (\d+).*/;

        t = java.io.File.createTempFile('test-file', 'txt');
        w = new java.io.PrintWriter(t);
        f = new java.io.File(propertiesFile);
        r = new java.io.BufferedReader(new java.io.FileReader(f));
        line = r.readLine();
        while (line != null) {
            m = regex.exec(line);
            if (m) {
                val = parseInt(m[1]) + 1;
                line = 'MY_PROPERTY = ' + val;
            }
            w.println(line);
            line = r.readLine();
        }
        r.close();
        w.close();

        f.delete();
        t.renameTo(f);
    </script>
like image 40
bsanders Avatar answered Nov 18 '22 18:11

bsanders