a9efb30eeb3dd4a4079bd27dfb02ccaa4b9a118f
[wix.git] / src / HaskellHelper.java
1 // Copyright 2008 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.misc.*;
7 import edu.berkeley.sbp.util.*;
8 import edu.berkeley.sbp.meta.*;
9 import edu.berkeley.sbp.chr.*;
10 import java.io.*;
11
12 public class HaskellHelper {
13     public static boolean isNull(Object o) { return o==null; }
14
15     private static CharParser parser = null;
16     static {
17         synchronized(HaskellHelper.class) {
18             if (parser == null) {
19                 try {
20                     // FIXME: bundle this into the jarfile
21                     InputStream grammarFile = HaskellHelper.class.getClassLoader().getResourceAsStream("wix.g");
22                     Tree<String> res = new CharParser(GrammarAST.getMetaGrammar())
23                         .parse(grammarFile).expand1();
24                     Union grammar = GrammarAST.buildFromAST(res, "s", new GrammarAST.ImportResolver() {
25                             public InputStream getImportStream(String filename) {
26                                 return this.getClass().getClassLoader().getResourceAsStream(filename);
27                             }
28                         });
29                     parser = new CharParser(grammar);
30                 } catch (Exception e) {
31                     throw new RuntimeException(e);
32                 }
33             }
34         }
35     }
36
37     public static Tree help(String targetFile) throws Throwable {
38         Tree ret = null;
39         try {
40             Reader r = new InputStreamReader(new FileInputStream(targetFile));
41             Input input = new CharInput(new IndentingReader(r, CharAtom.left, CharAtom.right));
42             ret = parser.parse(input).expand1();
43         } catch (Throwable e) {
44             e.printStackTrace();
45             throw e;
46         }
47         if (ret==null) throw new NullPointerException("CharParser returned null");
48         return ret;
49     }
50
51     public static void main(String[] argv) throws Throwable {
52         if (argv.length != 2) {
53             System.out.println("usage: java -jar wix.jar [-v] <indir> <outdir>");
54             // FIXME: implement this
55             System.out.println("     | java -jar wix.jar [-v] <infile>.wix");
56             System.out.println("");
57             // FIXME: implement these
58             System.out.println("   -v   print text as it is parsed (sbp.verbose=true)");
59             System.out.println("   -vv  like -v, but also dump parse tree");
60             System.out.println("   -vvv like -vv, but also dump wix tree");
61             System.exit(-1);
62             return;
63         }
64         File indir = new File(argv[0]);
65         File outdir = new File(argv[1]);
66         if (!indir.isDirectory()) {
67             process(new File(indir.getParent()), indir.getName(), outdir);
68         } else {
69             process(indir, "", outdir);
70         }
71     }
72
73     private static void process(File indir, String suffix, File outdir) throws Throwable {
74         File f = new File(indir.getAbsolutePath()+File.separatorChar+suffix);
75         //System.out.println(f+" "+indir + " " + suffix + " " + outdir);
76         if (!f.exists()) return;
77         if (f.isDirectory()) {
78             for (String s : f.list())
79                 process(indir, suffix + File.separatorChar + s, outdir);
80             return;
81         }
82         if (!f.getPath().endsWith(".wix")) {
83             boolean skip = false;
84             if (f.getName().equals(".DS_Store")) skip = true;
85             if (f.getName().endsWith("-")) skip = true;
86             if (!skip) {
87                 File dest = new File(outdir.getAbsolutePath()+File.separatorChar+suffix);
88                 if (dest.exists() && dest.lastModified()==f.lastModified() && dest.length()==f.length()) {
89                     System.out.println(ANSI.yellow("no change: "+f.getPath()));
90                     return;
91                 }
92                 System.out.println(ANSI.green("copying: "+f.getPath()));
93                 File dest_ = new File(outdir.getAbsolutePath()+File.separatorChar+suffix+"-");
94                 FileOutputStream fos = new FileOutputStream(dest_);
95                 FileInputStream fis = new FileInputStream(f);
96                 byte[] buf = new byte[1024];
97                 while(true) {
98                     int numread = fis.read(buf, 0, buf.length);
99                     if (numread==-1) break;
100                     fos.write(buf, 0, numread);
101                 }
102                 fos.close();
103                 fis.close();
104                 dest_.renameTo(dest);
105                 dest.setLastModified(f.lastModified());
106             }
107         } else {
108             String out = "== " + suffix + " ";
109             while(out.length() < 75) out+="=";
110             System.out.println(ANSI.yellow(out));
111             //System.out.println();
112             String outPath = outdir.getAbsolutePath()+File.separatorChar+suffix;
113             outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
114             if (new File(outPath).exists() && new File(outPath).lastModified() > f.lastModified()) return;
115             Class.forName("Main").
116                 getMethod("main", new Class[] { String[].class }).
117                 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
118             new File(new File(outPath).getParent()).mkdirs();
119             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
120             pw.println(ret);
121             pw.flush();
122             pw.close();
123             File dest = new File(outPath);
124             if (dest.exists()) {
125                 try {
126                     Process p = Runtime.getRuntime().exec(new String[] {
127                             "diff",
128                             "-Bub",
129                             dest.getAbsolutePath(),
130                             new File(outPath+"+").getAbsolutePath()
131                         });
132                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
133                     br.readLine();
134                     br.readLine();
135                     for(String s = br.readLine(); s != null; s = br.readLine()) {
136                         if      (s.startsWith("+")) System.out.println(ANSI.green(s));
137                         else if (s.startsWith("-")) System.out.println(ANSI.red(s));
138                         /*else System.out.println(ANSI.blue(s));*/
139                     }
140                     p.waitFor();
141                 } catch (Exception e) {
142                     e.printStackTrace();
143                 }
144             }
145             new File(outPath+"+").renameTo(dest);
146             if (dest.lastModified() <= f.lastModified())
147                 dest.setLastModified(f.lastModified()+1);
148         }
149     }
150
151     public static Object ret;
152     public static void putBack(String o) { ret = o; }
153 }