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