* add File utility methods to scripting env.

This commit is contained in:
Reinhard Pointner 2011-11-28 15:29:51 +00:00
parent 6e10fd5c4b
commit 17107d503b
2 changed files with 23 additions and 6 deletions

View File

@ -1,15 +1,12 @@
import static groovy.io.FileType.*
import static net.sourceforge.tuned.FileUtilities.*;
File.metaClass.plus = { path -> new File(delegate, path) }
File.metaClass.isVideo = { _types.getFilter("video").accept(delegate) }
File.metaClass.isAudio = { _types.getFilter("audio").accept(delegate) }
File.metaClass.isSubtitle = { _types.getFilter("subtitle").accept(delegate) }
File.metaClass.isVerification = { _types.getFilter("verification").accept(delegate) }
File.metaClass.isDerived = { file -> isDerived(delegate, file) }
File.metaClass.plus = { path -> new File(delegate, path) }
File.metaClass.hasFile = { c -> isDirectory() && listFiles().find{ c.call(it) }}
File.metaClass.getFiles = { def files = []; traverse(type:FILES) { files += it }; return files }
@ -25,6 +22,22 @@ String.metaClass.eachMediaFolder = { c -> new File(delegate).eachMediaFolder(c)
List.metaClass.eachMediaFolder = { c -> getFolders().findAll{ it.hasFile{ it.isVideo() } }.each(c) }
// FileUtilities
import static net.sourceforge.tuned.FileUtilities.*;
File.metaClass.getNameWithoutExtension = { getNameWithoutExtension(delegate.getName()) }
File.metaClass.getExtension = { getExtension(delegate) }
File.metaClass.hasExtension = { String... ext -> hasExtension(delegate, ext) }
File.metaClass.isDerived = { f -> isDerived(delegate, f) }
File.metaClass.validateFileName = { validateFileName(delegate) }
File.metaClass.validateFilePath = { validateFilePath(delegate) }
File.metaClass.moveTo = { f -> renameFile(delegate, f) }
List.metaClass.mapByFolder = { mapByFolder(delegate) }
List.metaClass.mapByExtension = { mapByExtension(delegate) }
def require(cond) { if (!cond()) throw new Exception('Require failed') }

View File

@ -346,11 +346,15 @@ public final class FileUtilities {
SortedMap<File, List<File>> map = new TreeMap<File, List<File>>();
for (File file : files) {
List<File> valueList = map.get(file.getParentFile());
File key = file.getParentFile();
if (key == null) {
throw new IllegalArgumentException("Parent is null: " + file);
}
List<File> valueList = map.get(key);
if (valueList == null) {
valueList = new ArrayList<File>();
map.put(file.getParentFile(), valueList);
map.put(key, valueList);
}
valueList.add(file);