mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 07 Dec 2012 19:16:21 +0100
branchregisters
changeset 283 51043a802035
parent 134 e79e499caac8
child 305 503b158cc093
permissions -rw-r--r--
Using format string for code generation
     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.lang.reflect.Method;
    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.project.MavenProject;
    37 
    38 /** Compiles classes into JavaScript. */
    39 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    40 public class Bck2BrswrMojo extends AbstractMojo {
    41     public Bck2BrswrMojo() {
    42     }
    43     /** Root of the class files */
    44     @Parameter(defaultValue="${project.build.directory}/classes")
    45     private File classes;
    46     /** File to generate. Defaults bootjava.js in the first non-empty 
    47      package under the classes directory */
    48     @Parameter
    49     private File javascript;
    50     
    51     @Parameter(defaultValue="${project}")
    52     private MavenProject prj;
    53     
    54     
    55 
    56     @Override
    57     public void execute() throws MojoExecutionException {
    58         if (!classes.isDirectory()) {
    59             throw new MojoExecutionException("Can't find " + classes);
    60         }
    61 
    62         if (javascript == null) {
    63             javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    64         }
    65 
    66         List<String> arr = new ArrayList<String>();
    67         long newest = collectAllClasses("", classes, arr);
    68         
    69         if (javascript.lastModified() > newest) {
    70             return;
    71         }
    72 
    73         try {
    74             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    75             
    76             Class<?> c = Class.forName("org.apidesign.vm4brwsr.GenJS");
    77             Method m = c.getDeclaredMethod("compile", ClassLoader.class, Appendable.class, String[].class);
    78             m.setAccessible(true);
    79             FileWriter w = new FileWriter(javascript);
    80             m.invoke(null, url, w, arr.toArray(new String[0]));
    81             w.close();
    82         } catch (Exception ex) {
    83             throw new MojoExecutionException("Can't compile", ex);
    84         }
    85     }
    86 
    87     private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    88         if (!dir.isDirectory()) {
    89             throw new MojoExecutionException("Not a directory " + dir);
    90         }
    91         File[] arr = dir.listFiles();
    92         if (arr.length == 1 && arr[0].isDirectory()) {
    93             return findNonEmptyFolder(arr[0]);
    94         }
    95         return dir;
    96     }
    97 
    98     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    99         File[] files = toCheck.listFiles();
   100         if (files != null) {
   101             long newest = 0L;
   102             for (File f : files) {
   103                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   104                 if (newest < lastModified) {
   105                     newest = lastModified;
   106                 }
   107             }
   108             return newest;
   109         } else if (toCheck.getName().endsWith(".class")) {
   110             arr.add(prefix.substring(0, prefix.length() - 7));
   111             return toCheck.lastModified();
   112         } else {
   113             return 0L;
   114         }
   115     }
   116 
   117     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   118         List<URL> arr = new ArrayList<URL>();
   119         arr.add(root.toURI().toURL());
   120         for (Artifact a : deps) {
   121             arr.add(a.getFile().toURI().toURL());
   122         }
   123         return new URLClassLoader(arr.toArray(new URL[0]), Bck2BrswrMojo.class.getClassLoader());
   124     }
   125 }