Skip to content

Commit

Permalink
MiniMaven: move business logic into fiji.build.minimaven
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Jul 9, 2012
1 parent 46c6f48 commit 4dc1a77
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import fiji.SimpleExecuter;

import fiji.build.Fake;
import fiji.build.POM;
import fiji.build.Parser;
import fiji.build.Rule;
import fiji.build.SubFake;
import fiji.build.minimaven.POM;

import ij.IJ;

Expand Down
13 changes: 10 additions & 3 deletions src-plugins/fake/src/main/java/fiji/build/Fake.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@

import java.util.zip.ZipException;

import fiji.build.minimaven.JarClassLoader;
import fiji.build.minimaven.JavaCompiler;
import fiji.build.minimaven.JavaCompiler.CompileError;

public class Fake {
protected static String fijiBuildJar;
protected static long mtimeFijiBuild;
public PrintStream out = System.out, err = System.err;
protected JavaCompiler javac;

public static void main(String[] args) {
checkObsoleteLauncher();
Expand Down Expand Up @@ -571,10 +576,12 @@ protected List<String> compileJavas(List<String> javas, File cwd, File buildDir,
err.println(output);
}

if (javac == null)
javac = new JavaCompiler(err, out);
try {
new JavaCompiler(err, out).call(args, verbose);
} catch (FakeException e) {
throw e;
javac.call(args, verbose);
} catch (CompileError e) {
throw new FakeException(e.getMessage(), e);
} catch (Exception e) {
e.printStackTrace();
throw new FakeException("Compile error: " + e);
Expand Down
4 changes: 4 additions & 0 deletions src-plugins/fake/src/main/java/fiji/build/FakeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public FakeException(String message) {
super(message);
}

public FakeException(String message, Throwable cause) {
super(message, cause);
}

public String toString() {
return getMessage();
}
Expand Down
27 changes: 16 additions & 11 deletions src-plugins/fake/src/main/java/fiji/build/MiniMaven.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fiji.build;

import fiji.build.minimaven.BuildEnvironment;
import fiji.build.minimaven.Coordinate;
import fiji.build.minimaven.POM;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -78,12 +82,13 @@ public static void ensureIJDirIsSet() {

public static void main(String[] args) throws Exception {
ensureIJDirIsSet();
BuildEnvironment miniMaven = new BuildEnvironment(System.err, "true".equals(getSystemProperty("minimaven.verbose", "false")), false);
POM root = miniMaven.parse(new File("pom.xml"), null);
PrintStream err = System.err;
BuildEnvironment env = new BuildEnvironment(err, false, "true".equals(getSystemProperty("minimaven.verbose", "false")), false);
POM root = env.parse(new File("pom.xml"), null);
String command = args.length == 0 ? "compile-and-run" : args[0];
String artifactId = getSystemProperty("artifactId", root.coordinate.artifactId.equals("pom-ij-base") ? "ij-app" : root.coordinate.artifactId);
String artifactId = getSystemProperty("artifactId", root.getArtifactId().equals("pom-ij-base") ? "ij-app" : root.getArtifactId());

POM pom = root.findPOM(new Coordinate(null, artifactId, null), false, miniMaven.downloadAutomatically);
POM pom = root.findPOM(new Coordinate(null, artifactId, null), false, env.getDownloadAutomatically());
if (pom == null)
pom = root;
if (command.equals("compile") || command.equals("build") || command.equals("compile-and-run")) {
Expand All @@ -94,23 +99,23 @@ public static void main(String[] args) throws Exception {
return;
}
else if (command.equals("jar") || command.equals("jars")) {
if (!pom.buildFromSource) {
if (!pom.getBuildFromSource()) {
System.err.println("Cannot build " + pom + " from source");
System.exit(1);
}
pom.buildJar();
if (command.equals("jars"))
pom.copyDependencies(new File(pom.directory, "target"), true);
pom.copyDependencies(pom.getTarget(), true);
return;
}
if (command.equals("clean"))
pom.clean();
else if (command.equals("get") || command.equals("get-dependencies"))
pom.downloadDependencies();
else if (command.equals("run")) {
String mainClass = getSystemProperty("mainClass", pom.mainClass);
String mainClass = getSystemProperty("mainClass", pom.getMainClass());
if (mainClass == null) {
miniMaven.err.println("No main class specified in pom " + pom.coordinate);
err.println("No main class specified in pom " + pom.getCoordinate());
System.exit(1);
}
String[] paths = pom.getClassPath(false).split(File.pathSeparator);
Expand All @@ -125,14 +130,14 @@ else if (command.equals("run")) {
main.invoke(null, new Object[] { new String[0] });
}
else if (command.equals("classpath"))
miniMaven.err.println(pom.getClassPath(false));
err.println(pom.getClassPath(false));
else if (command.equals("list")) {
Set<POM> result = new TreeSet<POM>();
Stack<POM> stack = new Stack<POM>();
stack.push(pom.getRoot());
while (!stack.empty()) {
pom = stack.pop();
if (result.contains(pom) || !pom.buildFromSource)
if (result.contains(pom) || !pom.getBuildFromSource())
continue;
result.add(pom);
for (POM child : pom.getChildren())
Expand All @@ -142,7 +147,7 @@ else if (command.equals("list")) {
System.err.println(pom2);
}
else
miniMaven.err.println("Unhandled command: " + command + "\n" + usage);
err.println("Unhandled command: " + command + "\n" + usage);
}

protected static String getSystemProperty(String key, String defaultValue) {
Expand Down
13 changes: 8 additions & 5 deletions src-plugins/fake/src/main/java/fiji/build/SubFake.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fiji.build;

import fiji.build.POM;
import fiji.build.minimaven.BuildEnvironment;
import fiji.build.minimaven.Coordinate;
import fiji.build.minimaven.POM;

import java.io.File;
import java.util.List;
Expand Down Expand Up @@ -119,9 +121,10 @@ public POM getPOM() {
targetBasename = targetBasename.substring(0, targetBasename.length() - 4);
// TODO: targetBasename could end in "-<version>"
try {
boolean verbose = getVarBool("VERBOSE");
boolean debug = getVarBool("DEBUG");
if (miniMaven == null) {
miniMaven = new BuildEnvironment(parser.fake.err, getVarBool("VERBOSE"), getVarBool("DEBUG"));
miniMaven.downloadAutomatically = !miniMaven.offlineMode;
miniMaven = new BuildEnvironment(parser.fake.err, true, verbose, debug);
MiniMaven.ensureIJDirIsSet();
String ijDir = System.getProperty("ij.dir");
File submodules = new File(ijDir, "modules");
Expand All @@ -139,8 +142,8 @@ public POM getPOM() {
}
}
pom = miniMaven.parse(file);
if (!targetBasename.equals(pom.getArtifact()))
pom = pom.findPOM(new Coordinate(null, targetBasename, null), miniMaven.verbose, miniMaven.downloadAutomatically);
if (!targetBasename.equals(pom.getArtifactId()))
pom = pom.findPOM(new Coordinate(null, targetBasename, null), verbose, miniMaven.getDownloadAutomatically());
} catch (Exception e) {
e.printStackTrace(parser.fake.err);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fiji.build;
package fiji.build.minimaven;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -50,6 +50,10 @@ public class BuildEnvironment {
mavenRepository = repository;
}

public boolean getDownloadAutomatically() {
return downloadAutomatically && !offlineMode;
}

protected static boolean isInteractiveConsole() {
// We want to compile/run with Java5, so we cannot test System.console() directly
try {
Expand All @@ -59,9 +63,10 @@ protected static boolean isInteractiveConsole() {
}
}

public BuildEnvironment(PrintStream err, boolean verbose, boolean debug) throws FakeException {
public BuildEnvironment(PrintStream err, boolean downloadAutomatically, boolean verbose, boolean debug) {
this.err = err;
javac = new JavaCompiler(err, err);
this.downloadAutomatically = downloadAutomatically;
this.verbose = verbose;
this.debug = debug;
if ("true".equalsIgnoreCase(System.getProperty("minimaven.offline")))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fiji.build;
package fiji.build.minimaven;

public class Coordinate {
protected String groupId, artifactId, version, systemPath, classifier, scope, snapshotVersion;
Expand Down Expand Up @@ -70,4 +70,4 @@ public String toString() {
extra = "{" + extra.substring(1) + "}";
return getFileName(true, true, null) + extra;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fiji.build;
package fiji.build.minimaven;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

Expand Down Expand Up @@ -117,7 +118,7 @@ public synchronized Class<?> loadClass(String name,
if (input == null)
throw new ClassNotFoundException(name);
try {
byte[] buffer = Util.readStream(input);
byte[] buffer = readStream(input);
input.close();
result = defineClass(name,
buffer, 0, buffer.length);
Expand All @@ -133,4 +134,18 @@ public synchronized Class<?> loadClass(String name,
return result;
}
}
}

protected static byte[] readStream(InputStream in) throws IOException {
byte[] buffer = new byte[16384];
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (;;) {
int count = in.read(buffer);
if (count < 0)
break;
out.write(buffer, 0, count);
}
in.close();
out.close();
return out.toByteArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fiji.build;
package fiji.build.minimaven;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,7 +17,7 @@ public JavaCompiler(PrintStream err, PrintStream out) {

// this function handles the javac singleton
public void call(String[] arguments,
boolean verbose) throws FakeException {
boolean verbose) throws CompileError {
synchronized(this) {
try {
if (javac == null) {
Expand All @@ -33,14 +33,11 @@ public void call(String[] arguments,

Object result = javac.invoke(null,
new Object[] { arguments, new PrintWriter(err) });
if (!result.equals(new Integer(0))) {
FakeException e = new FakeException("Compile error");
e.printStackTrace();
throw e;
}
if (!result.equals(new Integer(0)))
throw new CompileError(result);
return;
} catch (FakeException e) {
/* was compile error */
} catch (CompileError e) {
/* re-throw */
throw e;
} catch (Exception e) {
e.printStackTrace(err);
Expand All @@ -57,13 +54,26 @@ public void call(String[] arguments,
try {
execute(newArguments, new File("."), verbose);
} catch (Exception e) {
throw new FakeException("Could not even fall back "
throw new RuntimeException("Could not even fall back "
+ " to javac in the PATH");
}
}

public static class CompileError extends Exception {
protected Object result;

public CompileError(Object result) {
super("Compile error: " + result);
this.result = result;
}

public Object getResult() {
return result;
}
}

protected void execute(String[] args, File dir, boolean verbose)
throws IOException, FakeException {
throws IOException {
if (verbose) {
String output = "Executing:";
for (int i = 0; i < args.length; i++)
Expand All @@ -72,48 +82,27 @@ protected void execute(String[] args, File dir, boolean verbose)
}

/* stupid, stupid Windows... */
if (Util.getPlatform().startsWith("win")) {
// handle .sh scripts
if (args[0].endsWith(".sh")) {
String[] newArgs = new String[args.length + 1];
newArgs[0] = "sh.exe";
System.arraycopy(args, 0, newArgs, 1, args.length);
args = newArgs;
}
if (System.getProperty("os.name").startsWith("Windows")) {
for (int i = 0; i < args.length; i++)
args[i] = quoteArg(args[i]);
// stupid, stupid, stupid Windows taking all my time!!!
if (args[0].startsWith("../"))
args[0] = new File(dir,
args[0]).getAbsolutePath();
else if ((args[0].equals("bash") || args[0].equals("sh")) && Util.getPlatform().equals("win64")) {
String[] newArgs = new String[args.length + 2];
newArgs[0] = System.getenv("WINDIR") + "\\SYSWOW64\\cmd.exe";
newArgs[1] = "/C";
System.arraycopy(args, 0, newArgs, 2, args.length);
args = newArgs;
if (verbose) {
String output = "Executing (win32 on win64 using SYSWOW64\\cmd.exe):";
for (int i = 0; i < args.length; i++)
output += " '" + args[i] + "'";
err.println(output);
}

}
}

Process proc = Runtime.getRuntime().exec(args, null, dir);
new StreamDumper(proc.getErrorStream(), err).start();
new StreamDumper(proc.getInputStream(), out).start();
new ReadInto(proc.getErrorStream(), err).start();
new ReadInto(proc.getInputStream(), out).start();
try {
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
throw new FakeException(e.getMessage());
throw new RuntimeException(e.getMessage());
}
int exitValue = proc.exitValue();
if (exitValue != 0)
throw new FakeException("Failed: " + exitValue);
throw new RuntimeException("Failed: " + exitValue);
}

private static String quotables = " \"\'";
Expand Down Expand Up @@ -155,4 +144,4 @@ protected static JarClassLoader discoverJavac() throws IOException {
}
return new JarClassLoader(javac.getPath());
}
}
}
Loading

0 comments on commit 4dc1a77

Please sign in to comment.