Search in sources :

Example 1 with FileVisitDetails

use of org.gradle.api.file.FileVisitDetails 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 2 with FileVisitDetails

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

the class AbstractDirectoryWalker method walkDir.

@Override
public void walkDir(File file, RelativePath path, FileVisitor visitor, Spec<? super FileTreeElement> spec, AtomicBoolean stopFlag, boolean postfix) {
    File[] children = getChildren(file);
    if (children == null) {
        if (file.isDirectory() && !file.canRead()) {
            throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
        }
        // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
        throw new GradleException(String.format("Could not list contents of '%s'.", file));
    }
    List<FileVisitDetails> dirs = new ArrayList<FileVisitDetails>();
    for (int i = 0; !stopFlag.get() && i < children.length; i++) {
        File child = children[i];
        boolean isFile = child.isFile();
        RelativePath childPath = path.append(isFile, child.getName());
        FileVisitDetails details = new DefaultFileVisitDetails(child, childPath, stopFlag, fileSystem, fileSystem, !isFile);
        if (DirectoryFileTree.isAllowed(details, spec)) {
            if (isFile) {
                visitor.visitFile(details);
            } else {
                dirs.add(details);
            }
        }
    }
    // now handle dirs
    for (int i = 0; !stopFlag.get() && i < dirs.size(); i++) {
        FileVisitDetails dir = dirs.get(i);
        if (postfix) {
            walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix);
            visitor.visitDir(dir);
        } else {
            visitor.visitDir(dir);
            walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix);
        }
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) FileVisitDetails(org.gradle.api.file.FileVisitDetails) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) File(java.io.File)

Example 3 with FileVisitDetails

use of org.gradle.api.file.FileVisitDetails 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 4 with FileVisitDetails

use of org.gradle.api.file.FileVisitDetails 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>();
    new DirectoryFileTree(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) DirectoryFileTree(org.gradle.api.internal.file.collections.DirectoryFileTree) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 5 with FileVisitDetails

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

the class DefaultJarSnapshotter method createSnapshot.

public JarSnapshot createSnapshot(HashCode hash, JarArchive jarArchive) {
    final Map<String, HashCode> hashes = Maps.newHashMap();
    final ClassDependentsAccumulator accumulator = new ClassDependentsAccumulator();
    jarArchive.contents.visit(new FileVisitor() {

        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            if (!fileDetails.getName().endsWith(".class")) {
                return;
            }
            HashCode classFileHash;
            InputStream inputStream = fileDetails.open();
            try {
                classFileHash = hasher.hash(inputStream);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            ClassAnalysis analysis = analyzer.getClassAnalysis(classFileHash, fileDetails);
            accumulator.addClass(analysis);
            hashes.put(analysis.getClassName(), classFileHash);
        }
    });
    return new JarSnapshot(new JarSnapshotData(hash, hashes, accumulator.getAnalysis()));
}
Also used : HashCode(com.google.common.hash.HashCode) FileVisitDetails(org.gradle.api.file.FileVisitDetails) ClassDependentsAccumulator(org.gradle.api.internal.tasks.compile.incremental.deps.ClassDependentsAccumulator) InputStream(java.io.InputStream) FileVisitor(org.gradle.api.file.FileVisitor) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ClassAnalysis(org.gradle.api.internal.tasks.compile.incremental.deps.ClassAnalysis)

Aggregations

FileVisitDetails (org.gradle.api.file.FileVisitDetails)12 FileVisitor (org.gradle.api.file.FileVisitor)6 IOException (java.io.IOException)4 UncheckedIOException (org.gradle.api.UncheckedIOException)4 File (java.io.File)3 RelativePath (org.gradle.api.file.RelativePath)3 DefaultFileVisitDetails (org.gradle.api.internal.file.DefaultFileVisitDetails)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GradleException (org.gradle.api.GradleException)2 EmptyFileVisitor (org.gradle.api.file.EmptyFileVisitor)2 ClassReader (org.objectweb.asm.ClassReader)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashCode (com.google.common.hash.HashCode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 List (java.util.List)1