org.gradle.api.file.FileVisitor Java Examples

The following examples show how to use org.gradle.api.file.FileVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultJarSnapshotter.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
JarSnapshot createSnapshot(byte[] hash, FileTree classes, final ClassFilesAnalyzer analyzer) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    classes.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            analyzer.visitFile(fileDetails);
            String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", "");
            byte[] classHash = hasher.hash(fileDetails.getFile());
            hashes.put(className, classHash);
        }
    });

    return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis()));
}
 
Example #2
Source File: DefaultJarSnapshotter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
JarSnapshot createSnapshot(byte[] hash, FileTree classes, final ClassFilesAnalyzer analyzer) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    classes.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            analyzer.visitFile(fileDetails);
            String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", "");
            byte[] classHash = hasher.hash(fileDetails.getFile());
            hashes.put(className, classHash);
        }
    });

    return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis()));
}
 
Example #3
Source File: MapFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
Example #4
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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()));
        }
    });
}
 
Example #5
Source File: SingleIncludePatternFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!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));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
Example #6
Source File: JarSnapshotter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
JarSnapshot createSnapshot(FileTree archivedClasses) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    archivedClasses.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            hashes.put(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""), hasher.hash(fileDetails.getFile()));
        }
    });
    return new JarSnapshot(hashes);
}
 
Example #7
Source File: AllFromJarRebuildInfo.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Set<String> classesInJar(FileTree jarContents) {
    final Set<String> out = new HashSet<String>();
    jarContents.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}
        public void visitFile(FileVisitDetails fileDetails) {
            out.add(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""));
        }
    });
    return out;
}
 
Example #8
Source File: ArchiveFileTree.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void visit(FileVisitor visitor) {
    if (!archiveFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!archiveFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }

    AtomicBoolean stopFlag = new AtomicBoolean();

    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);
        try {
            ArchiveEntry archiveEntry;

            while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) {
                ArchiveEntryFileTreeElement details = createDetails(chmod);
                details.archiveInputStream = archiveInputStream;
                details.archiveEntry = (E) archiveEntry;
                details.stopFlag = stopFlag;

                try {
                    if (archiveEntry.isDirectory()) {
                        visitor.visitDir(details);
                    }
                    else {
                        visitor.visitFile(details);
                    }
                } finally {
                    details.close();
                }
            }
        } finally {
            archiveInputStream.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
 
Example #9
Source File: ArchiveFileTree.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
public void visit(FileVisitor visitor) {
    if (!archiveFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!archiveFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }

    AtomicBoolean stopFlag = new AtomicBoolean();

    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);
        try {
            ArchiveEntry archiveEntry;

            while (!stopFlag.get() && (archiveEntry = archiveInputStream.getNextEntry()) != null) {
                ArchiveEntryFileTreeElement details = createDetails(chmod);
                details.archiveInputStream = archiveInputStream;
                details.archiveEntry = (E) archiveEntry;
                details.stopFlag = stopFlag;

                try {
                    if (archiveEntry.isDirectory()) {
                        visitor.visitDir(details);
                    }
                    else {
                        visitor.visitFile(details);
                    }
                } finally {
                    details.close();
                }
            }
        } finally {
            archiveInputStream.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
 
Example #10
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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()));
        }
    });
}
 
Example #11
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #12
Source File: MapFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
Example #13
Source File: SingleIncludePatternFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!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));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
Example #14
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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()));
        }
    });
}
 
Example #15
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(FileVisitor visitor) {
    InputStream inputStream;
    try {
        inputStream = resource.read();
        assert inputStream != null;
    } catch (MissingResourceException e) {
        DeprecationLogger.nagUserOfDeprecatedBehaviour(
                String.format("The specified tar file %s does not exist and will be silently ignored", getDisplayName())
        );
        return;
    } catch (ResourceException e) {
        throw new InvalidUserDataException(String.format("Cannot expand %s.", getDisplayName()), e);
    }

    try {
        try {
            visitImpl(visitor, inputStream);
        } finally {
            inputStream.close();
        }
    } catch (Exception e) {
        String message = "Unable to expand " + getDisplayName() + "\n"
                + "  The tar might be corrupted or it is compressed in an unexpected way.\n"
                + "  By default the tar tree tries to guess the compression based on the file extension.\n"
                + "  If you need to specify the compression explicitly please refer to the DSL reference.";
        throw new GradleException(message, e);
    }
}
 
Example #16
Source File: TarFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #17
Source File: MapFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
Example #18
Source File: SingleIncludePatternFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!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));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
Example #19
Source File: JarSnapshotter.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
JarSnapshot createSnapshot(FileTree archivedClasses) {
    final Map<String, byte[]> hashes = new HashMap<String, byte[]>();
    archivedClasses.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {
        }

        public void visitFile(FileVisitDetails fileDetails) {
            hashes.put(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""), hasher.hash(fileDetails.getFile()));
        }
    });
    return new JarSnapshot(hashes);
}
 
Example #20
Source File: AllFromJarRebuildInfo.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Set<String> classesInJar(FileTree jarContents) {
    final Set<String> out = new HashSet<String>();
    jarContents.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}
        public void visitFile(FileVisitDetails fileDetails) {
            out.add(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""));
        }
    });
    return out;
}
 
Example #21
Source File: MapFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitDirs(RelativePath path, FileVisitor visitor) {
    if (path == null || path.getParent() == null || !visitedDirs.add(path)) {
        return;
    }

    visitDirs(path.getParent(), visitor);
    visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod));
}
 
Example #22
Source File: TarFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #23
Source File: SingleIncludePatternFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }

    String segment = patternSegments.get(segmentIndex);

    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!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));
        }
        for (File child : children) {
            if (stopFlag.get()) { break; }
            if (step.matches(child.getName())) {
                relativePath.addLast(child.getName());
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
 
Example #24
Source File: TarFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException {
    AtomicBoolean stopFlag = new AtomicBoolean();
    NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream);
    TarEntry entry;
    while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) {
        if (entry.isDirectory()) {
            visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod));
        } else {
            visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod));
        }
    }
}
 
Example #25
Source File: TarFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void visit(FileVisitor visitor) {
    InputStream inputStream;
    try {
        inputStream = resource.read();
        assert inputStream != null;
    } catch (MissingResourceException e) {
        DeprecationLogger.nagUserOfDeprecatedBehaviour(
                String.format("The specified tar file %s does not exist and will be silently ignored", getDisplayName())
        );
        return;
    } catch (ResourceException e) {
        throw new InvalidUserDataException(String.format("Cannot expand %s.", getDisplayName()), e);
    }

    try {
        try {
            visitImpl(visitor, inputStream);
        } finally {
            inputStream.close();
        }
    } catch (Exception e) {
        String message = "Unable to expand " + getDisplayName() + "\n"
                + "  The tar might be corrupted or it is compressed in an unexpected way.\n"
                + "  By default the tar tree tries to guess the compression based on the file extension.\n"
                + "  If you need to specify the compression explicitly please refer to the DSL reference.";
        throw new GradleException(message, e);
    }
}
 
Example #26
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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()));
        }
    });
}
 
Example #27
Source File: FileTreeAdapter.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public FileTree visit(FileVisitor visitor) {
    tree.visit(visitor);
    return this;
}
 
Example #28
Source File: DelegatingFileTree.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public FileTree visit(FileVisitor visitor) {
    return getDelegate().visit(visitor);
}
 
Example #29
Source File: MapFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Visit(FileVisitor visitor, AtomicBoolean stopFlag) {
    this.visitor = visitor;
    this.stopFlag = stopFlag;
}
 
Example #30
Source File: EmptyFileTree.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void visit(FileVisitor visitor) {
}