Skip to content

Commit 723a4cd

Browse files
committed
Add the compile() and the makeJar() methods for the script editor
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 764e49d commit 723a4cd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/main/java/org/scijava/plugins/scripting/java/JavaEngine.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,47 @@ public Class<? extends Command> loadClass() {
176176
return null;
177177
}
178178

179+
public void compile(final File file, final Writer errorWriter) {
180+
try {
181+
final Builder builder = new Builder(file, null, errorWriter);
182+
try {
183+
builder.project.build();
184+
} finally {
185+
builder.cleanup();
186+
}
187+
} catch (Throwable t) {
188+
printOrThrow(t, errorWriter);
189+
}
190+
}
191+
192+
public void makeJar(final File file, final boolean includeSources, final File output, final Writer errorWriter) {
193+
try {
194+
final Builder builder = new Builder(file, null, errorWriter);
195+
try {
196+
builder.project.build(true, true, includeSources);
197+
final File target = builder.project.getTarget();
198+
if (output != null && !target.equals(output)) {
199+
BuildEnvironment.copyFile(target, output);
200+
}
201+
} finally {
202+
builder.cleanup();
203+
}
204+
} catch (Throwable t) {
205+
printOrThrow(t, errorWriter);
206+
}
207+
}
208+
209+
private void printOrThrow(Throwable t, Writer errorWriter) {
210+
RuntimeException e = t instanceof RuntimeException ? (RuntimeException) t
211+
: new RuntimeException(t);
212+
if (errorWriter == null) {
213+
throw e;
214+
}
215+
final PrintWriter err = new PrintWriter(errorWriter);
216+
e.printStackTrace(err);
217+
err.flush();
218+
}
219+
179220
private class Builder {
180221
private final PrintStream err;
181222
private final File temporaryDirectory;

0 commit comments

Comments
 (0)