rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Wed, 13 Mar 2013 16:17:47 +0100
branchclosure
changeset 849 d95117153304
parent 772 d382dacfd73f
child 860 35507d1a5069
permissions -rw-r--r--
Allow to set obfuscation level for j2js
     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     /** File to generate. Defaults bootjava.js in the first non-empty 
    49      package under the classes directory */
    50     @Parameter
    51     private File javascript;
    52 
    53     @Parameter(defaultValue="${project}")
    54     private MavenProject prj;
    55 
    56     @Parameter(defaultValue="NONE")
    57     private ObfuscationLevel obfuscation;
    58 
    59     @Override
    60     public void execute() throws MojoExecutionException {
    61         if (!classes.isDirectory()) {
    62             throw new MojoExecutionException("Can't find " + classes);
    63         }
    64 
    65         if (javascript == null) {
    66             javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    67         }
    68 
    69         List<String> arr = new ArrayList<String>();
    70         long newest = collectAllClasses("", classes, arr);
    71         
    72         if (javascript.lastModified() > newest) {
    73             return;
    74         }
    75 
    76         try {
    77             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    78             FileWriter w = new FileWriter(javascript);
    79             Bck2Brwsr.generate(w, obfuscation, url, arr.toArray(new String[0]));
    80             w.close();
    81         } catch (IOException ex) {
    82             throw new MojoExecutionException("Can't compile", ex);
    83         }
    84     }
    85 
    86     private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    87         if (!dir.isDirectory()) {
    88             throw new MojoExecutionException("Not a directory " + dir);
    89         }
    90         File[] arr = dir.listFiles();
    91         if (arr.length == 1 && arr[0].isDirectory()) {
    92             return findNonEmptyFolder(arr[0]);
    93         }
    94         return dir;
    95     }
    96 
    97     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    98         File[] files = toCheck.listFiles();
    99         if (files != null) {
   100             long newest = 0L;
   101             for (File f : files) {
   102                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   103                 if (newest < lastModified) {
   104                     newest = lastModified;
   105                 }
   106             }
   107             return newest;
   108         } else if (toCheck.getName().endsWith(".class")) {
   109             arr.add(prefix.substring(0, prefix.length() - 7));
   110             return toCheck.lastModified();
   111         } else {
   112             return 0L;
   113         }
   114     }
   115 
   116     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   117         List<URL> arr = new ArrayList<URL>();
   118         arr.add(root.toURI().toURL());
   119         for (Artifact a : deps) {
   120             arr.add(a.getFile().toURI().toURL());
   121         }
   122         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   123     }
   124 }