Skip to content

Instantly share code, notes, and snippets.

@IvoLimmen
Last active March 21, 2016 06:29
Show Gist options
  • Save IvoLimmen/8d870d328ca7590fc438 to your computer and use it in GitHub Desktop.
Save IvoLimmen/8d870d328ca7590fc438 to your computer and use it in GitHub Desktop.
Needed a script that compared two (Java) property files
if (this.args.length != 2)
throw new IllegalArgumentException('Please pass two property files')
def base = new Properties()
def target = new Properties()
new File(".", this.args[0]).withInputStream {
stream -> base.load(stream)
}
new File(".", this.args[1]).withInputStream {
stream -> target.load(stream)
}
def baseSet = base.keySet()
def targetSet = target.keySet()
if (baseSet.size() == 0)
throw new IllegalArgumentException('Base property file is empty')
if (targetSet.size() == 0)
throw new IllegalArgumentException('Target property file is empty')
def newKeys = baseSet.minus(targetSet) as List
def oldKeys = targetSet.minus(baseSet) as List
if (newKeys.size() == 0 && oldKeys.size() == 0) {
println "Both files contain the same keys!"
return
}
leftWidth = (newKeys.plus(oldKeys).max { it.length() }).length()
rightWidth = 10
def printLine() {
print '+'
print '-' * (leftWidth + rightWidth + 5)
println '+'
}
println "Source: " + this.args[0] + " compared to " + this.args[1]
printLine()
newKeys.sort().each {
print '| '
print it.padRight(leftWidth)
print ' | '
print 'add'.padRight(rightWidth)
println ' |'
}
oldKeys.sort().each {
print '| '
print it.padRight(leftWidth)
print ' | '
print 'remove'.padRight(rightWidth)
println ' |'
}
printLine()
@IvoLimmen
Copy link
Author

Added a notification when both files contain the same keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment