Renaming the mojo to be more meaningful emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Feb 2013 19:21:40 +0100
branchemul
changeset 740d3220b78c21a
parent 739 e39c31612afe
child 741 1343f52164f3
Renaming the mojo to be more meaningful
mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java
mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
     1.1 --- a/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Bck2BrswrMojo.java	Fri Feb 15 19:18:57 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,122 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, version 2 of the License.
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program. Look for COPYING file in the top folder.
    1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 - */
    1.21 -package org.apidesign.bck2brwsr.mojo;
    1.22 -
    1.23 -import org.apache.maven.plugin.AbstractMojo;
    1.24 -
    1.25 -import java.io.File;
    1.26 -import java.io.FileWriter;
    1.27 -import java.io.IOException;
    1.28 -import java.net.MalformedURLException;
    1.29 -import java.net.URL;
    1.30 -import java.net.URLClassLoader;
    1.31 -import java.util.ArrayList;
    1.32 -import java.util.Collection;
    1.33 -import java.util.List;
    1.34 -import org.apache.maven.artifact.Artifact;
    1.35 -import org.apache.maven.plugin.MojoExecutionException;
    1.36 -import org.apache.maven.plugins.annotations.LifecyclePhase;
    1.37 -import org.apache.maven.plugins.annotations.Mojo;
    1.38 -import org.apache.maven.plugins.annotations.Parameter;
    1.39 -import org.apache.maven.project.MavenProject;
    1.40 -import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.41 -
    1.42 -/** Compiles classes into JavaScript. */
    1.43 -@Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    1.44 -public class Bck2BrswrMojo extends AbstractMojo {
    1.45 -    public Bck2BrswrMojo() {
    1.46 -    }
    1.47 -    /** Root of the class files */
    1.48 -    @Parameter(defaultValue="${project.build.directory}/classes")
    1.49 -    private File classes;
    1.50 -    /** File to generate. Defaults bootjava.js in the first non-empty 
    1.51 -     package under the classes directory */
    1.52 -    @Parameter
    1.53 -    private File javascript;
    1.54 -    
    1.55 -    @Parameter(defaultValue="${project}")
    1.56 -    private MavenProject prj;
    1.57 -    
    1.58 -    
    1.59 -
    1.60 -    @Override
    1.61 -    public void execute() throws MojoExecutionException {
    1.62 -        if (!classes.isDirectory()) {
    1.63 -            throw new MojoExecutionException("Can't find " + classes);
    1.64 -        }
    1.65 -
    1.66 -        if (javascript == null) {
    1.67 -            javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    1.68 -        }
    1.69 -
    1.70 -        List<String> arr = new ArrayList<String>();
    1.71 -        long newest = collectAllClasses("", classes, arr);
    1.72 -        
    1.73 -        if (javascript.lastModified() > newest) {
    1.74 -            return;
    1.75 -        }
    1.76 -
    1.77 -        try {
    1.78 -            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    1.79 -            FileWriter w = new FileWriter(javascript);
    1.80 -            Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
    1.81 -            w.close();
    1.82 -        } catch (IOException ex) {
    1.83 -            throw new MojoExecutionException("Can't compile", ex);
    1.84 -        }
    1.85 -    }
    1.86 -
    1.87 -    private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    1.88 -        if (!dir.isDirectory()) {
    1.89 -            throw new MojoExecutionException("Not a directory " + dir);
    1.90 -        }
    1.91 -        File[] arr = dir.listFiles();
    1.92 -        if (arr.length == 1 && arr[0].isDirectory()) {
    1.93 -            return findNonEmptyFolder(arr[0]);
    1.94 -        }
    1.95 -        return dir;
    1.96 -    }
    1.97 -
    1.98 -    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    1.99 -        File[] files = toCheck.listFiles();
   1.100 -        if (files != null) {
   1.101 -            long newest = 0L;
   1.102 -            for (File f : files) {
   1.103 -                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   1.104 -                if (newest < lastModified) {
   1.105 -                    newest = lastModified;
   1.106 -                }
   1.107 -            }
   1.108 -            return newest;
   1.109 -        } else if (toCheck.getName().endsWith(".class")) {
   1.110 -            arr.add(prefix.substring(0, prefix.length() - 7));
   1.111 -            return toCheck.lastModified();
   1.112 -        } else {
   1.113 -            return 0L;
   1.114 -        }
   1.115 -    }
   1.116 -
   1.117 -    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   1.118 -        List<URL> arr = new ArrayList<URL>();
   1.119 -        arr.add(root.toURI().toURL());
   1.120 -        for (Artifact a : deps) {
   1.121 -            arr.add(a.getFile().toURI().toURL());
   1.122 -        }
   1.123 -        return new URLClassLoader(arr.toArray(new URL[0]), Bck2BrswrMojo.class.getClassLoader());
   1.124 -    }
   1.125 -}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java	Fri Feb 15 19:21:40 2013 +0100
     2.3 @@ -0,0 +1,122 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.mojo;
    2.22 +
    2.23 +import org.apache.maven.plugin.AbstractMojo;
    2.24 +
    2.25 +import java.io.File;
    2.26 +import java.io.FileWriter;
    2.27 +import java.io.IOException;
    2.28 +import java.net.MalformedURLException;
    2.29 +import java.net.URL;
    2.30 +import java.net.URLClassLoader;
    2.31 +import java.util.ArrayList;
    2.32 +import java.util.Collection;
    2.33 +import java.util.List;
    2.34 +import org.apache.maven.artifact.Artifact;
    2.35 +import org.apache.maven.plugin.MojoExecutionException;
    2.36 +import org.apache.maven.plugins.annotations.LifecyclePhase;
    2.37 +import org.apache.maven.plugins.annotations.Mojo;
    2.38 +import org.apache.maven.plugins.annotations.Parameter;
    2.39 +import org.apache.maven.project.MavenProject;
    2.40 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    2.41 +
    2.42 +/** Compiles classes into JavaScript. */
    2.43 +@Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    2.44 +public class Java2JavaScript extends AbstractMojo {
    2.45 +    public Java2JavaScript() {
    2.46 +    }
    2.47 +    /** Root of the class files */
    2.48 +    @Parameter(defaultValue="${project.build.directory}/classes")
    2.49 +    private File classes;
    2.50 +    /** File to generate. Defaults bootjava.js in the first non-empty 
    2.51 +     package under the classes directory */
    2.52 +    @Parameter
    2.53 +    private File javascript;
    2.54 +    
    2.55 +    @Parameter(defaultValue="${project}")
    2.56 +    private MavenProject prj;
    2.57 +    
    2.58 +    
    2.59 +
    2.60 +    @Override
    2.61 +    public void execute() throws MojoExecutionException {
    2.62 +        if (!classes.isDirectory()) {
    2.63 +            throw new MojoExecutionException("Can't find " + classes);
    2.64 +        }
    2.65 +
    2.66 +        if (javascript == null) {
    2.67 +            javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
    2.68 +        }
    2.69 +
    2.70 +        List<String> arr = new ArrayList<String>();
    2.71 +        long newest = collectAllClasses("", classes, arr);
    2.72 +        
    2.73 +        if (javascript.lastModified() > newest) {
    2.74 +            return;
    2.75 +        }
    2.76 +
    2.77 +        try {
    2.78 +            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    2.79 +            FileWriter w = new FileWriter(javascript);
    2.80 +            Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
    2.81 +            w.close();
    2.82 +        } catch (IOException ex) {
    2.83 +            throw new MojoExecutionException("Can't compile", ex);
    2.84 +        }
    2.85 +    }
    2.86 +
    2.87 +    private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
    2.88 +        if (!dir.isDirectory()) {
    2.89 +            throw new MojoExecutionException("Not a directory " + dir);
    2.90 +        }
    2.91 +        File[] arr = dir.listFiles();
    2.92 +        if (arr.length == 1 && arr[0].isDirectory()) {
    2.93 +            return findNonEmptyFolder(arr[0]);
    2.94 +        }
    2.95 +        return dir;
    2.96 +    }
    2.97 +
    2.98 +    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    2.99 +        File[] files = toCheck.listFiles();
   2.100 +        if (files != null) {
   2.101 +            long newest = 0L;
   2.102 +            for (File f : files) {
   2.103 +                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
   2.104 +                if (newest < lastModified) {
   2.105 +                    newest = lastModified;
   2.106 +                }
   2.107 +            }
   2.108 +            return newest;
   2.109 +        } else if (toCheck.getName().endsWith(".class")) {
   2.110 +            arr.add(prefix.substring(0, prefix.length() - 7));
   2.111 +            return toCheck.lastModified();
   2.112 +        } else {
   2.113 +            return 0L;
   2.114 +        }
   2.115 +    }
   2.116 +
   2.117 +    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   2.118 +        List<URL> arr = new ArrayList<URL>();
   2.119 +        arr.add(root.toURI().toURL());
   2.120 +        for (Artifact a : deps) {
   2.121 +            arr.add(a.getFile().toURI().toURL());
   2.122 +        }
   2.123 +        return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   2.124 +    }
   2.125 +}