List<File> getSourceRoots() { if (sourceRoots == null) { sourceRoots = Lists.newArrayList(); for (Object source : sources) { if (isDirectory(source)) { sourceRoots.add((File) source); } else if (isDirectoryTree(source)) { sourceRoots.add(((DirectoryTree) source).getDir()); } else if (isSourceDirectorySet(source)) { sourceRoots.addAll(((SourceDirectorySet) source).getSrcDirs()); } else { throw new UnsupportedOperationException(); } } } return sourceRoots; }
private List<SourceFolder> projectRelativeFolders(Iterable<SourceSet> sourceSets, Function<File, String> provideRelativePath) { ArrayList<SourceFolder> entries = Lists.newArrayList(); List<SourceSet> sortedSourceSets = sortSourceSetsAsPerUsualConvention(sourceSets); for (SourceSet sourceSet : sortedSourceSets) { List<DirectoryTree> sortedSourceDirs = sortSourceDirsAsPerUsualConvention(sourceSet.getAllSource().getSrcDirTrees()); for (DirectoryTree tree : sortedSourceDirs) { File dir = tree.getDir(); if (dir.isDirectory()) { String relativePath = provideRelativePath.apply(dir); SourceFolder folder = new SourceFolder(relativePath, null); folder.setDir(dir); folder.setName(dir.getName()); folder.setIncludes(getIncludesForTree(sourceSet, tree)); folder.setExcludes(getExcludesForTree(sourceSet, tree)); entries.add(folder); } } } return entries; }
public boolean contains(File file) { File absoluteFile = file.getAbsoluteFile(); String pathWithSeparator = file.getAbsolutePath() + File.separator; for (File candidateFile : files) { String candidateFilePathWithSeparator = candidateFile.getPath() + File.separator; if (pathWithSeparator.startsWith(candidateFilePathWithSeparator)) { return true; } } for (DirectoryTree tree : trees) { if (tree.getDir().getAbsoluteFile().equals(absoluteFile) || DirectoryTrees.contains(FileSystems.getDefault(), tree, absoluteFile)) { return true; } } return false; }
private Set<DirectoryTree> doGetSrcDirTrees() { Set<DirectoryTree> result = new LinkedHashSet<DirectoryTree>(); for (Object path : source) { if (path instanceof SourceDirectorySet) { SourceDirectorySet nested = (SourceDirectorySet) path; result.addAll(nested.getSrcDirTrees()); } else { for (File srcDir : fileResolver.resolveFiles(path)) { if (srcDir.exists() && !srcDir.isDirectory()) { throw new InvalidUserDataException(String.format("Source directory '%s' is not a directory.", srcDir)); } result.add(directoryFileTreeFactory.create(srcDir, patterns)); } } } return result; }
private Set<DirectoryTree> doGetSrcDirTrees() { Set<DirectoryTree> result = new LinkedHashSet<DirectoryTree>(); for (Object path : source) { if (path instanceof SourceDirectorySet) { SourceDirectorySet nested = (SourceDirectorySet) path; result.addAll(nested.getSrcDirTrees()); } else { File srcDir = fileResolver.resolve(path); if (srcDir.exists() && !srcDir.isDirectory()) { throw new InvalidUserDataException(String.format("Source directory '%s' is not a directory.", srcDir)); } result.add(new DirectoryFileTree(srcDir, patterns)); } } return result; }
private List<String> getIncludesForTree(SourceSet sourceSet, DirectoryTree directoryTree) { List<Set<String>> includesByType = getFiltersForTreeGroupedByType(sourceSet, directoryTree, "includes"); for (Set<String> it : includesByType) { if (it.isEmpty()) { return Collections.emptyList(); } } List<String> allIncludes = CollectionUtils.flattenCollections(String.class, includesByType); return CollectionUtils.dedup(allIncludes, Equivalence.equals()); }
private List<Set<String>> getFiltersForTreeGroupedByType(SourceSet sourceSet, DirectoryTree directoryTree, String filterOperation) { // check for duplicate entries in java and resources Set<File> javaSrcDirs = sourceSet.getAllJava().getSrcDirs(); Set<File> resSrcDirs = sourceSet.getResources().getSrcDirs(); List<File> srcDirs = CollectionUtils.intersection(Lists.newArrayList(javaSrcDirs, resSrcDirs)); if (!srcDirs.contains(directoryTree.getDir())) { return Lists.<Set<String>>newArrayList(collectFilters(directoryTree.getPatterns(), filterOperation)); } else { Set<String> resourcesFilter = collectFilters(sourceSet.getResources().getSrcDirTrees(), directoryTree.getDir(), filterOperation); Set<String> sourceFilter = collectFilters(sourceSet.getAllJava().getSrcDirTrees(), directoryTree.getDir(), filterOperation); return Lists.<Set<String>>newArrayList(resourcesFilter, sourceFilter); } }
private Set<String> collectFilters(Set<DirectoryTree> directoryTrees, File targetDir, String filterOperation) { for (DirectoryTree directoryTree : directoryTrees) { if (directoryTree.getDir().equals(targetDir)) { PatternSet patterns = directoryTree.getPatterns(); return collectFilters(patterns, filterOperation); } } return Collections.emptySet(); }
private List<DirectoryTree> sortSourceDirsAsPerUsualConvention(Iterable<DirectoryTree> sourceDirs) { return CollectionUtils.sort(sourceDirs, new Comparator<DirectoryTree>() { @Override public int compare(DirectoryTree left, DirectoryTree right) { return toComparable(left).compareTo(toComparable(right)); } }); }
private static Comparable toComparable(DirectoryTree tree) { String path = tree.getDir().getPath(); if (path.endsWith("java")) { return 0; } else if (path.endsWith("resources")) { return 2; } else { return 1; } }
public static ImmutableDirectoryTree of(DirectoryTree source) { if (source instanceof ImmutableDirectoryTree) { return (ImmutableDirectoryTree) source; } else { return of(source.getDir(), source.getPatterns()); } }
public Iterable<? extends File> getRoots() { return FileUtils.calculateRoots( Iterables.concat(files, Iterables.transform(trees, new Function<DirectoryTree, File>() { @Override public File apply(DirectoryTree input) { return input.getDir(); } })) ); }
public Builder add(DirectoryTree directoryTree) { lock.lock(); try { trees.add(ImmutableDirectoryTree.of(directoryTree)); return this; } finally { lock.unlock(); } }
public Set<File> getSrcDirs() { Set<File> dirs = new LinkedHashSet<File>(); for (DirectoryTree tree : getSrcDirTrees()) { dirs.add(tree.getDir()); } return dirs; }
public Set<DirectoryTree> getSrcDirTrees() { // This implementation is broken. It does not consider include and exclude patterns Map<File, DirectoryTree> trees = new LinkedHashMap<File, DirectoryTree>(); for (DirectoryTree tree : doGetSrcDirTrees()) { if (!trees.containsKey(tree.getDir())) { trees.put(tree.getDir(), tree); } } return new LinkedHashSet<DirectoryTree>(trees.values()); }
public static boolean contains(FileSystem fileSystem, DirectoryTree tree, File file) { String prefix = tree.getDir().getAbsolutePath() + File.separator; if (!file.getAbsolutePath().startsWith(prefix)) { return false; } RelativePath path = RelativePath.parse(true, file.getAbsolutePath().substring(prefix.length())); return tree.getPatterns().getAsSpec().isSatisfiedBy(new DefaultFileTreeElement(file, path, fileSystem, fileSystem)); }
@Override public void resolve(FileCollectionResolveContext context) { for (DirectoryTree directoryTree : getSrcDirTrees()) { if (directoryTree.getDir().isDirectory()) { context.add(((DirectoryFileTree) directoryTree).filter(filter)); } } }
private boolean isDirectoryTree(Object source) { return source instanceof DirectoryTree; }
private List<String> getExcludesForTree(SourceSet sourceSet, DirectoryTree directoryTree) { List<Set<String>> excludesByType = getFiltersForTreeGroupedByType(sourceSet, directoryTree, "excludes"); return CollectionUtils.intersection(excludesByType); }
@Override public void visitContents(FileCollectionResolveContext context) { for (DirectoryTree directoryTree : doGetSrcDirTrees()) { context.add(((DirectoryFileTree) directoryTree).filter(filter)); } }