Added GroovyRenameAction and ProcessRenameAction

This commit is contained in:
Reinhard Pointner 2017-04-05 16:15:34 +08:00
parent 952b2c9c7e
commit 54cec6f9f8
5 changed files with 82 additions and 25 deletions

View File

@ -97,7 +97,12 @@ public final class HistorySpooler {
List<Element> sequence = new ArrayList<Element>(); List<Element> sequence = new ArrayList<Element>();
for (Entry<File, File> element : elements) { for (Entry<File, File> element : elements) {
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile())); File k = element.getKey();
File v = element.getValue();
if (k != null && v != null) {
sequence.add(new Element(k.getName(), v.getPath(), k.getParentFile()));
}
} }
if (sequence.size() > 0) { if (sequence.size() > 0) {

View File

@ -28,6 +28,7 @@ import org.kohsuke.args4j.spi.ExplicitBooleanOptionHandler;
import net.filebot.ApplicationFolder; import net.filebot.ApplicationFolder;
import net.filebot.Language; import net.filebot.Language;
import net.filebot.RenameAction;
import net.filebot.StandardRenameAction; import net.filebot.StandardRenameAction;
import net.filebot.WebServices; import net.filebot.WebServices;
import net.filebot.format.ExpressionFileFilter; import net.filebot.format.ExpressionFileFilter;
@ -201,7 +202,7 @@ public class ArgumentBean {
return files; return files;
} }
public StandardRenameAction getRenameAction() { public RenameAction getRenameAction() {
return StandardRenameAction.forName(action); return StandardRenameAction.forName(action);
} }

View File

@ -0,0 +1,34 @@
package net.filebot.cli;
import java.io.File;
import groovy.lang.Closure;
import net.filebot.RenameAction;
public class GroovyRenameAction implements RenameAction {
private final Closure<?> closure;
public GroovyRenameAction(Closure<?> closure) {
this.closure = closure;
}
@Override
public File rename(File from, File to) throws Exception {
Object value = closure.call(from, to);
// must return File object, so we try the result of the closure, but if it's not a File we just return the original destination parameter
return value instanceof File ? (File) value : null;
}
@Override
public boolean canRevert() {
return false;
}
@Override
public String toString() {
return "CLOSURE";
}
}

View File

@ -0,0 +1,38 @@
package net.filebot.cli;
import static net.filebot.Logging.*;
import java.io.File;
import net.filebot.RenameAction;
public class ProcessRenameAction implements RenameAction {
private final String executable;
public ProcessRenameAction(String executable) {
this.executable = executable;
}
@Override
public File rename(File from, File to) throws Exception {
Process process = new ProcessBuilder(executable, from.getPath(), to.getPath()).inheritIO().start();
if (process.waitFor() != 0) {
debug.severe(format("[%s] failed with exit code %d", executable, process.exitValue()));
}
return null;
}
@Override
public boolean canRevert() {
return false;
}
@Override
public String toString() {
return executable;
}
}

View File

@ -513,29 +513,8 @@ public abstract class ScriptShellBaseClass extends Script {
return StandardRenameAction.forName(obj.toString()); return StandardRenameAction.forName(obj.toString());
} }
if (obj instanceof Closure<?>) { if (obj instanceof Closure) {
return new RenameAction() { return new GroovyRenameAction((Closure) obj);
private final Closure<?> closure = (Closure<?>) obj;
@Override
public File rename(File from, File to) throws Exception {
Object value = closure.call(from, to);
// must return File object, so we try the result of the closure, but if it's not a File we just return the original destination parameter
return new File(value.toString());
}
@Override
public boolean canRevert() {
return false;
}
@Override
public String toString() {
return "CLOSURE";
}
};
} }
// object probably can't be casted // object probably can't be casted