Created
July 21, 2009 08:20
-
-
Save pascaldevink/151201 to your computer and use it in GitHub Desktop.
This file contains 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
if (!args) { | |
println "Usage: <path_to_directory>" | |
println "And: <existance of extensions.txt comma separated values file with file extensions to convert>" | |
return | |
} | |
Convert c = new Convert() | |
c.convert(args[0]) | |
class Convert { | |
Set extensions = new HashSet() | |
public void convert(String path) { | |
extensions() | |
println "extensions to convert: ${extensions}" | |
File f = new File(path) | |
convertDir(f) | |
} | |
private void convertDir(File f) { | |
def sum = 0 | |
def filesFound = 0 | |
def filesVisited = 0 | |
f.eachFileRecurse{File file -> | |
filesVisited = filesVisited + 1 | |
if (shouldConvert(file)) { | |
filesFound = filesFound + 1 | |
if (filesFound % 100 == 0) { | |
println "Files checked: ${filesFound}" | |
} | |
if (replaceLines(file)) { | |
sum = sum + 1 | |
if (sum % 10 == 0) | |
println "Replaced eol in files: " + sum | |
} | |
} | |
} | |
println "Files converted: ${sum}" | |
println "Files checked: ${filesFound}" | |
println "Files visited: ${filesVisited}" | |
} | |
private boolean shouldConvert(File f) { | |
return extensions.contains(extension(f)) | |
} | |
private String extension(File f) { | |
int idx = f.getName().lastIndexOf('.') | |
if (idx != -1) { | |
String result = f.getName().substring(idx + 1) | |
return result | |
} else { | |
return null | |
} | |
} | |
def replaceLines = {File f -> | |
String text = f.text | |
if (text.contains('\r\n') || text.contains('\r')) { | |
text = text.replaceAll('\r\n', '\n') | |
text = text.replaceAll('\r', '\n') | |
f.write(text) | |
return true | |
} | |
return false | |
} | |
private void extensions() { | |
File f = new File("extensions.txt") | |
String content = f.text | |
String[] str = content.split(",") | |
Set extensions = new HashSet(Arrays.asList(str)) | |
extensions.each{ | |
it = it.replaceAll('\r\n','') | |
this.extensions.add(it) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment