rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Mon, 20 May 2013 10:59:47 +0200
branchclosure
changeset 1104 47c1fc251d84
parent 988 c2386b2f53d0
child 1503 e0f2f3503eed
permissions -rw-r--r--
Obfuscation fixes to allow run demo-calculator as an external module
     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.bck2brwsr.mojo;
    19 
    20 import org.apache.maven.plugin.AbstractMojo;
    21 
    22 import java.io.File;
    23 import java.io.FileWriter;
    24 import java.io.IOException;
    25 import java.net.MalformedURLException;
    26 import java.net.URL;
    27 import java.net.URLClassLoader;
    28 import java.util.ArrayList;
    29 import java.util.Collection;
    30 import java.util.List;
    31 import org.apache.maven.artifact.Artifact;
    32 import org.apache.maven.plugin.MojoExecutionException;
    33 import org.apache.maven.plugins.annotations.LifecyclePhase;
    34 import org.apache.maven.plugins.annotations.Mojo;
    35 import org.apache.maven.plugins.annotations.Parameter;
    36 import org.apache.maven.project.MavenProject;
    37 import org.apidesign.vm4brwsr.Bck2Brwsr;
    38 import org.apidesign.vm4brwsr.ObfuscationLevel;
    39 
    40 /** Compiles classes into JavaScript. */
    41 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    42 public class Java2JavaScript extends AbstractMojo {
    43     public Java2JavaScript() {
    44     }
    45     /** Root of the class files */
    46     @Parameter(defaultValue="${project.build.directory}/classes")
    47     private File classes;
    48     /** JavaScript file to generate */
    49     @Parameter
    50     private File javascript;
    51 
    52     /** Additional classes that should be pre-compiled into the javascript 
    53      * file. By default compiles all classes found under <code>classes</code>
    54      * directory and their transitive closure.
    55      */
    56     @Parameter
    57     private List<String> compileclasses;
    58     
    59     @Parameter(defaultValue="${project}")
    60     private MavenProject prj;
    61 
    62     /**
    63      * The obfuscation level for the generated JavaScript file.
    64      *
    65      * @since 0.5
    66      */
    67     @Parameter(defaultValue="NONE")
    68     private ObfuscationLevel obfuscation;
    69 
    70     /**
    71      * Indicates whether to create an extension module instead of a standalone
    72      * JavaScript VM.
    73      *
    74      * @since 0.6
    75      */
    76     @Parameter(defaultValue="false")
    77     private boolean extension;
    78 
    79     @Override
    80     public void execute() throws MojoExecutionException {
    81         if (!classes.isDirectory()) {
    82             throw new MojoExecutionException("Can't find " + classes);
    83         }
    84 
    85         List<String> arr = new ArrayList<String>();
    86         long newest = collectAllClasses("", classes, arr);
    87         
    88         if (compileclasses != null) {
    89             arr.retainAll(compileclasses);
    90             arr.addAll(compileclasses);
    91         }
    92         
    93         if (javascript.lastModified() > newest) {
    94             return;
    95         }
    96 
    97         try {
    98             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    99             FileWriter w = new FileWriter(javascript);
   100             Bck2Brwsr.newCompiler().
   101                 obfuscation(obfuscation).
   102                 extension(extension).
   103                 resources(url).
   104                 addRootClasses(arr.toArray(new String[0])).
   105                 generate(w);
   106             w.close();
   107         } catch (IOException ex) {
   108             throw new MojoExecutionException("Can't compile", ex);
   109         }
   110     }
   111 
   112     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
   113         File[] files = toCheck.listFiles();
   114         if (files != null) {
   115             long newest = 0L;
   116             for (File f : files) {
   117                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   118                 if (newest < lastModified) {
   119                     newest = lastModified;
   120                 }
   121             }
   122             return newest;
   123         } else if (toCheck.getName().endsWith(".class")) {
   124             final String cls = prefix.substring(0, prefix.length() - 7);
   125             if (!cls.endsWith("package-info")) {
   126                 arr.add(cls);
   127             }
   128             return toCheck.lastModified();
   129         } else {
   130             return 0L;
   131         }
   132     }
   133 
   134     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   135         List<URL> arr = new ArrayList<URL>();
   136         arr.add(root.toURI().toURL());
   137         for (Artifact a : deps) {
   138             if (a.getFile() != null) {
   139                 arr.add(a.getFile().toURI().toURL());
   140             }
   141         }
   142         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   143     }
   144 }