diff --git a/source/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule b/source/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule index 579abc68..0c121c0b 100644 --- a/source/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule +++ b/source/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule @@ -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 diff --git a/source/net/filebot/cli/ScriptShell.properties b/source/net/filebot/cli/ScriptShell.properties index 9ba55344..5ee6c060 100644 --- a/source/net/filebot/cli/ScriptShell.properties +++ b/source/net/filebot/cli/ScriptShell.properties @@ -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 \ No newline at end of file +starStaticImport: net.filebot.WebServices, net.filebot.media.MediaDetection \ No newline at end of file diff --git a/source/net/filebot/format/ExpressionBindings.java b/source/net/filebot/format/ExpressionBindings.java index 52522866..3d4ab8a5 100644 --- a/source/net/filebot/format/ExpressionBindings.java +++ b/source/net/filebot/format/ExpressionBindings.java @@ -42,7 +42,7 @@ public class ExpressionBindings extends AbstractMap implements B } protected boolean isUndefined(Object value) { - return value == null || ExpressionFormatFunctions.isEmptyValue(value); + return value == null || ExpressionFormatFunctions.isEmptyValue(null, value); } public Object getBindingBean() { diff --git a/source/net/filebot/format/ExpressionFormat.java b/source/net/filebot/format/ExpressionFormat.java index cef6266e..0f9a3dd6 100644 --- a/source/net/filebot/format/ExpressionFormat.java +++ b/source/net/filebot/format/ExpressionFormat.java @@ -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); } diff --git a/source/net/filebot/format/ExpressionFormatFunctions.java b/source/net/filebot/format/ExpressionFormatFunctions.java index c6d53090..09b6e7a8 100644 --- a/source/net/filebot/format/ExpressionFormatFunctions.java +++ b/source/net/filebot/format/ExpressionFormatFunctions.java @@ -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 allOf(Object c1, Object c2, Object... cN) { - return stream(c1, c2, cN).collect(toList()); + public static List 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 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 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 csv(Object path) throws IOException { + public static Map csv(Script context, Object path) throws IOException { Pattern[] delimiter = { TAB, SEMICOLON }; Map map = new LinkedHashMap(); - 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 readLines(Object path) throws IOException { - return FileUtilities.readLines(getUserFile(path)); + public static List 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()) { diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index 84708305..2ebeb922 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -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; }