Skip to content

Commit 882d81a

Browse files
committed
Merge branch 'script-editor'
This branch brings methods required for the ImageJ2 script editor to recapitulate the File>Make .jar and Run>Compile features of the Fiji Script Editor (as the script editor was known before it moved to ImageJ2 and got a substantial clean-up). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Conflicts: pom.xml
2 parents 3bb620d + dc975e4 commit 882d81a

File tree

3 files changed

+169
-75
lines changed

3 files changed

+169
-75
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>1.167</version>
8+
<version>2.2</version>
99
<relativePath />
1010
</parent>
1111

1212
<artifactId>scripting-java</artifactId>
13-
<version>0.1.2-SNAPSHOT</version>
13+
<version>0.2.0-SNAPSHOT</version>
1414

1515
<name>SciJava Scripting: Java</name>
1616
<description>JSR-223-compliant Java scripting language plugin.</description>

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

Lines changed: 167 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131

3232
package org.scijava.plugins.scripting.java;
3333

34-
import imagej.build.minimaven.BuildEnvironment;
35-
import imagej.build.minimaven.Coordinate;
36-
import imagej.build.minimaven.MavenProject;
37-
3834
import java.io.BufferedReader;
3935
import java.io.ByteArrayInputStream;
4036
import java.io.ByteArrayOutputStream;
@@ -43,6 +39,7 @@
4339
import java.io.FileWriter;
4440
import java.io.IOException;
4541
import java.io.PrintStream;
42+
import java.io.PrintWriter;
4643
import java.io.Reader;
4744
import java.io.StringReader;
4845
import java.io.Writer;
@@ -69,6 +66,9 @@
6966
import org.scijava.command.Command;
7067
import org.scijava.command.CommandInfo;
7168
import org.scijava.command.CommandService;
69+
import org.scijava.minimaven.BuildEnvironment;
70+
import org.scijava.minimaven.Coordinate;
71+
import org.scijava.minimaven.MavenProject;
7272
import org.scijava.plugin.Parameter;
7373
import org.scijava.plugin.Plugin;
7474
import org.scijava.plugin.PluginService;
@@ -108,92 +108,187 @@ public Object eval(String script) throws ScriptException {
108108
@SuppressWarnings({ "unchecked" })
109109
@Override
110110
public Object eval(Reader reader) throws ScriptException {
111-
File temporaryDirectory = null;
111+
final String path = (String)get(FILENAME);
112+
File file = path == null ? null : new File(path);
113+
112114
final Writer writer = getContext().getErrorWriter();
113-
final PrintStream err;
114-
if (writer == null) {
115-
err = null;
116-
} else {
117-
err = new PrintStream(new LineOutputStream() {
118-
119-
@Override
120-
public void println(final String line) throws IOException {
121-
writer.append(line).append('\n');
115+
try {
116+
final Builder builder = new Builder(file, reader, writer);
117+
final MavenProject project = builder.project;
118+
String mainClass = builder.mainClass;
119+
120+
try {
121+
project.build(true);
122+
if (mainClass == null) {
123+
mainClass = project.getMainClass();
124+
if (mainClass == null) {
125+
throw new ScriptException(
126+
"No main class found for file " + file);
127+
}
122128
}
123129

124-
});
130+
// make class loader
131+
String[] paths = project.getClassPath(false).split(
132+
File.pathSeparator);
133+
URL[] urls = new URL[paths.length];
134+
for (int i = 0; i < urls.length; i++)
135+
urls[i] = new URL("file:" + paths[i]
136+
+ (paths[i].endsWith(".jar") ? "" : "/"));
137+
URLClassLoader classLoader = new URLClassLoader(urls,
138+
getClass().getClassLoader());
139+
140+
// needed for sezpoz
141+
Thread.currentThread().setContextClassLoader(classLoader);
142+
143+
// launch main class
144+
final Class<?> clazz = classLoader.loadClass(mainClass);
145+
if (Command.class.isAssignableFrom(clazz)) {
146+
final Plugin annotation = clazz.getAnnotation(Plugin.class);
147+
final CommandInfo info = new CommandInfo(mainClass,
148+
annotation) {
149+
150+
@Override
151+
public Class<? extends Command> loadClass() {
152+
return (Class<? extends Command>) clazz;
153+
}
154+
};
155+
pluginService.addPlugin(info);
156+
commandService.run(info, true);
157+
} else {
158+
Method main = clazz.getMethod("main",
159+
new Class[] { String[].class });
160+
main.invoke(null, new Object[] { new String[0] });
161+
}
162+
} finally {
163+
builder.cleanup();
164+
}
165+
} catch (Exception e) {
166+
if (writer != null) {
167+
final PrintWriter err = new PrintWriter(writer);
168+
e.printStackTrace(err);
169+
err.flush();
170+
} else {
171+
if (e instanceof ScriptException)
172+
throw (ScriptException) e;
173+
throw new ScriptException(e);
174+
}
125175
}
176+
return null;
177+
}
126178

179+
public void compile(final File file, final Writer errorWriter) {
127180
try {
128-
boolean verbose = "true".equals(get("verbose"));
129-
boolean debug = "true".equals(get("debug"));
130-
BuildEnvironment env = new BuildEnvironment(err, true, verbose, debug);
131-
final MavenProject project;
132-
String mainClass = null;
133-
134-
final String path = (String)get(FILENAME);
135-
File file = path == null ? null : new File(path);
136-
if (file == null || !file.exists()) try {
137-
project = writeTemporaryProject(env, reader);
138-
temporaryDirectory = project.getDirectory();
139-
mainClass = project.getMainClass();
140-
} catch (Exception e) {
141-
throw new ScriptException(e);
142-
} else {
143-
if (file.getName().equals("pom.xml")) {
144-
project = env.parse(file, null);
145-
} else {
146-
mainClass = getFullClassName(file);
147-
project = getMavenProject(env, file, mainClass);
148-
}
181+
final Builder builder = new Builder(file, null, errorWriter);
182+
try {
183+
builder.project.build();
184+
} finally {
185+
builder.cleanup();
149186
}
187+
} catch (Throwable t) {
188+
printOrThrow(t, errorWriter);
189+
}
190+
}
150191

151-
project.build(true);
152-
if (mainClass == null) {
153-
mainClass = project.getMainClass();
154-
if (mainClass == null) {
155-
throw new ScriptException("No main class found for file " + file);
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);
156200
}
201+
} finally {
202+
builder.cleanup();
157203
}
204+
} catch (Throwable t) {
205+
printOrThrow(t, errorWriter);
206+
}
207+
}
158208

159-
// make class loader
160-
String[] paths = project.getClassPath(false).split(File.pathSeparator);
161-
URL[] urls = new URL[paths.length];
162-
for (int i = 0; i < urls.length; i++)
163-
urls[i] = new URL("file:" + paths[i] + (paths[i].endsWith(".jar") ? "" : "/"));
164-
URLClassLoader classLoader = new URLClassLoader(urls, getClass().getClassLoader());
165-
166-
// needed for sezpoz
167-
Thread.currentThread().setContextClassLoader(classLoader);
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+
}
168219

169-
// launch main class
170-
final Class<?> clazz = classLoader.loadClass(mainClass);
171-
if (Command.class.isAssignableFrom(clazz)) {
172-
final Plugin annotation = clazz.getAnnotation(Plugin.class);
173-
final CommandInfo info = new CommandInfo(mainClass, annotation) {
220+
private class Builder {
221+
private final PrintStream err;
222+
private final File temporaryDirectory;
223+
private String mainClass;
224+
private MavenProject project;
225+
226+
/**
227+
* Constructs a wrapper around a possibly temporary project.
228+
*
229+
* @param file the {@code .java} file to build (or null, if {@code reader} is set).
230+
* @param reader provides the Java source if {@code file} is {@code null}
231+
* @param errorWriter where to write the error output.
232+
* @throws ScriptException
233+
* @throws IOException
234+
* @throws ParserConfigurationException
235+
* @throws SAXException
236+
* @throws TransformerConfigurationException
237+
* @throws TransformerException
238+
* @throws TransformerFactoryConfigurationError
239+
*/
240+
private Builder(final File file, final Reader reader,
241+
final Writer errorWriter) throws ScriptException, IOException,
242+
ParserConfigurationException, SAXException,
243+
TransformerConfigurationException, TransformerException,
244+
TransformerFactoryConfigurationError {
245+
if (errorWriter == null) {
246+
err = null;
247+
} else {
248+
err = new PrintStream(new LineOutputStream() {
174249

175250
@Override
176-
public Class<? extends Command> loadClass() {
177-
return (Class<? extends Command>) clazz;
251+
public void println(final String line) throws IOException {
252+
errorWriter.append(line).append('\n');
178253
}
179-
};
180-
pluginService.addPlugin(info);
181-
commandService.run(info, true);
182-
} else {
183-
Method main = clazz.getMethod("main", new Class[] { String[].class });
184-
main.invoke(null, new Object[] { new String[0] });
254+
255+
});
185256
}
186-
} catch (Exception e) {
187-
if (err != null) err.close();
188-
if (e instanceof ScriptException) throw (ScriptException)e;
189-
throw new ScriptException(e);
190-
} finally {
191-
if (err != null) err.close();
192-
if (temporaryDirectory != null && !FileUtils.deleteRecursively(temporaryDirectory)) {
257+
258+
boolean verbose = "true".equals(get("verbose"));
259+
boolean debug = "true".equals(get("debug"));
260+
BuildEnvironment env = new BuildEnvironment(err, true, verbose,
261+
debug);
262+
263+
if (file == null || !file.exists())
264+
try {
265+
project = writeTemporaryProject(env, reader);
266+
temporaryDirectory = project.getDirectory();
267+
mainClass = project.getMainClass();
268+
} catch (Exception e) {
269+
throw new ScriptException(e);
270+
}
271+
else {
272+
temporaryDirectory = null;
273+
if (file.getName().equals("pom.xml")) {
274+
project = env.parse(file, null);
275+
} else {
276+
mainClass = getFullClassName(file);
277+
project = getMavenProject(env, file, mainClass);
278+
}
279+
}
280+
}
281+
282+
private void cleanup() {
283+
if (err != null)
284+
err.close();
285+
if (err != null)
286+
err.close();
287+
if (temporaryDirectory != null
288+
&& !FileUtils.deleteRecursively(temporaryDirectory)) {
193289
temporaryDirectory.deleteOnExit();
194290
}
195291
}
196-
return null;
197292
}
198293

199294
private MavenProject getMavenProject(final BuildEnvironment env,
@@ -208,7 +303,7 @@ private MavenProject getMavenProject(final BuildEnvironment env,
208303
path = path.substring(0, path.length() - mainClass.length() - 5);
209304
if (path.replace(File.separatorChar, '/').endsWith("/src/main/java/")) {
210305
path = path.substring(0, path.length() - "src/main/java/".length());
211-
final File pom = new File(path + "pom.xml");
306+
final File pom = new File(path, "pom.xml");
212307
if (pom.exists()) return env.parse(pom, null);
213308
}
214309
final File rootDirectory = file.getParentFile();

src/test/java/org/scijava/plugins/scripting/java/JavaEngineTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ private ScriptEngine evalJava(final File file) throws ScriptException {
183183
miniMaven.put(ScriptEngine.FILENAME, file.getPath());
184184
final ScriptContext context = miniMaven.getContext();
185185
context.setWriter(new OutputStreamWriter(System.out));
186-
context.setErrorWriter(new OutputStreamWriter(System.err));
187186
miniMaven.eval((Reader) null);
188187
return miniMaven;
189188
}

0 commit comments

Comments
 (0)