rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 22 May 2014 10:48:09 +0200
branchclosure
changeset 1584 7b6295731c30
child 1594 d7c375541fb7
permissions -rw-r--r--
Static compilator demo is capable to load itself from individual extension files
     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 org.apache.maven.artifact.Artifact;
    39 import org.apache.maven.plugin.AbstractMojo;
    40 import org.apache.maven.plugin.MojoExecutionException;
    41 import org.apache.maven.plugin.MojoFailureException;
    42 import org.apache.maven.plugins.annotations.LifecyclePhase;
    43 import org.apache.maven.plugins.annotations.Mojo;
    44 import org.apache.maven.plugins.annotations.Parameter;
    45 import org.apache.maven.plugins.annotations.ResolutionScope;
    46 import org.apache.maven.project.MavenProject;
    47 import org.apidesign.vm4brwsr.Bck2Brwsr;
    48 import org.apidesign.vm4brwsr.ObfuscationLevel;
    49 
    50 /**
    51  *
    52  * @author Jaroslav Tulach
    53  * @since 0.9
    54  */
    55 @Mojo(name = "aot",
    56     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    57     defaultPhase = LifecyclePhase.PACKAGE
    58 )
    59 public class AheadOfTime extends AbstractMojo {
    60     @Parameter(defaultValue = "${project}")
    61     private MavenProject prj;
    62 
    63     /**
    64      * Directory where to generate ahead-of-time JavaScript files for
    65      * required libraries.
    66      */
    67     @Parameter(defaultValue = "${project.build.directory}/lib")
    68     private File aot;
    69 
    70     /** Root JavaScript file to generate */
    71     @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    72     private File vm;
    73     
    74     /**
    75      * The obfuscation level for the generated JavaScript file.
    76      *
    77      * @since 0.5
    78      */
    79     @Parameter(defaultValue = "NONE")
    80     private ObfuscationLevel obfuscation;
    81     
    82     @Override
    83     public void execute() throws MojoExecutionException, MojoFailureException {
    84         try {
    85             URLClassLoader loader = buildClassLoader(null, prj.getArtifacts());
    86             for (Artifact a : prj.getArtifacts()) {
    87                 if (a.getFile() == null) {
    88                     continue;
    89                 }
    90                 String n = a.getFile().getName();
    91                 if (!n.endsWith(".jar")) {
    92                     continue;
    93                 }
    94                 aot.mkdirs();
    95                 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
    96                 aotLibrary(a, js , loader);
    97             }
    98             
    99             FileWriter w = new FileWriter(vm);
   100             Bck2Brwsr.newCompiler().
   101                     obfuscation(obfuscation).
   102                     standalone(false).
   103                     resources(new Bck2Brwsr.Resources() {
   104 
   105                 @Override
   106                 public InputStream get(String resource) throws IOException {
   107                     return null;
   108                 }
   109             }).
   110                     generate(w);
   111             w.close();
   112             
   113         } catch (IOException ex) {
   114             throw new MojoExecutionException("Can't compile", ex);
   115         }
   116     }
   117 
   118     private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
   119         List<String> classes = new ArrayList<String>();
   120         List<String> resources = new ArrayList<String>();
   121         Set<String> exported = new HashSet<String>();
   122         
   123         JarFile jf = new JarFile(a.getFile());
   124         listJAR(jf, classes , resources, exported);
   125         
   126         FileWriter w = new FileWriter(js);
   127         Bck2Brwsr.newCompiler().
   128                 obfuscation(obfuscation).
   129                 library(true).
   130                 resources(loader).
   131                 addResources(resources.toArray(new String[0])).
   132                 addClasses(classes.toArray(new String[0])).
   133                 addExported(exported.toArray(new String[0])).
   134                 generate(w);
   135         w.close();
   136     }
   137     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   138         List<URL> arr = new ArrayList<URL>();
   139         if (root != null) {
   140             arr.add(root.toURI().toURL());
   141         }
   142         for (Artifact a : deps) {
   143             if (a.getFile() != null) {
   144                 arr.add(a.getFile().toURI().toURL());
   145             }
   146         }
   147         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   148     }
   149     
   150     private static void listJAR(
   151             JarFile j, List<String> classes,
   152             List<String> resources, Set<String> exported
   153     ) throws IOException {
   154         Enumeration<JarEntry> en = j.entries();
   155         while (en.hasMoreElements()) {
   156             JarEntry e = en.nextElement();
   157             final String n = e.getName();
   158             if (n.endsWith("/")) {
   159                 continue;
   160             }
   161             int last = n.lastIndexOf('/');
   162             String pkg = n.substring(0, last + 1);
   163             if (n.endsWith(".class")) {
   164                 classes.add(n.substring(0, n.length() - 6));
   165             } else {
   166                 resources.add(n);
   167                 if (n.startsWith("META-INF/services/") && exported != null) {
   168                     final InputStream is = j.getInputStream(e);
   169                     exportedServices(is, exported);
   170                     is.close();
   171                 }
   172             }
   173         }
   174         String exp = j.getManifest().getMainAttributes().getValue("Export-Package");
   175         if (exp != null && exported != null) {
   176             for (String def : exp.split(",")) {
   177                 for (String sep : def.split(";")) {
   178                     exported.add(sep.replace('.', '/') + "/");
   179                     break;
   180                 }
   181             }
   182         }
   183     }
   184 
   185     static void exportedServices(final InputStream is, Set<String> exported) throws IOException {
   186         BufferedReader r = new BufferedReader(new InputStreamReader(is));
   187         for (;;) {
   188             String l = r.readLine();
   189             if (l == null) {
   190                 break;
   191             }
   192             if (l.startsWith("#")) {
   193                 continue;
   194             }
   195             exported.add(l.replace('.', '/'));
   196         }
   197     }
   198     
   199 }