Inject ExpressionFormatFunctions via extensionClasses instead of starImports

This commit is contained in:
Reinhard Pointner 2019-05-25 01:34:58 +07:00
parent 49596efa67
commit 0678aa646a
6 changed files with 33 additions and 40 deletions

View File

@ -1,3 +1,3 @@
moduleName=filebot-format
moduleVersion=2.5.4
extensionClasses=net.filebot.cli.ScriptShellMethods,net.filebot.format.ExpressionFormatMethods
moduleVersion=2.5.7
extensionClasses=net.filebot.cli.ScriptShellMethods,net.filebot.format.ExpressionFormatMethods,net.filebot.format.ExpressionFormatFunctions

View File

@ -1,3 +1,3 @@
scriptBaseClass: net.filebot.cli.ScriptShellBaseClass
starImport: net.filebot, net.filebot.hash, net.filebot.media, net.filebot.mediainfo, net.filebot.similarity, net.filebot.subtitle, net.filebot.torrent, net.filebot.web, net.filebot.util, groovy.io, groovy.xml, groovy.json, java.nio.file, java.nio.file.attribute, java.nio.charset, java.util.stream, java.util.regex, java.time
starStaticImport: net.filebot.WebServices, net.filebot.media.MediaDetection, net.filebot.format.ExpressionFormatFunctions
starStaticImport: net.filebot.WebServices, net.filebot.media.MediaDetection

View File

@ -42,7 +42,7 @@ public class ExpressionBindings extends AbstractMap<String, Object> implements B
}
protected boolean isUndefined(Object value) {
return value == null || ExpressionFormatFunctions.isEmptyValue(value);
return value == null || ExpressionFormatFunctions.isEmptyValue(null, value);
}
public Object getBindingBean() {

View File

@ -23,7 +23,6 @@ import javax.script.SimpleScriptContext;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
import groovy.lang.GroovyClassLoader;
@ -225,14 +224,7 @@ public class ExpressionFormat extends Format {
}
protected static Compilable createScriptEngine() {
CompilerConfiguration config = new CompilerConfiguration();
// include default functions
ImportCustomizer imports = new ImportCustomizer();
imports.addStaticStars(ExpressionFormatFunctions.class.getName());
config.addCompilationCustomizers(imports);
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), config);
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), new CompilerConfiguration());
return new GroovyScriptEngineImpl(classLoader);
}

View File

@ -18,6 +18,7 @@ import java.util.stream.Stream;
import com.sun.jna.Platform;
import groovy.lang.Closure;
import groovy.lang.Script;
import groovy.util.XmlSlurper;
import net.filebot.ApplicationFolder;
import net.filebot.platform.mac.MacAppUtilities;
@ -32,23 +33,23 @@ public class ExpressionFormatFunctions {
* General helpers and utilities
*/
public static Object call(Object object) {
public static Object call(Script context, Object object) {
if (object instanceof Closure) {
try {
return call(((Closure) object).call());
return call(context, ((Closure) object).call());
} catch (Exception e) {
return null;
}
}
if (isEmptyValue(object)) {
if (isEmptyValue(context, object)) {
return null;
}
return object;
}
public static boolean isEmptyValue(Object object) {
public static boolean isEmptyValue(Script context, Object object) {
// treat empty string as null
if (object instanceof CharSequence && object.toString().isEmpty()) {
return true;
@ -62,46 +63,46 @@ public class ExpressionFormatFunctions {
return false;
}
public static Object any(Object c1, Object c2, Object... cN) {
return stream(c1, c2, cN).findFirst().orElse(null);
public static Object any(Script context, Object c1, Object c2, Object... cN) {
return stream(context, c1, c2, cN).findFirst().orElse(null);
}
public static List<Object> allOf(Object c1, Object c2, Object... cN) {
return stream(c1, c2, cN).collect(toList());
public static List<Object> allOf(Script context, Object c1, Object c2, Object... cN) {
return stream(context, c1, c2, cN).collect(toList());
}
public static String concat(Object c1, Object c2, Object... cN) {
return stream(c1, c2, cN).map(Objects::toString).collect(joining());
public static String concat(Script context, Object c1, Object c2, Object... cN) {
return stream(context, c1, c2, cN).map(Objects::toString).collect(joining());
}
protected static Stream<Object> stream(Object c1, Object c2, Object... cN) {
return Stream.concat(Stream.of(c1, c2), Stream.of(cN)).map(ExpressionFormatFunctions::call).filter(Objects::nonNull);
private static Stream<Object> stream(Script context, Object c1, Object c2, Object... cN) {
return Stream.concat(Stream.of(c1, c2), Stream.of(cN)).map(c -> call(context, c)).filter(Objects::nonNull);
}
/*
* Unix Shell / Windows PowerShell utilities
*/
public static String quote(Object c1, Object... cN) {
return Platform.isWindows() ? quotePowerShell(c1, cN) : quoteBash(c1, cN);
public static String quote(Script context, Object c1, Object... cN) {
return Platform.isWindows() ? quotePowerShell(context, c1, cN) : quoteBash(context, c1, cN);
}
public static String quoteBash(Object c1, Object... cN) {
return stream(c1, null, cN).map(Objects::toString).map(s -> "'" + s.replace("'", "'\"'\"'") + "'").collect(joining(" "));
public static String quoteBash(Script context, Object c1, Object... cN) {
return stream(context, c1, null, cN).map(Objects::toString).map(s -> "'" + s.replace("'", "'\"'\"'") + "'").collect(joining(" "));
}
public static String quotePowerShell(Object c1, Object... cN) {
return stream(c1, null, cN).map(Objects::toString).map(s -> "@'\n" + s + "\n'@").collect(joining(" "));
public static String quotePowerShell(Script context, Object c1, Object... cN) {
return stream(context, c1, null, cN).map(Objects::toString).map(s -> "@'\n" + s + "\n'@").collect(joining(" "));
}
/*
* I/O utilities
*/
public static Map<String, String> csv(Object path) throws IOException {
public static Map<String, String> csv(Script context, Object path) throws IOException {
Pattern[] delimiter = { TAB, SEMICOLON };
Map<String, String> map = new LinkedHashMap<String, String>();
for (String line : readLines(path)) {
for (String line : readLines(context, path)) {
for (Pattern d : delimiter) {
String[] field = d.split(line, 2);
if (field.length >= 2) {
@ -113,15 +114,15 @@ public class ExpressionFormatFunctions {
return map;
}
public static List<String> readLines(Object path) throws IOException {
return FileUtilities.readLines(getUserFile(path));
public static List<String> readLines(Script context, Object path) throws IOException {
return FileUtilities.readLines(getUserFile(context, path));
}
public static Object readXml(Object path) throws Exception {
return new XmlSlurper().parse(getUserFile(path));
public static Object readXml(Script context, Object path) throws Exception {
return new XmlSlurper().parse(getUserFile(context, path));
}
public static File getUserFile(Object path) {
public static File getUserFile(Script context, Object path) {
File f = new File(path.toString());
if (!f.isAbsolute()) {

View File

@ -531,7 +531,7 @@ public class ExpressionFormatMethods {
}
}
return new File(self.getParentFile(), concat(name, slash(concat(tag, null, tagN), ""), extension));
return new File(self.getParentFile(), concat(null, name, slash(concat(null, tag, null, tagN), ""), extension));
}
/**
@ -684,7 +684,7 @@ public class ExpressionFormatMethods {
}
public static String plus(String self, Closure closure) {
Object value = call(closure);
Object value = call(null, closure);
return value == null ? self : self + value;
}