package lu.technolink.vaadin.theme.precompiler; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class VaadinThemePreCompiler { /** * @param args * * Prepare the Vaadin themes by changing the url paths in order to make them work with the SASS/Compass compiler * */ private static final String[] VAADIN_THEMES = new String[] { "base", "chameleon", "liferay", "reindeer", "runo" }; private static final String PATTERN = "(.+) url\\(([\\w/\\.-]+)\\)(.+)"; public static void main(String[] args) { VaadinThemePreCompiler compiler = new VaadinThemePreCompiler(); compiler.compile(); } public void compile() { for (String theme : VaadinThemePreCompiler.VAADIN_THEMES) { System.out.println("Pre-compiling the theme --- " + theme + " ---"); compileTheme("VAADIN" + File.separator + theme); } System.out.println("Done."); } private void compileTheme(final String theme) { File themeRoot = new File(theme); if (themeRoot != null && themeRoot.isDirectory()) { processDirectory(themeRoot); } } private void processDirectory(final File directory) { System.out.println("\tProcessing directory --- " + directory.getName() + "---"); File[] listFiles = directory.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(".scss"); } }); for (File file : listFiles) { if (file.isDirectory()) { processDirectory(file); } else { processFile(file); } } } private void processFile(final File file) { System.out.println("\t\tProcessing file --- " + file.getName() + "---"); StringBuffer buffer = new StringBuffer(); String currentLine; Pattern pattern = Pattern.compile(VaadinThemePreCompiler.PATTERN); try { BufferedReader br = new BufferedReader(new FileReader(file)); while ((currentLine = br.readLine()) != null) { Matcher matcher = pattern.matcher(currentLine); if (matcher.matches()) { String parent = file.getParent().replaceFirst("VAADIN", ".."); String path = matcher.group(2); while (path.startsWith("..")) { path = path.substring(path.indexOf("/") + 1); parent = parent.substring(0, parent.lastIndexOf("/")); } String newPath = "url(" + parent + "/" + path + ")"; currentLine = matcher.replaceAll("$1 " + newPath + "$3"); } buffer.append(currentLine).append("\n"); } br.close(); BufferedWriter bw = new BufferedWriter(new FileWriter(file, false)); bw.append(buffer); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }