/** Returns all of the .gitattributes files which affect the given files. */ static List<File> gitAttributes(Iterable<File> files) { // build a radix tree out of all the parent folders in these files ConcurrentRadixTree<String> tree = new ConcurrentRadixTree<>(new DefaultCharSequenceNodeFactory()); for (File file : files) { String parentPath = file.getParent() + File.separator; tree.putIfAbsent(parentPath, parentPath); } // traverse the edge nodes to find the outermost folders List<File> edgeFolders = TreeStream.depthFirst(Node::getOutgoingEdges, tree.getNode()) .filter(node -> node.getOutgoingEdges().isEmpty() && node.getValue() != null) .map(node -> new File((String) node.getValue())) .collect(Collectors.toList()); List<File> gitAttrFiles = new ArrayList<>(); Set<File> visitedFolders = new HashSet<>(); for (File edgeFolder : edgeFolders) { gitAttrAddWithParents(edgeFolder, visitedFolders, gitAttrFiles); } return gitAttrFiles; }
protected String getContents(Predicate<String> subpathsToInclude) throws IOException { TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow()); List<File> files = TreeStream.depthFirst(treeDef, rootFolder()) .filter(File::isFile) .collect(Collectors.toList()); ListIterator<File> iterator = files.listIterator(files.size()); int rootLength = rootFolder().getAbsolutePath().length() + 1; return StringPrinter.buildString(printer -> Errors.rethrow().run(() -> { while (iterator.hasPrevious()) { File file = iterator.previous(); String subPath = file.getAbsolutePath().substring(rootLength); if (subpathsToInclude.test(subPath)) { printer.println("### " + subPath + " ###"); printer.println(read(subPath)); } } })); }
/** Sets the enabled status of every child, grandchild, etc. of the given composite. Skips plain-jane Composites. */ public static void setEnabledDeep(Composite root, boolean enabled) { TreeStream.depthFirst(treeDefControl(), root) // skip plain-jane Composites .filter(ctl -> ctl.getClass().equals(Composite.class)) // set the enabled flag .forEach(ctl -> ctl.setEnabled(enabled)); }
/** Deletes all empty folders (recursively). */ public static void deleteEmptyFolders(File d) throws IOException { retry(d, root -> { // define the directory hierarchy TreeDef<File> dirTree = file -> Arrays.stream(file.listFiles()) .filter(File::isDirectory) .collect(Collectors.toList()); // find all the empty directories List<File> emptyDirs = TreeStream.depthFirst(dirTree, root) .filter(dir -> dir.list().length == 0) .collect(Collectors.toList()); for (File emptyDir : emptyDirs) { File toDelete = emptyDir; while (!toDelete.equals(root)) { Preconditions.checkArgument(toDelete.delete(), "Failed to delete %s", toDelete); toDelete = toDelete.getParentFile(); if (toDelete.list().length > 0) { break; } } } return null; }); }
/** Returns all of the .gitattributes files which affect the given files. */ static List<File> gitAttributes(Iterable<File> files) { // build a radix tree out of all the parent folders in these files ConcurrentRadixTree<String> tree = new ConcurrentRadixTree<>(new DefaultCharSequenceNodeFactory()); for (File file : files) { String parentPath = file.getParent() + File.separator; tree.putIfAbsent(parentPath, parentPath); } // traverse the edge nodes to find the outermost folders List<File> edgeFolders = TreeStream.depthFirst(Node::getOutgoingEdges, tree.getNode()) .filter(node -> node.getOutgoingEdges().isEmpty() && node.getValue() != null) .map(node -> new File((String) node.getValue())) .collect(Collectors.toList()); List<File> gitAttrFiles = new ArrayList<>(); Set<File> visitedFolders = new HashSet<>(); for (File edgeFolder : edgeFolders) { gitAttrAddWithParents(edgeFolder, visitedFolders, gitAttrFiles); } return gitAttrFiles; }
protected String getContents(Predicate<String> subpathsToInclude) throws IOException { TreeDef<File> treeDef = TreeDef.forFile(Errors.rethrow()); List<File> files = TreeStream.depthFirst(treeDef, folder.getRoot()) .filter(file -> file.isFile()) .collect(Collectors.toList()); ListIterator<File> iterator = files.listIterator(files.size()); int rootLength = folder.getRoot().getAbsolutePath().length() + 1; return StringPrinter.buildString(printer -> { Errors.rethrow().run(() -> { while (iterator.hasPrevious()) { File file = iterator.previous(); String subPath = file.getAbsolutePath().substring(rootLength); if (subpathsToInclude.test(subPath)) { printer.println("### " + subPath + " ###"); printer.println(read(subPath)); } } }); }); }