Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle print directories in a folder - X level

I have the following folder structure under build folder (which you get during a Gradle build):

CDROM/disk1
CDROM/disk1/disk1file1a.txt
CDROM/disk1/disk1file1b.txt

CDROM/disk2/disk2file2a.txt
CDROM/disk2/disk2file2btxt
CDROM/disk2/disk2folder2x
CDROM/disk2/disk2folder2y

CDROM/disk3
CDROM/disk3/disk3
CDROM/disk3/disk33
CDROM/disk3/disk33/disk3


CDROM/folder1

CDROM/file1.txt

How can I tell Gradle to show me the following:

  1. Print only the top level / direct child folders (only) in folder "CDROM"
    i.e. it should print only disk1, disk2, disk3 and folder1

  2. Print only the top level / direct child folders (only) which has a pattern of disk[0-9] i.e. diskX where X is a number.
    i.e. it should print only disk1, disk2 and disk3

The following will do it, but I think there should be an efficient way to achieve the same and where one can define patterns and DON'T have to use "IF" statements that I have used below.

FileTree dirs = fileTree (dir: "$buildDir/CDROM", include: "disk*/**")

        dirs.visit { FileVisitDetails fd ->

                if (fd.directory && fd.name.startsWith('disk')){

                        println "------  $buildDir/CDROM_Installers/${fd.name}   ---------------"

                }

        }
like image 325
AKS Avatar asked Jul 23 '14 18:07

AKS


1 Answers

By top level if you mean only direct children of CDROM then this should be as easy as:

new File("${buildDir}/CDROM").eachDir{ if(it.name ==~/disk.*/) println it}

If you want more control on depth and other things, then you can try variations of following code:

new File("${buildDir}/CDROM").traverse( [maxDepth: 2, filter: ~/.*disk\d/, 
                                        type: groovy.io.FileType.DIRECTORIES]){
   println it // or do whatever
}

see traverse for more details.

like image 115
kdabir Avatar answered Sep 22 '22 14:09

kdabir