mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
brancharithmetic
changeset 774 42bc1e89134d
parent 755 5652acd48509
parent 773 406faa8bc64f
child 778 6f8683517f1f
     1.1 --- a/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java	Mon Feb 25 19:00:08 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 Java2JavaScript extends AbstractMojo {
    1.45 -    public Java2JavaScript() {
    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]), Java2JavaScript.class.getClassLoader());
   1.124 -    }
   1.125 -}