add support for processing directories, checking modified-times
[wix.git] / src / HaskellHelper.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.misc.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.meta.*;
7 import edu.berkeley.sbp.chr.*;
8 import java.io.*;
9
10 public class HaskellHelper {
11     public static boolean isNull(Object o) { return o==null; }
12
13     private static CharParser parser = null;
14     static {
15         synchronized(HaskellHelper.class) {
16             if (parser == null) {
17                 try {
18                     // FIXME: bundle this into the jarfile
19                     InputStream grammarFile = HaskellHelper.class.getClassLoader().getResourceAsStream("wix.g");
20                     Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
21                         .parse(grammarFile).expand1();
22                     Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
23                             public InputStream getImportStream(String filename) {
24                                 return this.getClass().getClassLoader().getResourceAsStream(filename);
25                             }
26                         });
27                     parser = new CharParser(grammar);
28                 } catch (Exception e) {
29                     throw new RuntimeException(e);
30                 }
31             }
32         }
33     }
34
35     public static Tree help(String targetFile) throws Throwable {
36         Tree ret = null;
37         try {
38             Reader r = new InputStreamReader(new FileInputStream(targetFile));
39             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
40             ret = parser.parse(input).expand1();
41         } catch (Throwable e) {
42             e.printStackTrace();
43             throw e;
44         }
45         if (ret==null) throw new NullPointerException("CharParser returned null");
46         return ret;
47     }
48
49     public static void main(String[] argv) throws Throwable {
50         if (argv.length != 2) {
51             System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
52             // FIXME: implement this
53             System.out.println("     | java -jar wix.jar [-v] <infile>.wix");
54             System.out.println("");
55             // FIXME: implement these
56             System.out.println("   -v   print text as it is parsed (sbp.verbose=true)");
57             System.out.println("   -vv  like -v, but also dump parse tree");
58             System.out.println("   -vvv like -vv, but also dump wix tree");
59             System.exit(-1);
60             return;
61         }
62         File indir = new File(argv[0]);
63         File outdir = new File(argv[1]);
64         if (!indir.isDirectory()) {
65             process(new File(indir.getParent()), indir.getName(), outdir);
66         } else {
67             process(indir, "", outdir);
68         }
69     }
70
71     private static void process(File indir, String suffix, File outdir) throws Throwable {
72         File f = new File(indir.getAbsolutePath()+File.separatorChar+suffix);
73         //System.out.println(f+" "+indir + " " + suffix + " " + outdir);
74         if (!f.exists()) return;
75         if (f.isDirectory()) {
76             for (String s : f.list())
77                 process(indir, suffix + File.separatorChar + s, outdir);
78             return;
79         }
80         if (f.getPath().endsWith(".wix")) {
81             String out = "== " + suffix + " ";
82             while(out.length() < 75) out+="=";
83             System.out.println(ANSI.yellow(out));
84             //System.out.println();
85             String outPath = outdir.getAbsolutePath()+File.separatorChar+suffix;
86             outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
87             if (new File(outPath).exists() && new File(outPath).lastModified() > f.lastModified()) return;
88             Class.forName("Main").
89                 getMethod("main", new Class[] { String[].class }).
90                 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
91             new File(new File(outPath).getParent()).mkdirs();
92             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
93             pw.println(ret);
94             pw.flush();
95             pw.close();
96             File dest = new File(outPath);
97             if (dest.exists()) {
98                 try {
99                     Process p = Runtime.getRuntime().exec(new String[] {
100                             "diff",
101                             "-Bub",
102                             dest.getAbsolutePath(),
103                             new File(outPath+"+").getAbsolutePath()
104                         });
105                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
106                     br.readLine();
107                     br.readLine();
108                     for(String s = br.readLine(); s != null; s = br.readLine()) {
109                         if      (s.startsWith("+")) System.out.println(ANSI.green(s));
110                         else if (s.startsWith("-")) System.out.println(ANSI.red(s));
111                         /*else System.out.println(ANSI.blue(s));*/
112                     }
113                     p.waitFor();
114                 } catch (Exception e) {
115                     e.printStackTrace();
116                 }
117             }
118             new File(outPath+"+").renameTo(dest);
119             if (dest.lastModified() <= f.lastModified())
120                 dest.setLastModified(f.lastModified()+1);
121         }
122     }
123
124     public static Object ret;
125     public static void putBack(String o) { ret = o; }
126 }