Skip to content

Instantly share code, notes, and snippets.

@SijmenHuizenga
Created July 18, 2016 08:20
Show Gist options
  • Save SijmenHuizenga/5d1c1c3fd4bc46187caedfecc680b5e4 to your computer and use it in GitHub Desktop.
Save SijmenHuizenga/5d1c1c3fd4bc46187caedfecc680b5e4 to your computer and use it in GitHub Desktop.
A little application that concats SQL files into one single file.
package nl.sijmenhuizenga.sqlcompiler;
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.*;
/**
* 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<>(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);
}
});
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
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;
for(String ignore : ignores)
if(file.toAbsolutePath().toString().toLowerCase().contains(ignore.toLowerCase()))
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