Wednesday, September 22, 2010

Applying a License into Source Code

Below is a script to apply a license into source code.

ApplyLicense.groovy
validateArgs(this.args)

def projectDir = new File(this.args[0])
def licenseText = new File(this.args[1]).text
def fileExtension = this.args[2]

projectDir.eachFileRecurse { file ->
    if (file.name.endsWith(fileExtension)) {
        def tmpFile = new File(file.path + ".tmp")
        tmpFile.text = licenseText + file.text
        tmpFile.renameTo(file) 
    }
}

def validateArgs(def args) {
    if (args.size() < 3) {
        printUsage()
        System.exit(1)
    }
    if (!new File(args[0]).isDirectory()) {
        printUsage()
        System.exit(1)
    }
    if (!new File(args[1]).isFile()) {
        printUsage()
        System.exit(1)
    }
}

def printUsage() {
    println "Usage: groovy ApplyLicense.groovy <project_dir> <license_file> <file_extension>"
}

No comments:

Post a Comment