rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 29 Apr 2013 14:39:27 +0200
branchmodel
changeset 1058 e61f24684a69
parent 881 6a3a063b6eb1
child 1146 e499b0dddd12
child 1359 5d93ca1561c3
permissions -rw-r--r--
Speedup the compilation by skipping it if the target bck2brwsr.js exists
     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.File;
    22 import java.io.FileWriter;
    23 import java.io.IOException;
    24 import java.io.Writer;
    25 
    26 /** Generator of JavaScript from bytecode of classes on classpath of the VM
    27  * with a Main method.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 final class Main {
    32     private Main() {}
    33     
    34     public static void main(String... args) throws IOException {
    35         final String obfuscate = "--obfuscatelevel";
    36         
    37         if (args.length < 2) {
    38             System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
    39             System.err.println("Usage: java -cp ... -jar ... [");
    40             System.err.print(obfuscate);
    41             System.err.print(" [");
    42             boolean first = true;
    43             for (ObfuscationLevel l : ObfuscationLevel.values()) {
    44                 if (!first) {
    45                     System.err.print('|');
    46                 }
    47                 System.err.print(l.name());
    48                 first = false;
    49             }
    50                 
    51             System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    52             System.exit(9);
    53         }
    54         
    55         ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
    56         StringArray classes = new StringArray();
    57         String generateTo = null;
    58         for (int i = 0; i < args.length; i++) {
    59             if (obfuscate.equals(args[i])) { // NOI18N
    60                 i++;
    61                 try {
    62                     obfLevel = ObfuscationLevel.valueOf(args[i]);
    63                 } catch (Exception e) {
    64                     System.err.print(obfuscate);
    65                     System.err.print(" parameter needs to be followed by one of ");
    66                     boolean first = true;
    67                     for (ObfuscationLevel l : ObfuscationLevel.values()) {
    68                         if (!first) {
    69                             System.err.print(", ");
    70                         }
    71                         System.err.print(l.name());
    72                         first = false;
    73                     }
    74                     System.err.println();
    75                     System.exit(1);
    76                 }
    77                 continue;
    78             }
    79             if (generateTo == null) {
    80                 generateTo = args[i];
    81             } else {
    82                 classes = classes.addAndNew(args[i]);
    83             }
    84         }
    85         
    86         File gt = new File(generateTo);
    87         if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) {
    88             System.err.println("Skipping as " + gt + " exists.");
    89             System.exit(0);
    90         }
    91         
    92         try (Writer w = new BufferedWriter(new FileWriter(gt))) {
    93             Bck2Brwsr.newCompiler().
    94                 obfuscation(obfLevel).
    95                 addRootClasses(classes.toArray()).
    96                 resources(Main.class.getClassLoader()).
    97                 generate(w);
    98         }
    99     }
   100 }