dom_watson
dom_watson

Reputation: 328

Replace regex matches in a file with dynamic content

I have a file that looks something like this:

// run script Y
#include ./relativePath/to/script.y

// run script Z
#include ./relativePath/to/script/z.z

// etc.

What I would like ANT to do, is take this file and replace all the includes with the actual content of the files that are pointed to by the #include 'directive', relative to the file itself. I am searching around replaceregexp but not getting much joy.

Can anyone point me in the direction to look in?

TIA

Upvotes: 0

Views: 499

Answers (2)

dom_watson
dom_watson

Reputation: 328

I didn't find a perfect way to do this. I did however come up with a solution that matched my needs.

Instead of starting with the single source file and trying to figure out the files to replace, I looped over the files that I knew might be included, replacing 'included' instances of them with their file content.

Something like:

<target name="fubar">
    ...

    <foreach target="injectFile" inheritall="true" param="include.file">
        <fileset dir="${src.dir}" casesensitive="yes" />
    </foreach>
</target>

<target name="injectFile">
    ... code that works out relative path of include file here ...

    <loadfile property="include.file.content" srcFile="${include.file}" />

    <replace file  = "${build.file}"
             token = "#include ${include.file.relative.path}"
             value = "${include.file.content}" />
</target>

Upvotes: 1

J&#246;rg Beyer
J&#246;rg Beyer

Reputation: 3671

your syntax is very close to the C preprocessor. Is it a solution to put the filenames in quotes, e.g.:

#include "./relativePath/to/script.y"

an to feed you file in the cpp:

cpp -o output inputfile 

Upvotes: 0

Related Questions