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. closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Mar 2013 13:28:33 +0100
branchclosure
changeset 8816a3a063b6eb1
parent 880 32eb44c74e1e
child 882 60d9ea48ec99
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.
rt/vm/pom.xml
rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java
     1.1 --- a/rt/vm/pom.xml	Mon Mar 25 12:47:25 2013 +0100
     1.2 +++ b/rt/vm/pom.xml	Mon Mar 25 13:28:33 2013 +0100
     1.3 @@ -84,14 +84,19 @@
     1.4                       <id>generate-js</id>
     1.5                       <phase>process-classes</phase>
     1.6                       <configuration>
     1.7 -                        <mainClass>org.apidesign.vm4brwsr.Main</mainClass>
     1.8 +                         <executable>java</executable>
     1.9                          <arguments>
    1.10 +                            <argument>-cp</argument>
    1.11 +                            <classpath/>
    1.12 +                            <argument>org.apidesign.vm4brwsr.Main</argument>
    1.13 +                            <argument>--obfuscatelevel</argument>
    1.14 +                            <argument>MINIMAL</argument>
    1.15                              <argument>${project.build.directory}/bck2brwsr.js</argument>
    1.16                              <argument>org/apidesign/vm4brwsr/Bck2Brwsr</argument>
    1.17                          </arguments>
    1.18                       </configuration>
    1.19                       <goals>
    1.20 -                         <goal>java</goal>
    1.21 +                         <goal>exec</goal>
    1.22                       </goals>
    1.23                   </execution>
    1.24               </executions>
     2.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java	Mon Mar 25 12:47:25 2013 +0100
     2.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java	Mon Mar 25 13:28:33 2013 +0100
     2.3 @@ -31,20 +31,62 @@
     2.4      private Main() {}
     2.5      
     2.6      public static void main(String... args) throws IOException {
     2.7 +        final String obfuscate = "--obfuscatelevel";
     2.8 +        
     2.9          if (args.length < 2) {
    2.10              System.err.println("Bck2Brwsr Translator from Java(tm) to JavaScript, (c) Jaroslav Tulach 2012");
    2.11 -            System.err.println("Usage: java -cp ... -jar ... <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    2.12 -            return;
    2.13 +            System.err.println("Usage: java -cp ... -jar ... [");
    2.14 +            System.err.print(obfuscate);
    2.15 +            System.err.print(" [");
    2.16 +            boolean first = true;
    2.17 +            for (ObfuscationLevel l : ObfuscationLevel.values()) {
    2.18 +                if (!first) {
    2.19 +                    System.err.print('|');
    2.20 +                }
    2.21 +                System.err.print(l.name());
    2.22 +                first = false;
    2.23 +            }
    2.24 +                
    2.25 +            System.err.println("] <file_to_generate_js_code_to> java/lang/Class org/your/App ...");
    2.26 +            System.exit(9);
    2.27          }
    2.28          
    2.29 -        Writer w = new BufferedWriter(new FileWriter(args[0]));
    2.30 -        StringArray classes = StringArray.asList(args);
    2.31 -        classes.delete(0);
    2.32 -        try {
    2.33 -            Bck2Brwsr.generate(w, Main.class.getClassLoader(),
    2.34 -                               classes.toArray());
    2.35 -        } finally {
    2.36 -            w.close();
    2.37 +        ObfuscationLevel obfLevel = ObfuscationLevel.NONE;
    2.38 +        StringArray classes = new StringArray();
    2.39 +        String generateTo = null;
    2.40 +        for (int i = 0; i < args.length; i++) {
    2.41 +            if (obfuscate.equals(args[i])) { // NOI18N
    2.42 +                i++;
    2.43 +                try {
    2.44 +                    obfLevel = ObfuscationLevel.valueOf(args[i]);
    2.45 +                } catch (Exception e) {
    2.46 +                    System.err.print(obfuscate);
    2.47 +                    System.err.print(" parameter needs to be followed by one of ");
    2.48 +                    boolean first = true;
    2.49 +                    for (ObfuscationLevel l : ObfuscationLevel.values()) {
    2.50 +                        if (!first) {
    2.51 +                            System.err.print(", ");
    2.52 +                        }
    2.53 +                        System.err.print(l.name());
    2.54 +                        first = false;
    2.55 +                    }
    2.56 +                    System.err.println();
    2.57 +                    System.exit(1);
    2.58 +                }
    2.59 +                continue;
    2.60 +            }
    2.61 +            if (generateTo == null) {
    2.62 +                generateTo = args[i];
    2.63 +            } else {
    2.64 +                classes = classes.addAndNew(args[i]);
    2.65 +            }
    2.66 +        }
    2.67 +        try (Writer w = new BufferedWriter(new FileWriter(generateTo))) {
    2.68 +            Bck2Brwsr.newCompiler().
    2.69 +                obfuscation(obfLevel).
    2.70 +                addRootClasses(classes.toArray()).
    2.71 +                resources(Main.class.getClassLoader()).
    2.72 +                generate(w);
    2.73          }
    2.74      }
    2.75  }