Skip to content

Instantly share code, notes, and snippets.

@nshaw
Created September 5, 2012 15:31
Show Gist options
  • Save nshaw/3638497 to your computer and use it in GitHub Desktop.
Save nshaw/3638497 to your computer and use it in GitHub Desktop.
Patch tool - detect code conflicts
import java.util.zip.ZipFile
/*
* detect-patch-conflicts.groovy
* Input - patches.dir property to indicate the location of the patches zip files
* Output - detected.patch.conflict property - set to true if conflicts were found
*/
def conflict = false
// CUSTOM START - this configuraton should be paramaterized to generalize this script
def pluginRoot = "liferay-plugin"
def extRoot = pluginRoot + "/ext/sesame-ext/docroot/WEB-INF"
def paths = ["src/portal-impl":extRoot + "/ext-impl",
"src/portal-service":extRoot + "/ext-service",
//TODO: these mappings should be generalized
"src/portlets/web-form-portlet":pluginRoot + "/portlets/web-form-portlet",
"src/webs/wurfl-web":pluginRoot + "/webs/wurfl-web"]
// There may be some paths which will be manually merged due to existing customizations. Note those
// as conflicts in the logs but don't fail the build
def pathsToIgnore = []
//CUSTOM END
def patchesDir = properties["patches.dir"]
println "Checking for patches in " + patchesDir
def zipFiles = new File((String) patchesDir).listFiles().findAll {it =~ ".*zip"}
println zipFiles
zipFiles.each { it ->
println "----"
println "Processing " + it
ZipFile file = new ZipFile((String) it);
file.entries().each { entry ->
def path = (String) entry
if (path.startsWith("src") && !path.endsWith("/")) {
println path
def check = checkZipResource(path, paths);
if (check) {
def reportConflict = true
pathsToIgnore.each { ignore ->
// Might want to make
if (path.contains(ignore)) {
println "- Ignoring conflict with " + path
reportConflict = false
}
}
if (reportConflict) {
conflict = true
}
}
}
}
}
if (conflict) {
properties["detected.patch.conflict"] = "true"
}
def checkZipResource(zipPath, paths) {
def resourceConflict = false
def checked = false
paths.each { i ->
def key = i.key
def path = i.value
if (zipPath.startsWith(key)) {
checked = true
def check = detectConflictByPath(key, path, zipPath)
if (check) {
resourceConflict = true
}
}
}
if (!checked) {
println "- POSSIBLE CONFLICT. Unable to find mapping for zipPath " + zipPath
resourceConflict = true
}
return resourceConflict
}
def detectConflictByPath(key, pathRoot, name) {
if (!name.startsWith(key)) {
return
}
def index = key.size() + 1
name = name.substring(index)
def path = pathRoot + "/" + name;
File theFile = new File(path)
def conflict = theFile.exists();
if (conflict) {
println "- CONFLICT found with " + path
}
else {
println "- no conflict found at " + path
}
return conflict
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment