* use new groovy extension mechanisms (but keep slow *.lib.groovy backwards compatibility for now)
This commit is contained in:
parent
9aa97268c5
commit
a0ae0392da
|
@ -1,6 +1,6 @@
|
||||||
package net.sourceforge.filebot.cli;
|
package net.sourceforge.filebot.cli;
|
||||||
|
|
||||||
import static net.sourceforge.filebot.cli.CLILogging.*;
|
import groovy.lang.GroovyClassLoader;
|
||||||
|
|
||||||
import java.awt.AWTPermission;
|
import java.awt.AWTPermission;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -28,24 +28,21 @@ import javax.script.ScriptException;
|
||||||
import javax.script.SimpleBindings;
|
import javax.script.SimpleBindings;
|
||||||
import javax.script.SimpleScriptContext;
|
import javax.script.SimpleScriptContext;
|
||||||
|
|
||||||
import net.sourceforge.filebot.MediaTypes;
|
|
||||||
import net.sourceforge.filebot.WebServices;
|
|
||||||
import net.sourceforge.filebot.format.AssociativeScriptObject;
|
|
||||||
import net.sourceforge.filebot.format.ExpressionFormat;
|
import net.sourceforge.filebot.format.ExpressionFormat;
|
||||||
import net.sourceforge.filebot.format.PrivilegedInvocation;
|
import net.sourceforge.filebot.format.PrivilegedInvocation;
|
||||||
import net.sourceforge.filebot.web.EpisodeListProvider;
|
|
||||||
import net.sourceforge.filebot.web.MovieIdentificationService;
|
|
||||||
|
|
||||||
import org.codehaus.groovy.jsr223.GroovyScriptEngineFactory;
|
import org.codehaus.groovy.control.CompilerConfiguration;
|
||||||
|
import org.codehaus.groovy.control.customizers.ImportCustomizer;
|
||||||
|
import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
|
||||||
import org.codehaus.groovy.runtime.StackTraceUtils;
|
import org.codehaus.groovy.runtime.StackTraceUtils;
|
||||||
|
|
||||||
public class ScriptShell {
|
public class ScriptShell {
|
||||||
|
|
||||||
private final ScriptEngine engine = new GroovyScriptEngineFactory().getScriptEngine();
|
private final ScriptEngine engine;
|
||||||
|
|
||||||
private final ScriptProvider scriptProvider;
|
private final ScriptProvider scriptProvider;
|
||||||
|
|
||||||
public ScriptShell(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc, ScriptProvider scriptProvider) throws ScriptException {
|
public ScriptShell(CmdlineInterface cli, ArgumentBean args, AccessControlContext acc, ScriptProvider scriptProvider) throws ScriptException {
|
||||||
|
this.engine = createScriptEngine();
|
||||||
this.scriptProvider = scriptProvider;
|
this.scriptProvider = scriptProvider;
|
||||||
|
|
||||||
// setup script context
|
// setup script context
|
||||||
|
@ -58,6 +55,25 @@ public class ScriptShell {
|
||||||
engine.eval(new InputStreamReader(ScriptShell.class.getResourceAsStream("ScriptShell.lib.groovy")));
|
engine.eval(new InputStreamReader(ScriptShell.class.getResourceAsStream("ScriptShell.lib.groovy")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScriptEngine createScriptEngine() {
|
||||||
|
CompilerConfiguration config = new CompilerConfiguration();
|
||||||
|
config.setScriptBaseClass("net.sourceforge.filebot.cli.ScriptShellBaseClass");
|
||||||
|
config.setRecompileGroovySource(false);
|
||||||
|
config.setDebug(false);
|
||||||
|
|
||||||
|
// default imports
|
||||||
|
ImportCustomizer imports = new ImportCustomizer();
|
||||||
|
imports.addStarImports("net.sourceforge.filebot", "net.sourceforge.filebot.util", "net.sourceforge.filebot.web", "net.sourceforge.filebot.media", "net.sourceforge.filebot.mediainfo", "net.sourceforge.filebot.hash");
|
||||||
|
imports.addStaticStars("net.sourceforge.filebot.WebServices");
|
||||||
|
imports.addStarImports("groovy.io", "groovy.xml", "groovy.json", "org.jsoup");
|
||||||
|
imports.addStarImports("java.nio.file", "java.nio.file.attribute", "java.util.regex");
|
||||||
|
imports.addStaticStars("java.nio.file.Files");
|
||||||
|
config.addCompilationCustomizers(imports);
|
||||||
|
|
||||||
|
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
|
||||||
|
return new GroovyScriptEngineImpl(classLoader);
|
||||||
|
}
|
||||||
|
|
||||||
public static interface ScriptProvider {
|
public static interface ScriptProvider {
|
||||||
|
|
||||||
public URI getScriptLocation(String input) throws Exception;
|
public URI getScriptLocation(String input) throws Exception;
|
||||||
|
@ -115,7 +131,7 @@ public class ScriptShell {
|
||||||
|
|
||||||
// bind external parameters
|
// bind external parameters
|
||||||
if (args.bindings != null) {
|
if (args.bindings != null) {
|
||||||
for (Entry<String, String> it : args.bindings) {
|
for (Entry<String, String> it : args.bindings.entrySet()) {
|
||||||
bindings.put(it.getKey(), it.getValue());
|
bindings.put(it.getKey(), it.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,32 +144,12 @@ public class ScriptShell {
|
||||||
|
|
||||||
Map<String, String> defines = new LinkedHashMap<String, String>();
|
Map<String, String> defines = new LinkedHashMap<String, String>();
|
||||||
if (args.bindings != null) {
|
if (args.bindings != null) {
|
||||||
for (Entry<String, String> it : args.bindings) {
|
for (Entry<String, String> it : args.bindings.entrySet()) {
|
||||||
defines.put(it.getKey(), it.getValue());
|
defines.put(it.getKey(), it.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bindings.put("_def", defines);
|
bindings.put("_def", defines);
|
||||||
|
|
||||||
bindings.put("_types", MediaTypes.getDefault());
|
|
||||||
bindings.put("_log", CLILogger);
|
|
||||||
|
|
||||||
// bind Java properties and environment variables
|
|
||||||
bindings.put("_system", new AssociativeScriptObject(System.getProperties()));
|
|
||||||
bindings.put("_environment", new AssociativeScriptObject(System.getenv()));
|
|
||||||
|
|
||||||
// bind console object
|
|
||||||
bindings.put("console", System.console());
|
|
||||||
|
|
||||||
// bind Episode data providers
|
|
||||||
for (EpisodeListProvider service : WebServices.getEpisodeListProviders()) {
|
|
||||||
bindings.put(service.getName(), service);
|
|
||||||
}
|
|
||||||
|
|
||||||
// bind Movie data providers
|
|
||||||
for (MovieIdentificationService service : WebServices.getMovieIdentificationServices()) {
|
|
||||||
bindings.put(service.getName(), service);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bindings;
|
return bindings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
package net.sourceforge.filebot.cli;
|
||||||
|
|
||||||
|
import static net.sourceforge.filebot.cli.CLILogging.*;
|
||||||
|
import groovy.lang.Closure;
|
||||||
|
import groovy.lang.Script;
|
||||||
|
|
||||||
|
import java.io.Console;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import net.sourceforge.filebot.MediaTypes;
|
||||||
|
import net.sourceforge.filebot.format.AssociativeScriptObject;
|
||||||
|
|
||||||
|
public abstract class ScriptShellBaseClass extends Script {
|
||||||
|
|
||||||
|
public ScriptShellBaseClass() {
|
||||||
|
System.out.println(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object _guarded(Closure<?> c) {
|
||||||
|
try {
|
||||||
|
return c.call();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
CLILogger.severe(String.format("%s: %s", e.getClass().getSimpleName(), e.getMessage()));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object run() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// define global variable: _system
|
||||||
|
public AssociativeScriptObject get_system() {
|
||||||
|
return new AssociativeScriptObject(System.getProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
// define global variable: _environment
|
||||||
|
public AssociativeScriptObject get_environment() {
|
||||||
|
return new AssociativeScriptObject(System.getenv());
|
||||||
|
}
|
||||||
|
|
||||||
|
// define global variable: _types
|
||||||
|
public MediaTypes get_types() {
|
||||||
|
return MediaTypes.getDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
// define global variable: _log
|
||||||
|
public Logger get_log() {
|
||||||
|
return CLILogger;
|
||||||
|
}
|
||||||
|
|
||||||
|
// define global variable: console
|
||||||
|
public Console getConsole() {
|
||||||
|
return System.console();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue