mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java
changeset 109 b19d12a9e6d5
child 110 2428122fb24d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java	Tue Oct 16 16:51:22 2012 +0200
     1.3 @@ -0,0 +1,100 @@
     1.4 +package org.apidesign.bck2brwsr.mojo;
     1.5 +
     1.6 +import org.apache.maven.plugin.AbstractMojo;
     1.7 +
     1.8 +import java.io.File;
     1.9 +import java.io.FileWriter;
    1.10 +import java.lang.reflect.Method;
    1.11 +import java.net.MalformedURLException;
    1.12 +import java.net.URL;
    1.13 +import java.net.URLClassLoader;
    1.14 +import java.util.ArrayList;
    1.15 +import java.util.Collection;
    1.16 +import java.util.List;
    1.17 +import org.apache.maven.artifact.Artifact;
    1.18 +import org.apache.maven.model.Dependency;
    1.19 +import org.apache.maven.plugin.MojoExecutionException;
    1.20 +import org.apache.maven.plugins.annotations.Component;
    1.21 +import org.apache.maven.plugins.annotations.LifecyclePhase;
    1.22 +import org.apache.maven.plugins.annotations.Mojo;
    1.23 +import org.apache.maven.plugins.annotations.Parameter;
    1.24 +import org.apache.maven.project.MavenProject;
    1.25 +
    1.26 +/** Compiles classes into JavaScript. */
    1.27 +@Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    1.28 +public class Bck2BrswrMojo extends AbstractMojo {
    1.29 +    public Bck2BrswrMojo() {
    1.30 +    }
    1.31 +    /** Root of the class files */
    1.32 +    @Parameter(defaultValue="${project.build.directory}/classes")
    1.33 +    private File classes;
    1.34 +    /** File to generate. Defaults bootjava.js in the first non-empty 
    1.35 +     package under the classes directory */
    1.36 +    @Parameter
    1.37 +    private File javascript;
    1.38 +    
    1.39 +    @Parameter(defaultValue="${project}")
    1.40 +    private MavenProject prj;
    1.41 +    
    1.42 +    
    1.43 +
    1.44 +    @Override
    1.45 +    public void execute() throws MojoExecutionException {
    1.46 +        if (!classes.isDirectory()) {
    1.47 +            throw new MojoExecutionException("Can't find " + classes);
    1.48 +        }
    1.49 +
    1.50 +        if (javascript == null) {
    1.51 +            javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    1.52 +        }
    1.53 +
    1.54 +        List<String> arr = new ArrayList<String>();
    1.55 +        collectAllClasses("", classes, arr);
    1.56 +        
    1.57 +        
    1.58 +
    1.59 +        try {
    1.60 +            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    1.61 +            
    1.62 +            Class<?> c = Class.forName("org.apidesign.vm4brwsr.GenJS");
    1.63 +            Method m = c.getDeclaredMethod("compile", ClassLoader.class, Appendable.class, List.class);
    1.64 +            m.setAccessible(true);
    1.65 +            FileWriter w = new FileWriter(javascript);
    1.66 +            m.invoke(null, url, w, arr);
    1.67 +            w.close();
    1.68 +        } catch (Exception ex) {
    1.69 +            throw new MojoExecutionException("Can't compile", ex);
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    1.74 +        if (!dir.isDirectory()) {
    1.75 +            throw new MojoExecutionException("Not a directory " + dir);
    1.76 +        }
    1.77 +        File[] arr = dir.listFiles();
    1.78 +        if (arr.length == 1 && arr[0].isDirectory()) {
    1.79 +            return findNonEmptyFolder(arr[0]);
    1.80 +        }
    1.81 +        return dir;
    1.82 +    }
    1.83 +
    1.84 +    private static void collectAllClasses(String prefix, File toCheck, List<String> arr) {
    1.85 +        File[] files = toCheck.listFiles();
    1.86 +        if (files != null) {
    1.87 +            for (File f : files) {
    1.88 +                collectAllClasses(prefix + f.getName() + "/", f, arr);
    1.89 +            }
    1.90 +        } else if (toCheck.getName().endsWith(".class")) {
    1.91 +            arr.add(prefix.substring(0, prefix.length() - 7));
    1.92 +        }
    1.93 +    }
    1.94 +
    1.95 +    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
    1.96 +        List<URL> arr = new ArrayList<URL>();
    1.97 +        arr.add(root.toURI().toURL());
    1.98 +        for (Artifact a : deps) {
    1.99 +            arr.add(a.getFile().toURI().toURL());
   1.100 +        }
   1.101 +        return new URLClassLoader(arr.toArray(new URL[0]), Bck2BrswrMojo.class.getClassLoader());
   1.102 +    }
   1.103 +}