copy any files found in the source directory which are not wix files
[wix.git] / src / HaskellHelper.java
index 3ca1cae..a9efb30 100644 (file)
@@ -1,4 +1,6 @@
-// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+// Copyright 2008 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.*;
@@ -16,11 +18,13 @@ public class HaskellHelper {
             if (parser == null) {
                 try {
                     // FIXME: bundle this into the jarfile
-                    File grammarFile = new File("src/wix.g");
-                    Tree<String> res = new CharParser(MetaGrammar.newInstance())
-                        .parse(new FileInputStream(grammarFile)).expand1();
-                    Union grammar = GrammarAST.buildFromAST(res, "s", new File[] {
-                            new File(grammarFile.getParent())
+                    InputStream grammarFile = HaskellHelper.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) {
@@ -59,27 +63,58 @@ public class HaskellHelper {
         }
         File indir = new File(argv[0]);
         File outdir = new File(argv[1]);
-        process(indir, "", outdir);
+        if (!indir.isDirectory()) {
+            process(new File(indir.getParent()), indir.getName(), outdir);
+        } else {
+            process(indir, "", outdir);
+        }
     }
 
     private static void process(File indir, String suffix, File outdir) throws Throwable {
-        File f = new File(indir.getAbsolutePath()+suffix);
+        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);
             return;
         }
-        if (f.getPath().endsWith(".wix")) {
-            System.out.println();
+        if (!f.getPath().endsWith(".wix")) {
+            boolean skip = false;
+            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+"-");
+                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;
             Class.forName("Main").
                 getMethod("main", new Class[] { String[].class }).
                 invoke(null, new Object[] { new String[] { f.getAbsolutePath() } });
-            String outPath = outdir.getAbsolutePath()+suffix;
-            outPath = outPath.substring(0, outPath.length()-".wix".length())+".html";
             new File(new File(outPath).getParent()).mkdirs();
             PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outPath+"+")));
             pw.println(ret);
@@ -108,6 +143,8 @@ public class HaskellHelper {
                 }
             }
             new File(outPath+"+").renameTo(dest);
+            if (dest.lastModified() <= f.lastModified())
+                dest.setLastModified(f.lastModified()+1);
         }
     }