/** 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; }
/** * Searches breadth-first for the TreeNode with the given content. * * @throws IllegalArgumentException if no such node exists */ public TreeNode<T> findByContent(T content) { requireNonNull(content); Optional<TreeNode<T>> opt = TreeStream.breadthFirst(treeDef(), this).filter(node -> node.getContent().equals(content)).findFirst(); if (opt.isPresent()) { return opt.get(); } else { throw new IllegalArgumentException(this.toString() + " has no child with content " + content); } } }
/** * @param project the project on which we'll call {@link Project#javaexec(Action)}. * @param input the JavaExecable which we'll take as input and call run() on. * @param settings any extra settings you'd like to set on the JavaExec (e.g. heap) * @return the JavaExecable after it has had run() called. */ public static <T extends JavaExecable> T exec(Project project, T input, Action<JavaExecSpec> settings) throws Throwable { // copy the classpath from the project's buildscript (and its parents) List<FileCollection> classpaths = TreeStream.toParent(ProjectPlugin.treeDef(), project) .map(p -> p.getBuildscript().getConfigurations().getByName(BUILDSCRIPT_CLASSPATH)) .collect(Collectors.toList()); // add the gradleApi, workaround from https://discuss.gradle.org/t/gradle-doesnt-add-the-same-dependencies-to-classpath-when-applying-plugins/9759/6?u=ned_twigg classpaths.add(project.getConfigurations().detachedConfiguration(project.getDependencies().gradleApi())); // add stuff from the local classloader too, to fix testkit's classpath classpaths.add(project.files(JavaExecableImp.fromLocalClassloader())); // run it return JavaExecableImp.execInternal(input, project.files(classpaths), settings, execSpec -> JavaExecWinFriendly.javaExec(project, execSpec)); }
public ColorComparePanel(Composite parent) { super(new Composite(parent, SWT.NONE)); Layouts.setGrid(wrapped) .numColumns(2) .columnsEqualWidth(true) .horizontalSpacing(0); swatchActual = new Label(wrapped, SWT.NONE); swatchNearest = new Label(wrapped, SWT.NONE); Layouts.setGridData(swatchActual).grabHorizontal(); Layouts.setGridData(swatchNearest).grabHorizontal(); Label actualLbl = new Label(wrapped, SWT.NONE); actualLbl.setText("Actual"); Label nearestLbl = new Label(wrapped, SWT.NONE); nearestLbl.setText("Nearest"); rgbActual = new Text(wrapped, SWT.BORDER | SWT.READ_ONLY); rgbNearest = new Text(wrapped, SWT.BORDER | SWT.READ_ONLY); Layouts.setGridData(rgbActual).grabHorizontal(); Layouts.setGridData(rgbNearest).grabHorizontal(); nameNearest = new Text(wrapped, SWT.BORDER | SWT.READ_ONLY); Layouts.setGridData(nameNearest).horizontalSpan(2).grabHorizontal(); // set all fonts to system large TreeStream.breadthFirst(SwtMisc.treeDefControl(), parent) .forEach(ctl -> ctl.setFont(Fonts.systemLarge())); rgbActual.setText(" "); rgbNearest.setText(" "); }
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)); } } }); }); }