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()));
}
});
}
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);
}
}
}
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);
}
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);
}
}
}
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()));
}
Aggregations