Added GroovyRenameAction and ProcessRenameAction

This commit is contained in:
Reinhard Pointner 2017-04-05 16:57:58 +08:00
parent 1f47b7db54
commit 38e57db3ef
3 changed files with 42 additions and 16 deletions

View File

@ -203,6 +203,16 @@ public class ArgumentBean {
} }
public RenameAction getRenameAction() { public RenameAction getRenameAction() {
// support custom executables (via absolute path)
if (action.startsWith("/")) {
return new ProcessRenameAction(action);
}
// support custom groovy scripts (via closures)
if (action.startsWith("{")) {
return new GroovyRenameAction(action);
}
return StandardRenameAction.forName(action); return StandardRenameAction.forName(action);
} }

View File

@ -2,6 +2,10 @@ package net.filebot.cli;
import java.io.File; import java.io.File;
import javax.script.ScriptException;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import groovy.lang.Closure; import groovy.lang.Closure;
import net.filebot.RenameAction; import net.filebot.RenameAction;
@ -9,6 +13,14 @@ public class GroovyRenameAction implements RenameAction {
private final Closure<?> closure; private final Closure<?> closure;
public GroovyRenameAction(String script) {
try {
this.closure = compile(script);
} catch (ScriptException e) {
throw new IllegalArgumentException(e);
}
}
public GroovyRenameAction(Closure<?> closure) { public GroovyRenameAction(Closure<?> closure) {
this.closure = closure; this.closure = closure;
} }
@ -31,4 +43,8 @@ public class GroovyRenameAction implements RenameAction {
return "CLOSURE"; return "CLOSURE";
} }
public static Closure<?> compile(String script) throws ScriptException {
return (Closure) DefaultTypeTransformation.castToType(ScriptShell.createScriptEngine().eval(script), Closure.class);
}
} }

View File

@ -19,6 +19,22 @@ import groovy.lang.GroovyClassLoader;
public class ScriptShell { public class ScriptShell {
public static ScriptEngine createScriptEngine() {
ResourceBundle bundle = ResourceBundle.getBundle(ScriptShell.class.getName());
CompilerConfiguration config = new CompilerConfiguration();
config.setScriptBaseClass(bundle.getString("scriptBaseClass"));
// default imports
ImportCustomizer imports = new ImportCustomizer();
imports.addStarImports(COMMA.split(bundle.getString("starImport")));
imports.addStaticStars(COMMA.split(bundle.getString("starStaticImport")));
config.addCompilationCustomizers(imports);
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
return new GroovyScriptEngineImpl(classLoader);
}
public static final String ARGV_BINDING_NAME = "args"; public static final String ARGV_BINDING_NAME = "args";
public static final String SHELL_BINDING_NAME = "__shell"; public static final String SHELL_BINDING_NAME = "__shell";
public static final String SHELL_CLI_BINDING_NAME = "__cli"; public static final String SHELL_CLI_BINDING_NAME = "__cli";
@ -43,22 +59,6 @@ public class ScriptShell {
engine.getContext().setBindings(bindings, ScriptContext.GLOBAL_SCOPE); engine.getContext().setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
} }
public ScriptEngine createScriptEngine() {
ResourceBundle bundle = ResourceBundle.getBundle(ScriptShell.class.getName());
CompilerConfiguration config = new CompilerConfiguration();
config.setScriptBaseClass(bundle.getString("scriptBaseClass"));
// default imports
ImportCustomizer imports = new ImportCustomizer();
imports.addStarImports(COMMA.split(bundle.getString("starImport")));
imports.addStaticStars(COMMA.split(bundle.getString("starStaticImport")));
config.addCompilationCustomizers(imports);
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
return new GroovyScriptEngineImpl(classLoader);
}
public Object evaluate(String script, Bindings bindings) throws Throwable { public Object evaluate(String script, Bindings bindings) throws Throwable {
try { try {
return engine.eval(script, bindings); return engine.eval(script, bindings);