rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 26 Jun 2014 07:42:54 +0200
changeset 1638 a84f511654ae
parent 1636 eb97a082741b
child 1694 615bae775606
permissions -rw-r--r--
Using Object.defineProperty to specify non-enumerable properties on Object
     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 
    19 package org.apidesign.bck2brwsr.mojo;
    20 
    21 import java.io.File;
    22 import java.io.FileWriter;
    23 import java.io.IOException;
    24 import java.io.InputStream;
    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.AbstractMojo;
    33 import org.apache.maven.plugin.MojoExecutionException;
    34 import org.apache.maven.plugin.MojoFailureException;
    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.bck2brwsr.aot.Bck2BrwsrJars;
    41 import org.apidesign.vm4brwsr.Bck2Brwsr;
    42 import org.apidesign.vm4brwsr.ObfuscationLevel;
    43 
    44 /**
    45  *
    46  * @author Jaroslav Tulach
    47  * @since 0.9
    48  */
    49 @Mojo(name = "aot",
    50     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    51     defaultPhase = LifecyclePhase.PACKAGE
    52 )
    53 public class AheadOfTime extends AbstractMojo {
    54     @Parameter(defaultValue = "${project}")
    55     private MavenProject prj;
    56     
    57     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
    58     private File mainJar;
    59 
    60     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
    61     private File mainJavaScript;
    62     
    63     @Parameter
    64     private String[] exports;
    65     
    66     /**
    67      * Directory where to generate ahead-of-time JavaScript files for
    68      * required libraries.
    69      */
    70     @Parameter(defaultValue = "lib")
    71     private String classPathPrefix;
    72 
    73     /** Root JavaScript file to generate */
    74     @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    75     private File vm;
    76     
    77     /**
    78      * The obfuscation level for the generated JavaScript file.
    79      *
    80      * @since 0.5
    81      */
    82     @Parameter(defaultValue = "NONE")
    83     private ObfuscationLevel obfuscation;
    84     
    85     @Override
    86     public void execute() throws MojoExecutionException, MojoFailureException {
    87         URLClassLoader loader;
    88         try {
    89             loader = buildClassLoader(mainJar, prj.getArtifacts());
    90         } catch (MalformedURLException ex) {
    91             throw new MojoFailureException("Can't initialize classloader");
    92         }
    93         for (Artifact a : prj.getArtifacts()) {
    94             if (a.getFile() == null) {
    95                 continue;
    96             }
    97             String n = a.getFile().getName();
    98             if (!n.endsWith(".jar")) {
    99                 continue;
   100             }
   101             if ("provided".equals(a.getScope())) {
   102                 continue;
   103             }
   104             File aot = new File(prj.getBuild().getDirectory(), classPathPrefix);
   105             aot.mkdirs();
   106             File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
   107             if (js.exists()) {
   108                 getLog().info("Skipping " + js + " as it already exists.");
   109                 continue;
   110             }
   111             try {
   112                 getLog().info("Generating " + js);
   113                 aotLibrary(a, js , loader);
   114             } catch (IOException ex) {
   115                 throw new MojoFailureException("Can't compile" + a.getFile(), ex);
   116             }
   117         }
   118         
   119         try {
   120             if (mainJavaScript.exists()) {
   121                 getLog().info("Skipping " + mainJavaScript + " as it already exists.");
   122             } else {
   123                 getLog().info("Generating " + mainJavaScript);
   124                 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar);
   125                 if (exports != null) {
   126                     for (String e : exports) {
   127                         c = c.addExported(e.replace('.', '/'));
   128                     }
   129                 }
   130                 FileWriter w = new FileWriter(mainJavaScript);
   131                 c.
   132                         obfuscation(obfuscation).
   133                         resources(loader).
   134                         generate(w);
   135                 w.close();
   136             }
   137         } catch (IOException ex) {
   138             throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
   139         }
   140             
   141         try {
   142             FileWriter w = new FileWriter(vm);
   143             Bck2Brwsr.newCompiler().
   144                     obfuscation(obfuscation).
   145                     standalone(false).
   146                     resources(new Bck2Brwsr.Resources() {
   147 
   148                 @Override
   149                 public InputStream get(String resource) throws IOException {
   150                     return null;
   151                 }
   152             }).
   153                     generate(w);
   154             w.close();
   155             
   156         } catch (IOException ex) {
   157             throw new MojoExecutionException("Can't compile", ex);
   158         }
   159     }
   160 
   161     private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
   162         FileWriter w = new FileWriter(js);
   163         Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile());
   164         c.
   165             obfuscation(obfuscation).
   166             resources(loader).
   167             generate(w);
   168         w.close();
   169     }
   170     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   171         List<URL> arr = new ArrayList<URL>();
   172         if (root != null) {
   173             arr.add(root.toURI().toURL());
   174         }
   175         for (Artifact a : deps) {
   176             if (a.getFile() != null) {
   177                 arr.add(a.getFile().toURI().toURL());
   178             }
   179         }
   180         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   181     }
   182 }