rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Mar 2013 13:28:33 +0100
branchclosure
changeset 881 6a3a063b6eb1
parent 772 d382dacfd73f
child 1020 a6bacea2518f
child 1058 e61f24684a69
permissions -rw-r--r--
One can specify --obfuscationlevel when invoking the bck2brwsr compiler from command line. Forking external Java when calling into the compiler to prevent "out of perm gem" errors.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.BufferedWriter;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import java.io.Writer;
    24 
    25 /** Generator of JavaScript from bytecode of classes on classpath of the VM
    26  * with a Main method.
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 final class Main {
    31     private Main() {}
    32     
    33     public static void main(String... args) throws IOException {
    34         final String obfuscate = "--obfuscatelevel";
    35         
    36         if (args.length < 2) {
    37             System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
    38             System.err.println("Usage: java -cp ... -jar ... [");
    39             System.err.print(obfuscate);
    40             System.err.print(" [");
    41             boolean first = true;
    42             for (ObfuscationLevel l : ObfuscationLevel.values()) {
    43                 if (!first) {
    44                     System.err.print('|');
    45                 }
    46                 System.err.print(l.name());
    47                 first = false;
    48             }
    49                 
    50             System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    51             System.exit(9);
    52         }
    53         
    54         ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
    55         StringArray classes = new StringArray();
    56         String generateTo = null;
    57         for (int i = 0; i < args.length; i++) {
    58             if (obfuscate.equals(args[i])) { // NOI18N
    59                 i++;
    60                 try {
    61                     obfLevel = ObfuscationLevel.valueOf(args[i]);
    62                 } catch (Exception e) {
    63                     System.err.print(obfuscate);
    64                     System.err.print(" parameter needs to be followed by one of ");
    65                     boolean first = true;
    66                     for (ObfuscationLevel l : ObfuscationLevel.values()) {
    67                         if (!first) {
    68                             System.err.print(", ");
    69                         }
    70                         System.err.print(l.name());
    71                         first = false;
    72                     }
    73                     System.err.println();
    74                     System.exit(1);
    75                 }
    76                 continue;
    77             }
    78             if (generateTo == null) {
    79                 generateTo = args[i];
    80             } else {
    81                 classes = classes.addAndNew(args[i]);
    82             }
    83         }
    84         try (Writer w = new BufferedWriter(new FileWriter(generateTo))) {
    85             Bck2Brwsr.newCompiler().
    86                 obfuscation(obfLevel).
    87                 addRootClasses(classes.toArray()).
    88                 resources(Main.class.getClassLoader()).
    89                 generate(w);
    90         }
    91     }
    92 }