Created
May 7, 2014 14:06
-
-
Save yackx/a9bf4e4e639e68907c14 to your computer and use it in GitHub Desktop.
Remove trailing spaces inserted by Hippo CMS at the end of some .xml files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
/* | |
* Remove nasty trailing spaces inserted by the naughty Hippo | |
* at the end of some .xml files. | |
*/ | |
@Grab('org.apache.commons:commons-lang3:3.3.1') | |
import org.apache.commons.lang3.StringUtils | |
import static groovy.io.FileType.FILES | |
def removeTrailingSpaces(String text) { | |
def newText = new StringBuffer() | |
boolean modified = false | |
text.eachLine { line -> | |
def stripped = StringUtils.stripEnd(line, ' ') | |
if (stripped != line) { | |
modified = true | |
} | |
newText.append stripped | |
newText.append "\n" | |
} | |
return [modified, newText.toString()] | |
} | |
boolean removeTrailingSpacesFromFile(File file) { | |
def (isModified, newContent) = removeTrailingSpaces(file.text) | |
if (isModified) { | |
println "Removed trailing spaces from ${file.getPath()}" | |
file.write newContent | |
} | |
return isModified | |
} | |
def modifiedCount = 0 | |
def base = '../../../..' | |
File dir = new File(base) | |
println "Base source directory: ${dir.getCanonicalPath()}" | |
dir.traverse(type: FILES, nameFilter: ~/hippo.*\.xml$/) { file -> | |
println "Processing ${file.getPath()}" | |
if (removeTrailingSpacesFromFile(file)) { | |
modifiedCount++ | |
} | |
} | |
println "Modified files: $modifiedCount - certified on ${new Date()}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment