# HG changeset patch # User Jaroslav Tulach # Date 1350399082 -7200 # Node ID b19d12a9e6d55eeb803623818615b6d1dfd10978 # Parent 7e3f72897a839822b11f27ce73fc2858e5b93bbe Maven mojo to generate the Javascript during compilation phase diff -r 7e3f72897a83 -r b19d12a9e6d5 htmlpage/pom.xml --- a/htmlpage/pom.xml Tue Oct 16 13:23:04 2012 +0200 +++ b/htmlpage/pom.xml Tue Oct 16 16:51:22 2012 +0200 @@ -7,7 +7,7 @@ bck2brwsr 1.0-SNAPSHOT - org.apidesign + org.apidesign.bck2brwsr htmlpage 1.0-SNAPSHOT htmlpage diff -r 7e3f72897a83 -r b19d12a9e6d5 mojo/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mojo/pom.xml Tue Oct 16 16:51:22 2012 +0200 @@ -0,0 +1,66 @@ + + + 4.0.0 + + org.apidesign + bck2brwsr + 1.0-SNAPSHOT + + org.apidesign.bck2brwsr + mojo + 1.0-SNAPSHOT + maven-plugin + Maven Mojo to Compile to JavaScript + http://maven.apache.org + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.1 + + + java-annotations + + true + + + + mojo-descriptor + process-classes + + descriptor + + + + + + + + + + org.apache.maven + maven-plugin-api + 3.0.4 + jar + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.0 + jar + + + ${project.groupId} + vm4brwsr + 0.1-SNAPSHOT + + + org.apache.maven + maven-core + 3.0.2 + jar + + + diff -r 7e3f72897a83 -r b19d12a9e6d5 mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java Tue Oct 16 16:51:22 2012 +0200 @@ -0,0 +1,100 @@ +package org.apidesign.bck2brwsr.mojo; + +import org.apache.maven.plugin.AbstractMojo; + +import java.io.File; +import java.io.FileWriter; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +/** Compiles classes into JavaScript. */ +@Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES) +public class Bck2BrswrMojo extends AbstractMojo { + public Bck2BrswrMojo() { + } + /** Root of the class files */ + @Parameter(defaultValue="${project.build.directory}/classes") + private File classes; + /** File to generate. Defaults bootjava.js in the first non-empty + package under the classes directory */ + @Parameter + private File javascript; + + @Parameter(defaultValue="${project}") + private MavenProject prj; + + + + @Override + public void execute() throws MojoExecutionException { + if (!classes.isDirectory()) { + throw new MojoExecutionException("Can't find " + classes); + } + + if (javascript == null) { + javascript = new File(findNonEmptyFolder(classes), "bootjava.js"); + } + + List arr = new ArrayList(); + collectAllClasses("", classes, arr); + + + + try { + URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts()); + + Class c = Class.forName("org.apidesign.vm4brwsr.GenJS"); + Method m = c.getDeclaredMethod("compile", ClassLoader.class, Appendable.class, List.class); + m.setAccessible(true); + FileWriter w = new FileWriter(javascript); + m.invoke(null, url, w, arr); + w.close(); + } catch (Exception ex) { + throw new MojoExecutionException("Can't compile", ex); + } + } + + private static File findNonEmptyFolder(File dir) throws MojoExecutionException { + if (!dir.isDirectory()) { + throw new MojoExecutionException("Not a directory " + dir); + } + File[] arr = dir.listFiles(); + if (arr.length == 1 && arr[0].isDirectory()) { + return findNonEmptyFolder(arr[0]); + } + return dir; + } + + private static void collectAllClasses(String prefix, File toCheck, List arr) { + File[] files = toCheck.listFiles(); + if (files != null) { + for (File f : files) { + collectAllClasses(prefix + f.getName() + "/", f, arr); + } + } else if (toCheck.getName().endsWith(".class")) { + arr.add(prefix.substring(0, prefix.length() - 7)); + } + } + + private static URLClassLoader buildClassLoader(File root, Collection deps) throws MalformedURLException { + List arr = new ArrayList(); + arr.add(root.toURI().toURL()); + for (Artifact a : deps) { + arr.add(a.getFile().toURI().toURL()); + } + return new URLClassLoader(arr.toArray(new URL[0]), Bck2BrswrMojo.class.getClassLoader()); + } +} diff -r 7e3f72897a83 -r b19d12a9e6d5 pom.xml --- a/pom.xml Tue Oct 16 13:23:04 2012 +0200 +++ b/pom.xml Tue Oct 16 16:51:22 2012 +0200 @@ -11,6 +11,7 @@ htmlpage emul core + mojo diff -r 7e3f72897a83 -r b19d12a9e6d5 vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Tue Oct 16 13:23:04 2012 +0200 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Tue Oct 16 16:51:22 2012 +0200 @@ -55,6 +55,9 @@ compile(out, Arrays.asList(names)); } static void compile(Appendable out, List names) throws IOException { + compile(GenJS.class.getClassLoader(), out, names); + } + static void compile(ClassLoader l, Appendable out, List names) throws IOException { for (String baseClass : names) { Map processed = new HashMap(); LinkedList toProcess = new LinkedList(); @@ -92,7 +95,7 @@ processed.put(name, ""); continue; } - InputStream is = loadClass(name); + InputStream is = loadClass(l, name); if (is == null) { throw new IOException("Can't find class " + name); } @@ -119,7 +122,10 @@ } } for (String resource : scripts) { - InputStream emul = GenJS.class.getResourceAsStream(resource); + while (resource.startsWith("/")) { + resource = resource.substring(1); + } + InputStream emul = l.getResourceAsStream(resource); if (emul == null) { throw new IOException("Can't find " + resource); } @@ -183,12 +189,13 @@ } } - private static InputStream loadClass(String name) throws IOException { - Enumeration en = ClassLoader.getSystemClassLoader().getResources(name + ".class"); + private static InputStream loadClass(ClassLoader l, String name) throws IOException { + Enumeration en = l.getResources(name + ".class"); URL u = null; while (en.hasMoreElements()) { u = en.nextElement(); } + System.err.println("loader: " + l + " url : " + u); if (u == null) { throw new IOException("Can't find " + name); }