Thursday, August 27, 2009

Using Groovy to gather a file path list

Just thought I'd share a good use case of Groovy as a basic script.

I've created this script in a need to speed up one of our legacy manual processes of gathering files to be released to another environment (QA, Staging and Prod).
Basically, when we used CVS, we would need to list out all of the files needed to be pushed manually. What this script does is allows you to specify the base directory in which it is to begin recursing. You can also specify the prefix to use.
For example, on another computer the paths where the CVS is located may be different.
This script also skips the CVS and SVN type files. And it switches the slashes from Windows (back-slash) to Unix style (forward-slash).

def outputMatch = { prependString, file ->
def fileName = file.path
fileName = fileName.replaceAll(/^\./, "");
fileName = fileName.replaceAll("\\\\", "/")
def prependStringStartPos = fileName.indexOf(prependString)
fileName.substring(prependStringStartPos, fileName.length())

def baseDir = null
def prependString = ""
def lookupString = null

if (args.length > 0 && args[0] != null && !args[0].equals("")){
// i.e. C:\dev\cvsrepo\devel\fr_site\frdart
baseDirectory = args[0]

if (args.length > 1 && args[1] != null && !args[1].equals("")){
// i.e. fr_site/frdart
prependString = args[1]

if (args.length > 2 && args[2] != null && !args[2].equals("")){
// i.e. frdart
lookupString = args[2]

def count = 0

if (baseDirectory == null)
baseDirectory = "."

new File(baseDirectory).eachFileRecurse{file ->

if (file.isFile() &&
!(file.parent.toLowerCase().endsWith("cvs") || file.parent.toLowerCase().endsWith("svn"))){
if ((lookupString != null &&
|| lookupString == null){
println outputMatch(prependString, file)

println "\nNumber of Files=" + count

1 comment:

Alexander said...

real hardcore. i beggin to not be a guy who supports your code.