rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 17:52:56 +0200
branchclosure
changeset 1599 36746c46716a
parent 1594 d7c375541fb7
child 1604 7665471a56c1
permissions -rw-r--r--
Moving the JAR file parsing and processing into a shared library
     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.BufferedReader;
    22 import java.io.File;
    23 import java.io.FileWriter;
    24 import java.io.IOException;
    25 import java.io.InputStream;
    26 import java.io.InputStreamReader;
    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.Enumeration;
    33 import java.util.HashSet;
    34 import java.util.List;
    35 import java.util.Set;
    36 import java.util.jar.JarEntry;
    37 import java.util.jar.JarFile;
    38 import java.util.logging.Level;
    39 import java.util.logging.Logger;
    40 import org.apache.maven.artifact.Artifact;
    41 import org.apache.maven.plugin.AbstractMojo;
    42 import org.apache.maven.plugin.MojoExecutionException;
    43 import org.apache.maven.plugin.MojoFailureException;
    44 import org.apache.maven.plugins.annotations.LifecyclePhase;
    45 import org.apache.maven.plugins.annotations.Mojo;
    46 import org.apache.maven.plugins.annotations.Parameter;
    47 import org.apache.maven.plugins.annotations.ResolutionScope;
    48 import org.apache.maven.project.MavenProject;
    49 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    50 import org.apidesign.vm4brwsr.Bck2Brwsr;
    51 import org.apidesign.vm4brwsr.ObfuscationLevel;
    52 
    53 /**
    54  *
    55  * @author Jaroslav Tulach
    56  * @since 0.9
    57  */
    58 @Mojo(name = "aot",
    59     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    60     defaultPhase = LifecyclePhase.PACKAGE
    61 )
    62 public class AheadOfTime extends AbstractMojo {
    63     @Parameter(defaultValue = "${project}")
    64     private MavenProject prj;
    65 
    66     /**
    67      * Directory where to generate ahead-of-time JavaScript files for
    68      * required libraries.
    69      */
    70     @Parameter(defaultValue = "${project.build.directory}/lib")
    71     private File aot;
    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(null, 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             aot.mkdirs();
   105             File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
   106             try {
   107                 aotLibrary(a, js , loader);
   108             } catch (IOException ex) {
   109                 throw new MojoFailureException("Can't compile" + a.getFile(), ex);
   110             }
   111         }
   112             
   113         try {
   114             FileWriter w = new FileWriter(vm);
   115             Bck2Brwsr.newCompiler().
   116                     obfuscation(obfuscation).
   117                     standalone(false).
   118                     resources(new Bck2Brwsr.Resources() {
   119 
   120                 @Override
   121                 public InputStream get(String resource) throws IOException {
   122                     return null;
   123                 }
   124             }).
   125                     generate(w);
   126             w.close();
   127             
   128         } catch (IOException ex) {
   129             throw new MojoExecutionException("Can't compile", ex);
   130         }
   131     }
   132 
   133     private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
   134         FileWriter w = new FileWriter(js);
   135         Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile());
   136         c.
   137             obfuscation(obfuscation).
   138             resources(loader).
   139             generate(w);
   140         w.close();
   141     }
   142     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   143         List<URL> arr = new ArrayList<URL>();
   144         if (root != null) {
   145             arr.add(root.toURI().toURL());
   146         }
   147         for (Artifact a : deps) {
   148             if (a.getFile() != null) {
   149                 arr.add(a.getFile().toURI().toURL());
   150             }
   151         }
   152         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   153     }
   154 }