Search in sources :

Example 1 with FileVisitor

use of org.gradle.api.file.FileVisitor in project gradle by gradle.

the class CompositeFileTreeTest method visitsEachTreeWithClosure.

@Test
public void visitsEachTreeWithClosure() {
    final Closure visitor = TestUtil.TEST_CLOSURE;
    final FileVisitor closureAsVisitor = DefaultGroovyMethods.asType(visitor, FileVisitor.class);
    context.checking(new Expectations() {

        {
            oneOf(source1).visit(closureAsVisitor);
            oneOf(source2).visit(closureAsVisitor);
        }
    });
    tree.visit(visitor);
}
Also used : Expectations(org.jmock.Expectations) Closure(groovy.lang.Closure) FileVisitor(org.gradle.api.file.FileVisitor) Test(org.junit.Test)

Example 2 with FileVisitor

use of org.gradle.api.file.FileVisitor in project gradle by gradle.

the class CompositeFileTreeTest method visitsEachTreeWithVisitor.

@Test
public void visitsEachTreeWithVisitor() {
    final FileVisitor visitor = context.mock(FileVisitor.class);
    context.checking(new Expectations() {

        {
            oneOf(source1).visit(visitor);
            oneOf(source2).visit(visitor);
        }
    });
    tree.visit(visitor);
}
Also used : Expectations(org.jmock.Expectations) FileVisitor(org.gradle.api.file.FileVisitor) Test(org.junit.Test)

Example 3 with FileVisitor

use of org.gradle.api.file.FileVisitor in project gradle by gradle.

the class SerializableCoffeeScriptCompileSpec method toRelativeFiles.

public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();
    fileTree.visit(new FileVisitor() {

        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
Also used : FileVisitDetails(org.gradle.api.file.FileVisitDetails) RelativeFile(org.gradle.api.internal.file.RelativeFile) FileTree(org.gradle.api.file.FileTree) FileVisitor(org.gradle.api.file.FileVisitor)

Example 4 with FileVisitor

use of org.gradle.api.file.FileVisitor in project gradle by gradle.

the class TarTaskOutputPacker method storeDirectoryProperty.

private void storeDirectoryProperty(String propertyPath, File directory, final TarOutputStream outputStream) throws IOException {
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException(String.format("Expected '%s' to be a directory", directory));
    }
    final String propertyRoot = propertyPath + "/";
    outputStream.putNextEntry(new TarEntry(propertyRoot));
    outputStream.closeEntry();
    FileVisitor visitor = new FileVisitor() {

        @Override
        public void visitDir(FileVisitDetails dirDetails) {
            try {
                storeDirectoryEntry(dirDetails, propertyRoot, outputStream);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            try {
                String path = propertyRoot + fileDetails.getRelativePath().getPathString();
                storeFileEntry(fileDetails.getFile(), path, fileDetails.getLastModified(), fileDetails.getSize(), fileDetails.getMode(), outputStream);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    };
    directoryWalkerFactory.create().walkDir(directory, RelativePath.EMPTY_ROOT, visitor, Specs.satisfyAll(), new AtomicBoolean(), false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileVisitDetails(org.gradle.api.file.FileVisitDetails) TarEntry(org.apache.tools.tar.TarEntry) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 5 with FileVisitor

use of org.gradle.api.file.FileVisitor in project gradle by gradle.

the class RuntimeShadedJarCreator method processDirectory.

private void processDirectory(final ZipOutputStream outputStream, File file, final byte[] buffer, final HashSet<String> seenPaths, final Map<String, List<String>> services) {
    final List<FileVisitDetails> fileVisitDetails = new ArrayList<FileVisitDetails>();
    directoryFileTreeFactory.create(file).visit(new FileVisitor() {

        @Override
        public void visitDir(FileVisitDetails dirDetails) {
            fileVisitDetails.add(dirDetails);
        }

        @Override
        public void visitFile(FileVisitDetails fileDetails) {
            fileVisitDetails.add(fileDetails);
        }
    });
    // We need to sort here since the file order obtained from the filesystem
    // can change between machines and we always want to have the same shaded jars.
    Collections.sort(fileVisitDetails, new Comparator<FileVisitDetails>() {

        @Override
        public int compare(FileVisitDetails o1, FileVisitDetails o2) {
            return o1.getPath().compareTo(o2.getPath());
        }
    });
    for (FileVisitDetails details : fileVisitDetails) {
        try {
            if (details.isDirectory()) {
                ZipEntry zipEntry = newZipEntryWithFixedTime(details.getPath() + "/");
                processEntry(outputStream, null, zipEntry, buffer, seenPaths, services);
            } else {
                ZipEntry zipEntry = newZipEntryWithFixedTime(details.getPath());
                InputStream inputStream = details.open();
                try {
                    processEntry(outputStream, inputStream, zipEntry, buffer, seenPaths, services);
                } finally {
                    inputStream.close();
                }
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}
Also used : FileVisitDetails(org.gradle.api.file.FileVisitDetails) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Aggregations

FileVisitor (org.gradle.api.file.FileVisitor)12 FileVisitDetails (org.gradle.api.file.FileVisitDetails)10 IOException (java.io.IOException)4 UncheckedIOException (org.gradle.api.UncheckedIOException)3 InputStream (java.io.InputStream)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FileTreeElement (org.gradle.api.file.FileTreeElement)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Closure (groovy.lang.Closure)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 JarFile (java.util.jar.JarFile)1 JarOutputStream (java.util.jar.JarOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1