You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Jay Dickon Glanville <di...@gmail.com> on 2007/11/05 19:36:47 UTC

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?

Thanks

JDG

PS: I'm limited by my current environment to ant 1.6.5.

-- 
Jay Dickon Glanville

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: how to set a property based on the contents of files in a tree?

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 



-----Original Message-----
From: Jay Dickon Glanville [mailto:dickon.glanville@gmail.com] 
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: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: how to set a property based on the contents of files in a tree?

Posted by "Rebhan, Gilbert" <Gi...@huk-coburg.de>.
 
Hi,


-----Original Message-----
From: David Weintraub [mailto:qazwart@gmail.com] 
Sent: Tuesday, November 06, 2007 5:52 PM
To: Ant Users List
Subject: Re: how to set a property based on the contents of files in a
tree?

/*
I'd just use <property name="files_path" refid="fileset"> and then
check if property "files_path" is greater than zero.

 <fileset dir="WebInfra/Source" id="fileset" includes="**/*">
   <containsregexp expression="dg" />
 </fileset>

<property name="files_path" refid="fileset">

<condition property="prop_from_condition">
    <equals arg1="${files_path}" arg2=""/>
</condition>
*/

or a bit shorter =

<fileset dir="Y:/test" id="fileset" includes="**/*">
  <containsregexp expression="dg" />
</fileset>

<condition property="prop_from_condition">
  <equals arg1="${toString:fileset}" arg2=""/>
</condition>

as already said, if it doesn't get more complicated
i would stick with the 'normal' ant solution, if i need
more i would go with <script>, <scriptdef> or an own task.


Regards, Gilbert


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: how to set a property based on the contents of files in a tree?

Posted by Jay Dickon Glanville <di...@gmail.com>.
Thanks for the suggestion.  It does look a little cleaner.

JDG

On Nov 6, 2007 11:51 AM, David Weintraub <qa...@gmail.com> wrote:
> I'd just use <property name="files_path" refid="fileset"> and then
> check if property "files_path" is greater than zero.
>
>  <fileset dir="WebInfra/Source" id="fileset" includes="**/*">
>    <containsregexp expression="dg" />
>  </fileset>
>
> <property name="files_path" refid="fileset">
>
> <condition property="prop_from_condition">
>     <equals arg1="${files_path}" arg2=""/>
> </condition>
>
> That I believe works in 1.6.5. I took a quick look at a few other ways
> using selectors, but I don't think those work with 1.6.5.
>
>
>
>
> On 11/5/07, Jay Dickon Glanville <di...@gmail.com> wrote:
> > 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?
> >
> > Thanks
> >
> > JDG
> >
> > PS: I'm limited by my current environment to ant 1.6.5.
> >
> > --
> > Jay Dickon Glanville
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> >
> >
>
>
> --
> --
> David Weintraub
> qazwart@gmail.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>



-- 
Jay Dickon Glanville

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: how to set a property based on the contents of files in a tree?

Posted by David Weintraub <qa...@gmail.com>.
I'd just use <property name="files_path" refid="fileset"> and then
check if property "files_path" is greater than zero.

 <fileset dir="WebInfra/Source" id="fileset" includes="**/*">
   <containsregexp expression="dg" />
 </fileset>

<property name="files_path" refid="fileset">

<condition property="prop_from_condition">
    <equals arg1="${files_path}" arg2=""/>
</condition>

That I believe works in 1.6.5. I took a quick look at a few other ways
using selectors, but I don't think those work with 1.6.5.



On 11/5/07, Jay Dickon Glanville <di...@gmail.com> wrote:
> 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?
>
> Thanks
>
> JDG
>
> PS: I'm limited by my current environment to ant 1.6.5.
>
> --
> Jay Dickon Glanville
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


-- 
--
David Weintraub
qazwart@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org