Skip to content

Instantly share code, notes, and snippets.

@yackx
Created May 7, 2014 14:06
Show Gist options
  • Select an option

  • Save yackx/a9bf4e4e639e68907c14 to your computer and use it in GitHub Desktop.

Select an option

Save yackx/a9bf4e4e639e68907c14 to your computer and use it in GitHub Desktop.

Revisions

  1. yackx created this gist May 7, 2014.
    48 changes: 48 additions & 0 deletions gistfile1.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/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()}"