rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 740 mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java@d3220b78c21a
child 847 094e10a97511
child 849 d95117153304
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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.io.IOException;
    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 import org.apidesign.vm4brwsr.Bck2Brwsr;
    38 
    39 /** Compiles classes into JavaScript. */
    40 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    41 public class Java2JavaScript extends AbstractMojo {
    42     public Java2JavaScript() {
    43     }
    44     /** Root of the class files */
    45     @Parameter(defaultValue="${project.build.directory}/classes")
    46     private File classes;
    47     /** File to generate. Defaults bootjava.js in the first non-empty 
    48      package under the classes directory */
    49     @Parameter
    50     private File javascript;
    51     
    52     @Parameter(defaultValue="${project}")
    53     private MavenProject prj;
    54     
    55     
    56 
    57     @Override
    58     public void execute() throws MojoExecutionException {
    59         if (!classes.isDirectory()) {
    60             throw new MojoExecutionException("Can't find " + classes);
    61         }
    62 
    63         if (javascript == null) {
    64             javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    65         }
    66 
    67         List<String> arr = new ArrayList<String>();
    68         long newest = collectAllClasses("", classes, arr);
    69         
    70         if (javascript.lastModified() > newest) {
    71             return;
    72         }
    73 
    74         try {
    75             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    76             FileWriter w = new FileWriter(javascript);
    77             Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
    78             w.close();
    79         } catch (IOException ex) {
    80             throw new MojoExecutionException("Can't compile", ex);
    81         }
    82     }
    83 
    84     private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    85         if (!dir.isDirectory()) {
    86             throw new MojoExecutionException("Not a directory " + dir);
    87         }
    88         File[] arr = dir.listFiles();
    89         if (arr.length == 1 && arr[0].isDirectory()) {
    90             return findNonEmptyFolder(arr[0]);
    91         }
    92         return dir;
    93     }
    94 
    95     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    96         File[] files = toCheck.listFiles();
    97         if (files != null) {
    98             long newest = 0L;
    99             for (File f : files) {
   100                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   101                 if (newest < lastModified) {
   102                     newest = lastModified;
   103                 }
   104             }
   105             return newest;
   106         } else if (toCheck.getName().endsWith(".class")) {
   107             arr.add(prefix.substring(0, prefix.length() - 7));
   108             return toCheck.lastModified();
   109         } else {
   110             return 0L;
   111         }
   112     }
   113 
   114     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   115         List<URL> arr = new ArrayList<URL>();
   116         arr.add(root.toURI().toURL());
   117         for (Artifact a : deps) {
   118             arr.add(a.getFile().toURI().toURL());
   119         }
   120         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   121     }
   122 }