29 April 2009

Comparing Code Folders, the groovy way

Ich habe mir vor einiger Zeit mal die Zeit genommen, ein wenig mit Groovy rumzuspielen, da ich glaube dass Groovy eine Prima Scriptingergänzung zu JAVA darstellt. Und heute hatte ich mal einen typischen Scripting Task aufm Schreibtisch:

Das Vergleichen zweier Verzeichnisse, in dem einen JAVA Code aus einer älteren Iteration und in dem anderen die neue Version. Mich haben nur die groben Zahlen (LoC, Files) interessiert und so sieht die Groovy-Lösung aus:

// Pattern for Java Files
def p = ~/.*\.java/
// Pathes to the directories containing the code
def newPath ='c:\\code\\20090427'
def oldPath ='c:\\code\\20090213'
// variables for counting
def newFilecount = 0
def oldFilecount = 0
def newLOCcount = 0
def oldLOCcount = 0

 
//define a closure for recursive directory access
// in the new code folder
def directoryClosNew
directoryClosNew = {
    it.eachDir(directoryClosNew);
    it.eachFileMatch(p){
        newFilecount++
        println "New source: $it"
        it.eachLine {
            newLOCcount++
        }
    }
}

//define a closure for recursive directory access
// in the old code folder
def directoryClosOld
directoryClosOld = {
    it.eachDir(directoryClosOld);
    it.eachFileMatch(p){
        oldFilecount++
        println "Old source: $it"
        it.eachLine {
            oldLOCcount++
        }
    }
}

// call closure on directories
directoryClosNew(new File( newPath ))
directoryClosOld(new File( oldPath ))
println " Old number of Java Files $oldFilecount \n New number of Java Files $newFilecount "
println " Old Lines of Code $oldLOCcount \n New Lines of Code $newLOCcount"

Schick, nicht? :-)

Labels: ,