--- /dev/null
+// Copyright 2011 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.meta.*;
+import edu.berkeley.sbp.chr.*;
+import java.io.*;
+
+public class ScalaHelper {
+ public static boolean isNull(Object o) { return o==null; }
+
+ private static CharParser parser = null;
+ static {
+ synchronized(ScalaHelper.class) {
+ if (parser == null) {
+ try {
+ // FIXME: bundle this into the jarfile
+ InputStream grammarFile = ScalaHelper.class.getClassLoader().getResourceAsStream("wix.g");
+ Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
+ .parse(grammarFile).expand1();
+ Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
+ public InputStream getImportStream(String filename) {
+ return this.getClass().getClassLoader().getResourceAsStream(filename);
+ }
+ });
+ parser = new CharParser(grammar);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ }
+
+ public static Tree parseFile(String targetFile) throws Throwable {
+ Tree ret = null;
+ try {
+ Reader r = new InputStreamReader(new FileInputStream(targetFile));
+ Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
+ ret = parser.parse(input).expand1();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ throw e;
+ }
+ if (ret==null) throw new NullPointerException("CharParser returned null");
+ return ret;
+ }
+
+ public static void main(String[] argv, TreeToString t2s) throws Throwable {
+ if (argv.length != 2) {
+ System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
+ // FIXME: implement this
+ System.out.println(" | java -jar wix.jar [-v] <infile>.wix");
+ System.out.println("");
+ // FIXME: implement these
+ System.out.println(" -v print text as it is parsed (sbp.verbose=true)");
+ System.out.println(" -vv like -v, but also dump parse tree");
+ System.out.println(" -vvv like -vv, but also dump wix tree");
+ System.exit(-1);
+ return;
+ }
+ File indir = new File(argv[0]);
+ File outdir = new File(argv[1]);
+ if (!indir.isDirectory()) {
+ process(new File(indir.getParent()), indir.getName(), outdir, t2s);
+ } else {
+ process(indir, "", outdir, t2s);
+ }
+ }
+
+ private static void process(File indir, String suffix, File outdir, TreeToString t2s) throws Throwable {
+ File f = new File(indir.getAbsolutePath()+File.separatorChar+suffix);
+ //System.out.println(f+" "+indir + " " + suffix + " " + outdir);
+ if (!f.exists()) return;
+ if (f.isDirectory()) {
+ for (String s : f.list())
+ process(indir, suffix + File.separatorChar + s, outdir, t2s);
+ return;
+ }
+ if (!f.getPath().endsWith(".wix")) {
+ boolean skip = false;
+ if (f.getName().equals(".DS_Store")) skip = true;
+ if (f.getName().equals("._.DS_Store")) skip = true;
+ if (f.getName().endsWith("-")) skip = true;
+ if (!skip) {
+ File dest = new File(outdir.getAbsolutePath()+File.separatorChar+suffix);
+ if (dest.exists() && dest.lastModified()==f.lastModified() && dest.length()==f.length()) {
+ System.out.println(ANSI.yellow("no change: "+f.getPath()));
+ return;
+ }
+ System.out.println(ANSI.green("copying: "+f.getPath()));
+ File dest_ = new File(outdir.getAbsolutePath()+File.separatorChar+suffix+"-");
+ new File(dest_.getParent()).mkdirs();
+ FileOutputStream fos = new FileOutputStream(dest_);
+ FileInputStream fis = new FileInputStream(f);
+ byte[] buf = new byte[1024];
+ while(true) {
+ int numread = fis.read(buf, 0, buf.length);
+ if (numread==-1) break;
+ fos.write(buf, 0, numread);
+ }
+ fos.close();
+ fis.close();
+ dest_.renameTo(dest);
+ dest.setLastModified(f.lastModified());
+ }
+ } else {
+ String out = "== " + suffix + " ";
+ while(out.length() < 75) out+="=";
+ System.out.println(ANSI.yellow(out));
+ //System.out.println();
+ String outPath = outdir.getAbsolutePath()+File.separatorChar+suffix;
+ outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
+ if (new File(outPath).exists() && new File(outPath).lastModified() > f.lastModified()) return;
+
+ Tree tree = parseFile(f.getAbsolutePath());
+ String ret = t2s.run(tree);
+ try {
+ new File(new File(outPath).getParent()).mkdirs();
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
+ pw.println(ret);
+ pw.flush();
+ pw.close();
+ File dest = new File(outPath);
+ if (dest.exists()) {
+ Process p = Runtime.getRuntime().exec(new String[] {
+ "diff",
+ "-Bub",
+ dest.getAbsolutePath(),
+ new File(outPath+"+").getAbsolutePath()
+ });
+ BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+ br.readLine();
+ br.readLine();
+ for(String s = br.readLine(); s != null; s = br.readLine()) {
+ if (s.startsWith("+")) System.out.println(ANSI.green(s));
+ else if (s.startsWith("-")) System.out.println(ANSI.red(s));
+ /*else System.out.println(ANSI.blue(s));*/
+ }
+ p.waitFor();
+ }
+ new File(outPath+"+").renameTo(dest);
+ if (dest.lastModified() <= f.lastModified())
+ dest.setLastModified(f.lastModified()+1);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public static Object ret;
+ public static void putBack(String o) { ret = o; }
+}