brettjonesdev
brettjonesdev

Reputation: 2281

Output replacement summary in ant replaceregexp task

Is there a summary="true" equivalent in ant replaceregexp tasks like there is in the simple ant replace task?

Here's my use case. I am replacing an ant replace task with a replaceregexp task, like so:

<replace dir="${dir}" summary="true" value="" token="Some Text">
    <include name="**/*.jsp" />
</replace>

Becomes this:

<replaceregexp byline="true">
    <regexp pattern="Some\s*Text"/>
    <substitution expression=""/>
 <fileset dir="${dir}" includes="**/*.jsp"/>
</replaceregexp>

However, I cannot find a way to have my ant build output the number of replacements which have been made. There is no summary attribute for replaceregexp, and the documentation does not offer any ready suggestions. Is this possible?

Upvotes: 2

Views: 2005

Answers (1)

Rebse
Rebse

Reputation: 10377

From reading the source of ReplaceRegexp task there ain't such a feature.
ant184 => src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java, line 351 :

protected void doReplace(File f, int options) throws IOException {
        File temp = FILE_UTILS.createTempFile("replace", ".txt", null, true, true);

        Reader r = null;
        Writer w = null;
        BufferedWriter bw = null;

        try {
            if (encoding == null) {
                r = new FileReader(f);
                w = new FileWriter(temp);
            } else {
                r = new InputStreamReader(new FileInputStream(f), encoding);
                w = new OutputStreamWriter(new FileOutputStream(temp),
                                           encoding);
            }

            BufferedReader br = new BufferedReader(r);
            bw = new BufferedWriter(w);

            boolean changes = false;

            log("Replacing pattern '" + regex.getPattern(getProject())
                + "' with '" + subs.getExpression(getProject())
                + "' in '" + f.getPath() + "'" + (byline ? " by line" : "")
                + (flags.length() > 0 ? " with flags: '" + flags + "'" : "")
                + ".", Project.MSG_VERBOSE);

means use loglevel verbose to log the replacements:
ant -verbose -f yourfile.xml
or
ant -v -f yourfile.xml

Get the ant source distribution file and simply extend the tasks doReplace() method to get your desired output, for example, a counter for the replacements, controlled by a new task attribute 'summary' false|true
You may fill an enhancement report and submit a patch to make it available for other users in the next ant release, more details here.

Upvotes: 2

Related Questions