Is there a way to do a collectEntries() on a FileTree?


(gradle-shorn) #1

At the moment, I’m doing this:

def entries = []
myFileTree.each{ entries << it }
return entries.collectEntries([:]){ [it, myFileMappingFunction(it)] }

It feels wrong to be doing that intermediate call to “each(Closure)”.

I’d like to do this, or something like it:

myFileTree.collectEntries([:]){ [it, myFileMappingFunction(it)] }

Am I missing something?

Where is the FileTree.each() defined, I couldn’t find it in the doco - I only know it exists because of the user guide.


(Luke Daley) #2

Hi Shorn,

You’d have to do:

myFileTree.files.collectEntries([:]){ [it, myFileMappingFunction(it)] }

You’ll notice that FileTree has a ‘getFiles()’ method that you can use to get a real collection that has all the Groovy methods.

The ‘each()’ method works out of the box because ‘FileTree’ implements ‘Iterable’, for which Groovy adds the ‘each()’ method. You could argue that ‘collectEntries()’ should be available to all 'Iterable’s, but that’s an argument for the Groovy language.


(gradle-shorn) #3

Ah, cool - that’ll do nicely.

Thanks.