KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > JRubyC


1 /*
2  * JRubyC.java
3  *
4  * Created on January 11, 2007, 11:24 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.jruby;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import org.jruby.ast.Node;
16 import org.jruby.compiler.NodeCompilerFactory;
17 import org.jruby.compiler.NotCompilableException;
18 import org.jruby.compiler.impl.StandardASMCompiler;
19
20 /**
21  *
22  * @author headius
23  */

24 public class JRubyC {
25     
26     public static void main(String JavaDoc args[]) {
27         Ruby runtime = Ruby.getDefaultInstance();
28         
29         try {
30             if (args.length < 1 || args.length > 2) {
31                 System.out.println("Usage: jrubyc <filename> [<dest>]");
32                 return;
33             }
34             String JavaDoc filename = args[0];
35             File JavaDoc srcfile = new File JavaDoc(filename);
36             if (!srcfile.exists()) {
37                 System.err.println("Error -- file not found: " + filename);
38                 return;
39             }
40             File JavaDoc destfile = new File JavaDoc(System.getProperty("user.dir"));
41             if (args.length == 2) {
42                 String JavaDoc dirname = args[1];
43                 destfile = new File JavaDoc(dirname);
44                 if (!destfile.exists()) {
45                     System.err.println("Error -- destination not found: " + dirname);
46                     return;
47                 }
48                 if (!destfile.isDirectory()) {
49                     System.err.println("Error -- not a directory: " + dirname);
50                 }
51             }
52
53             int size = (int)srcfile.length();
54             byte[] chars = new byte[size];
55             new FileInputStream JavaDoc(srcfile).read(chars);
56             // FIXME: encoding?
57
String JavaDoc content = new String JavaDoc(chars);
58             Node scriptNode = runtime.parse(content, filename, null);
59             
60             // do the compile
61
StandardASMCompiler compiler = new StandardASMCompiler(filename.substring(0, filename.lastIndexOf(".")), filename);
62             NodeCompilerFactory.getCompiler(scriptNode).compile(scriptNode, compiler);
63             
64             compiler.writeClass(destfile);
65         } catch (IOException JavaDoc ioe) {
66             System.err.println("Error -- IO exception during compile: " + ioe.getMessage());
67         } catch (NotCompilableException nce) {
68             System.err.println("Error -- Not compileable: " + nce.getMessage());
69         }
70     }
71     
72 }
73
Popular Tags