mirror of
https://github.com/mitb-archive/filebot
synced 2025-01-10 21:38:04 -05:00
Added GroovyRenameAction and ProcessRenameAction
This commit is contained in:
parent
1f47b7db54
commit
38e57db3ef
@ -203,6 +203,16 @@ public class ArgumentBean {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,10 @@ package net.filebot.cli;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
|
||||
|
||||
import groovy.lang.Closure;
|
||||
import net.filebot.RenameAction;
|
||||
|
||||
@ -9,6 +13,14 @@ public class GroovyRenameAction implements RenameAction {
|
||||
|
||||
private final Closure<?> closure;
|
||||
|
||||
public GroovyRenameAction(String script) {
|
||||
try {
|
||||
this.closure = compile(script);
|
||||
} catch (ScriptException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public GroovyRenameAction(Closure<?> closure) {
|
||||
this.closure = closure;
|
||||
}
|
||||
@ -31,4 +43,8 @@ public class GroovyRenameAction implements RenameAction {
|
||||
return "CLOSURE";
|
||||
}
|
||||
|
||||
public static Closure<?> compile(String script) throws ScriptException {
|
||||
return (Closure) DefaultTypeTransformation.castToType(ScriptShell.createScriptEngine().eval(script), Closure.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,22 @@ import groovy.lang.GroovyClassLoader;
|
||||
|
||||
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 SHELL_BINDING_NAME = "__shell";
|
||||
public static final String SHELL_CLI_BINDING_NAME = "__cli";
|
||||
@ -43,22 +59,6 @@ public class ScriptShell {
|
||||
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 {
|
||||
try {
|
||||
return engine.eval(script, bindings);
|
||||
|
Loading…
Reference in New Issue
Block a user