rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 11 Sep 2015 14:51:09 +0200
branchflow
changeset 1842 dd4dabfead82
parent 1769 f6057dc5922c
child 1824 9fb23d7831da
permissions -rw-r--r--
Giving the flow analyzer chance to generate the whole function body
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 java.io.File;
    21 import java.io.FileOutputStream;
    22 import java.io.IOException;
    23 import java.io.OutputStreamWriter;
    24 import java.io.Writer;
    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 java.util.jar.Attributes;
    32 import java.util.jar.JarEntry;
    33 import java.util.jar.JarOutputStream;
    34 import java.util.jar.Manifest;
    35 import org.apache.maven.artifact.Artifact;
    36 import org.apache.maven.plugin.AbstractMojo;
    37 import org.apache.maven.plugin.MojoExecutionException;
    38 import org.apache.maven.plugin.MojoFailureException;
    39 import org.apache.maven.plugins.annotations.Component;
    40 import org.apache.maven.plugins.annotations.LifecyclePhase;
    41 import org.apache.maven.plugins.annotations.Mojo;
    42 import org.apache.maven.plugins.annotations.Parameter;
    43 import org.apache.maven.plugins.annotations.ResolutionScope;
    44 import org.apache.maven.project.MavenProject;
    45 import org.apache.maven.project.MavenProjectHelper;
    46 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
    47 import org.apidesign.vm4brwsr.Bck2Brwsr;
    48 import org.apidesign.vm4brwsr.ObfuscationLevel;
    49 
    50 /**
    51  *
    52  * @author Jaroslav Tulach
    53  * @since 0.12
    54  */
    55 @Mojo(name = "library",
    56     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    57     defaultPhase = LifecyclePhase.PACKAGE
    58 )
    59 public class AOTLibrary extends AbstractMojo {
    60     @Parameter(defaultValue = "${project}")
    61     private MavenProject prj;
    62 
    63     @Component
    64     private MavenProjectHelper projectHelper;
    65     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
    66     private File mainJar;    
    67     @Parameter(defaultValue = "${project.build.finalName}-min.js")
    68     private String minified;
    69     @Parameter(defaultValue = "${project.build.finalName}-debug.js")
    70     private String debug;
    71     
    72     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar")
    73     private File aotJar;
    74     
    75     @Parameter
    76     private String[] exports;
    77     
    78     @Parameter
    79     private String[] aotDeps;
    80 
    81     @Parameter(defaultValue = "true")
    82     private boolean ignoreBootClassPath;
    83 
    84     @Override
    85     public void execute() throws MojoExecutionException, MojoFailureException {
    86         URLClassLoader loader;
    87         try {
    88             loader = buildClassLoader(mainJar, prj.getArtifacts());
    89         } catch (MalformedURLException ex) {
    90             throw new MojoFailureException("Can't initialize classloader");
    91         }
    92 
    93         try {
    94             Manifest m = new Manifest();
    95             if (!"false".equals(minified)) {
    96                 Attributes attr = new Attributes();
    97                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
    98                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
    99                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   100                 attr.putValue("Bck2BrwsrMinified", "true");
   101                 m.getEntries().put(minified, attr);
   102             }
   103             if (!"false".equals(debug)) {
   104                 Attributes attr = new Attributes();
   105                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
   106                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
   107                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   108                 attr.putValue("Bck2BrwsrDebug", "true");
   109                 m.getEntries().put(debug, attr);
   110             }
   111             
   112             if (aotDeps != null) {
   113                 for (Artifact a : prj.getArtifacts()) {
   114                     if (!matches(aotDeps, a)) {
   115                         continue;
   116                     }
   117                     
   118                     {
   119                         Attributes attr = new Attributes();
   120                         attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
   121                         attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
   122                         attr.putValue("Bck2BrwsrVersion", a.getVersion());
   123                         attr.putValue("Bck2BrwsrDebug", "true");
   124                         m.getEntries().put(artifactName(a, true), attr);
   125                     }
   126                     {
   127                         Attributes attr = new Attributes();
   128                         attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
   129                         attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
   130                         attr.putValue("Bck2BrwsrVersion", a.getVersion());
   131                         attr.putValue("Bck2BrwsrMinified", "true");
   132                         m.getEntries().put(artifactName(a, false), attr);
   133                     }
   134                 }
   135             }
   136             
   137             FileOutputStream fos = new FileOutputStream(this.aotJar);
   138             JarOutputStream os = new JarOutputStream(fos, m);
   139 
   140             if (!"false".equals(debug)) {
   141                 os.putNextEntry(new JarEntry(debug));
   142                 Writer w = new OutputStreamWriter(os, "UTF-8");
   143                 configureMain(loader).
   144                     obfuscation(ObfuscationLevel.NONE).
   145                     generate(w);
   146                 w.flush();
   147                 os.closeEntry();
   148             }
   149             if (!"false".equals(minified)) {
   150                 os.putNextEntry(new JarEntry(minified));
   151             
   152                 Writer w = new OutputStreamWriter(os, "UTF-8");
   153                 configureMain(loader).
   154                     obfuscation(ObfuscationLevel.FULL).
   155                     generate(w);
   156                 w.flush();
   157                 os.closeEntry();
   158             }
   159             
   160             if (aotDeps != null) {
   161                 for (Artifact a : prj.getArtifacts()) {
   162                     if (!matches(aotDeps, a)) {
   163                         continue;
   164                     }
   165                     getLog().info("Generating bck2brwsr for " + a.getFile());
   166                     Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
   167                     if (exports != null) {
   168                         for (String e : exports) {
   169                             c = c.addExported(e.replace('.', '/'));
   170                         }
   171                     }
   172                     {
   173                         os.putNextEntry(new JarEntry(artifactName(a, true)));
   174                         Writer w = new OutputStreamWriter(os, "UTF-8");
   175                         c.
   176                             obfuscation(ObfuscationLevel.NONE).
   177                             generate(w);
   178                         w.flush();
   179                         os.closeEntry();
   180                     }
   181                     {
   182                         os.putNextEntry(new JarEntry(artifactName(a, false)));
   183 
   184                         Writer w = new OutputStreamWriter(os, "UTF-8");
   185                         c.
   186                             obfuscation(ObfuscationLevel.FULL).
   187                             generate(w);
   188                         w.flush();
   189                         os.closeEntry();
   190                     }                    
   191                 }
   192             }
   193             os.close();
   194             
   195             projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
   196         } catch (IOException ex) {
   197             throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
   198         }
   199     }
   200 
   201     private Bck2Brwsr configureMain(URLClassLoader loader) throws IOException {
   202         Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
   203         if (exports != null) {
   204             for (String e : exports) {
   205                 c = c.addExported(e.replace('.', '/'));
   206             }
   207         }
   208         return c;
   209     }
   210 
   211     private static String artifactName(Artifact a, boolean debug) {
   212         return a.getGroupId() + "-" + a.getArtifactId() + (debug ? "-debug.js" : "-min.js");
   213     }
   214 
   215     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   216         List<URL> arr = new ArrayList<URL>();
   217         if (root != null) {
   218             arr.add(root.toURI().toURL());
   219         }
   220         for (Artifact a : deps) {
   221             if (a.getFile() != null) {
   222                 arr.add(a.getFile().toURI().toURL());
   223             }
   224         }
   225         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   226     }
   227 
   228     private static boolean matches(String[] aotDeps, Artifact a) {
   229         for (String d : aotDeps) {
   230             String[] parts = d.split(":");
   231             for (int i = 0; i < parts.length; i++) {
   232                 if ("*".equals(parts[i])) {
   233                     parts[i] = null;
   234                 }
   235             }
   236             
   237             if (parts[0] != null && !parts[0].equals(a.getGroupId())) {
   238                 continue;
   239             }
   240             if (parts[1] != null && !parts[1].equals(a.getArtifactId())) {
   241                 continue;
   242             }
   243             if (parts.length > 2 && parts[2] != null && !parts[2].equals(a.getClassifier())) {
   244                 continue;
   245             }
   246             return true;
   247         }
   248         return false;
   249     }
   250 }