Created
December 7, 2015 15:40
-
-
Save SijmenHuizenga/a6024e26051dbbd95fd4 to your computer and use it in GitHub Desktop.
SQL Compiler
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
package nl.sijmenhuizenga.sqlcompiler; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
import java.util.function.Consumer; | |
import java.util.stream.Collectors; | |
/** | |
* Created by sijmen on 7-12-2015. | |
*/ | |
public class SqlCompiler { | |
private static Map<String, String> map = new HashMap<>(); | |
private static ArrayList<String> ignores = new ArrayList<>(); | |
private static String output = ""; | |
private static void makeMap(String[] args) { | |
for (String arg : args) { | |
if (arg.contains("=")) { | |
map.put(arg.substring(0, arg.indexOf('=')), | |
arg.substring(arg.indexOf('=') + 1)); | |
} | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
makeMap(args); | |
String base = map.containsKey("base") ? map.get("base") : ""; | |
if(map.containsKey("ignore")) { | |
ignores = new ArrayList<String>(Arrays.asList(map.get("ignore").split(";"))); | |
for(String ingore : ignores){ | |
System.out.println("Ignoring: \t" + ingore); | |
} | |
} | |
Files.walkFileTree(Paths.get(base), new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
if (!validateFile(file)) { | |
System.out.println("Excluding: \t" + file); | |
return super.visitFile(file, attrs); | |
} | |
System.out.println("Including: \t" + file); | |
List<String> lines = Files.readAllLines(file, Charset.defaultCharset()); | |
lines.removeIf(s -> s.length() == 0); | |
output += "--****************************************************************************--\n"; | |
output += "-- " + file + "\n"; | |
output += "--****************************************************************************--\n"; | |
output += String.join("\n", lines); | |
output += "\ngo\n"; | |
return super.visitFile(file, attrs); | |
} | |
}); | |
DateFormat df = new SimpleDateFormat("yyyy-mm-dd HHmmss"); | |
Date today = Calendar.getInstance().getTime(); | |
String outputName = map.containsKey("output") ? map.get("output") : "bin/concat - "+ df.format(today)+".min.sql"; | |
System.out.println("Saving to \t" + outputName); | |
Files.write(Paths.get(outputName), output.getBytes()); | |
} | |
private static boolean validateFile(Path file) { | |
if (file.getFileName().toString().endsWith(".min.sql")) | |
return false; | |
if (ignores.contains(file.getFileName().toString())) | |
return false; | |
if (file.getFileName().toString().startsWith(".")) | |
return false; | |
if (!file.getFileName().toString().toLowerCase().endsWith(".sql")) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment