Skip to content

Instantly share code, notes, and snippets.

@shuaibiyy
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save shuaibiyy/88527c3a50e57cde2492 to your computer and use it in GitHub Desktop.

Select an option

Save shuaibiyy/88527c3a50e57cde2492 to your computer and use it in GitHub Desktop.
import groovy.io.FileType
def editionProp = "edition"
def resourcesDir = "${basedir}/src/main/resources/"
def currentEdition = project.properties[editionProp]
File mappingsFile = new File("${resourcesDir}classMappings.properties")
Properties mappings = new Properties()
if (mappingsFile.exists()) {
mappingsFile.withInputStream { stream ->
mappings.load(stream)
}
} else {
println "WARNING: classMappings.properties not found! Skipping classMapper.groovy execution!"
return
}
if (mappings.getProperty(editionProp) == currentEdition) {
return
}
def modifiedFiles = []
def testFilesDir = new File("${basedir}/src/test")
/*
* For each test file
* For each property in the mappings file
* If a mapping key or value matches one found in the test file
* Replace the key with the value or vice-versa
*/
testFilesDir.eachFileRecurse (FileType.FILES) { testFile ->
def fileText = testFile.text
def modified = false
for (Iterator iterator = mappings.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next()
testFile.eachLine { line ->
if (key != editionProp) {
if (line.contains(key)) {
fileText = fileText.replace(key, mappings.getProperty(key))
modified = true
modifiedFiles << testFile
} else if (line.contains(mappings.getProperty(key))) {
fileText = fileText.replace(mappings.getProperty(key), key)
modified = true
modifiedFiles << testFile
}
}
}
}
if (modified) {
testFile.write(fileText)
}
}
if (!modifiedFiles.isEmpty()) {
mappings.setProperty(editionProp, currentEdition)
mappings.store(new FileOutputStream(mappingsFile), null)
}
println "INFO: The following files have been modified by classMapper.groovy:"
modifiedFiles.each {
println it.path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment