Skip to content

Instantly share code, notes, and snippets.

@yackx
Created May 7, 2014 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yackx/a9bf4e4e639e68907c14 to your computer and use it in GitHub Desktop.
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
#!/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