-----Original Message-----
From: Jay Dickon Glanville [mailto:[EMAIL PROTECTED]
Sent: Monday, November 05, 2007 7:37 PM
To: Ant User
Subject: how to set a property based on the contents of files in a tree?
/*
Hello all,
I'm doing something, but I'm not sure I'm doing it the best way
possible. So, I thought I'd ask the experts.
What I want to do is set a property if at least one file in a
directory tree contains a regular expression X. Here's what I'm
doing:
Create a fileset containing all files that match this condition:
<fileset dir="WebInfra/Source" id="fileset" includes="**/*">
<containsregexp expression="dg" />
</fileset>
Convert that fileset to a string that contains the filenames in the
fileset:
<pathconvert pathsep="--" property="files_path" refid="fileset" />
Set the property if the filename string is longer then 0:
<condition property="prop_from_condition" value="true">
<length string="${files_path}" trim="true" when="greater" length="0"
/>
</condition>
So, is this the best way to set a property to true if a directory tree
contains a file that matches a RegEx? Is there a better way?
*/
If it works for you, then it's OK
if it gets more complicated i would start with a basic scriptdef like
the following example and add more logic when i need =
<scriptdef name="checkFiles" language="ruby">
<attribute name="dir" />
<attribute name="extension" />
<attribute name="regex" />
<attribute name="prop" />
<![CDATA[
filesfound=""
regex=/#{$attributes.get("regex")}/
Dir[$attributes.get("dir")+'/'+$attributes.get("extension")].each do
|path|
if File.open(path).read.scan(regex)
filesfound += path + " "
end
end
if filesfound != ""
$project.log "[checkfiles] Regex matches in == " + filesfound
$project.setProperty "#{$attributes.get('prop')}", "true"
end
]]>
</scriptdef>
usage =
recursive or not via extension attribute, f.e.
extension="*.txt"
also fileextension, f.e.
extension="**/*.xml"
example =
<checkFiles
dir="Y:/test"
extension="**/*.*"
regex="dg"
prop="prop_from_condition"
/>
<echo>
$${prop_from_condition} == ${prop_from_condition}
</echo>
gives you =
[checkfiles] Regex matches in == Y:/test/foo.txt
Y:/test/bla/foo/fooo.txt Y:/test/bla/foo/bar/foobar/bla.txt
Y:/test/bla/foo/bar/foobar/blaa.txt
[echo] ${prop_from_condition} == true
Regards, Gilbert
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]