rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Jan 2015 20:46:35 +0100
changeset 1762 293838e72201
parent 1604 7665471a56c1
child 1763 647282885e6f
permissions -rw-r--r--
Always write down the JavaScript file in UTF-8 encoding
     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.FileOutputStream;
    24 import java.io.IOException;
    25 import java.io.OutputStreamWriter;
    26 import java.io.Writer;
    27 import java.net.MalformedURLException;
    28 import java.net.URL;
    29 import java.net.URLClassLoader;
    30 import java.util.ArrayList;
    31 import java.util.Collection;
    32 import java.util.List;
    33 import org.apache.maven.artifact.Artifact;
    34 import org.apache.maven.plugin.MojoExecutionException;
    35 import org.apache.maven.plugins.annotations.LifecyclePhase;
    36 import org.apache.maven.plugins.annotations.Mojo;
    37 import org.apache.maven.plugins.annotations.Parameter;
    38 import org.apache.maven.plugins.annotations.ResolutionScope;
    39 import org.apache.maven.project.MavenProject;
    40 import org.apidesign.vm4brwsr.Bck2Brwsr;
    41 import org.apidesign.vm4brwsr.ObfuscationLevel;
    42 
    43 /** Compiles classes into JavaScript. */
    44 @Mojo(name="j2js", 
    45     requiresDependencyResolution = ResolutionScope.COMPILE,
    46     defaultPhase=LifecyclePhase.PROCESS_CLASSES
    47 )
    48 public class Java2JavaScript extends AbstractMojo {
    49     public Java2JavaScript() {
    50     }
    51     /** Root of the class files */
    52     @Parameter(defaultValue="${project.build.directory}/classes")
    53     private File classes;
    54     /** JavaScript file to generate */
    55     @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    56     private File javascript;
    57 
    58     /** Additional classes that should be pre-compiled into the javascript 
    59      * file. By default compiles all classes found under <code>classes</code>
    60      * directory and their transitive closure.
    61      */
    62     @Parameter
    63     private List<String> compileclasses;
    64     
    65     @Parameter(defaultValue="${project}")
    66     private MavenProject prj;
    67 
    68     /**
    69      * The obfuscation level for the generated JavaScript file.
    70      *
    71      * @since 0.5
    72      */
    73     @Parameter(defaultValue="NONE")
    74     private ObfuscationLevel obfuscation;
    75     
    76     /** Should classes from rt.jar be included? */
    77     @Parameter(defaultValue = "false")
    78     private boolean ignoreBootClassPath;
    79 
    80     /**
    81      * Indicates whether to create an extension library 
    82      * module instead of a standalone JavaScript VM.
    83      *
    84      * @since 0.9
    85      */
    86     @Parameter(defaultValue="false")
    87     private boolean library;
    88 
    89     @Override
    90     public void execute() throws MojoExecutionException {
    91         if (!classes.isDirectory()) {
    92             throw new MojoExecutionException("Can't find " + classes);
    93         }
    94         if (javascript == null) {
    95             throw new MojoExecutionException("Need to define 'javascript' attribute with a path to file to generate");
    96         }
    97 
    98         List<String> arr = new ArrayList<String>();
    99         long newest = collectAllClasses("", classes, arr);
   100         
   101         if (compileclasses != null) {
   102             arr.retainAll(compileclasses);
   103             arr.addAll(compileclasses);
   104         }
   105         
   106         if (javascript.lastModified() > newest) {
   107             return;
   108         }
   109 
   110         try {
   111             URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
   112             Writer w = new OutputStreamWriter(new FileOutputStream(javascript), "UTF-8");
   113             Bck2Brwsr c = Bck2Brwsr.newCompiler().
   114                 obfuscation(obfuscation).
   115                 resources(url, ignoreBootClassPath).
   116                 addRootClasses(arr.toArray(new String[0]));
   117             if (library) {
   118                 c = c.library();
   119             }
   120             c.generate(w);
   121             w.close();
   122         } catch (IOException ex) {
   123             throw new MojoExecutionException("Can't compile", ex);
   124         }
   125     }
   126 
   127     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
   128         File[] files = toCheck.listFiles();
   129         if (files != null) {
   130             long newest = 0L;
   131             for (File f : files) {
   132                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   133                 if (newest < lastModified) {
   134                     newest = lastModified;
   135                 }
   136             }
   137             return newest;
   138         } else if (toCheck.getName().endsWith(".class")) {
   139             final String cls = prefix.substring(0, prefix.length() - 7);
   140             if (!cls.endsWith("package-info")) {
   141                 arr.add(cls);
   142             }
   143             return toCheck.lastModified();
   144         } else {
   145             return 0L;
   146         }
   147     }
   148 
   149     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   150         List<URL> arr = new ArrayList<URL>();
   151         arr.add(root.toURI().toURL());
   152         for (Artifact a : deps) {
   153             if (a.getFile() != null) {
   154                 arr.add(a.getFile().toURI().toURL());
   155             }
   156         }
   157         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   158     }
   159 }