public WorkResult execute(final CopyActionProcessingStream stream) { final Set<RelativePath> visited = new HashSet<RelativePath>(); WorkResult didWork = delegate.execute(new CopyActionProcessingStream() { public void process(final CopyActionProcessingStreamAction action) { stream.process(new CopyActionProcessingStreamAction() { public void processFile(FileCopyDetailsInternal details) { visited.add(details.getRelativePath()); action.processFile(details); } }); } }); SyncCopyActionDecoratorFileVisitor fileVisitor = new SyncCopyActionDecoratorFileVisitor(visited, preserveSpec); MinimalFileTree walker = new DirectoryFileTree(baseDestDir).postfix(); walker.visit(fileVisitor); visited.clear(); return new SimpleWorkResult(didWork.getDidWork() || fileVisitor.didWork); }
public WorkResult execute(final CopyActionProcessingStream stream) { final Set<RelativePath> visited = new HashSet<RelativePath>(); WorkResult didWork = delegate.execute(new CopyActionProcessingStream() { public void process(final CopyActionProcessingStreamAction action) { stream.process(new CopyActionProcessingStreamAction() { public void processFile(FileCopyDetailsInternal details) { visited.add(details.getRelativePath()); action.processFile(details); } }); } }); SyncCopyActionDecoratorFileVisitor fileVisitor = new SyncCopyActionDecoratorFileVisitor(visited); MinimalFileTree walker = new DirectoryFileTree(baseDestDir).postfix(); walker.visit(fileVisitor); visited.clear(); return new SimpleWorkResult(didWork.getDidWork() || fileVisitor.didWork); }
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; } String childName = child.getName(); if (step.matches(childName)) { relativePath.addLast(childName); 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(); } }
public File transform(RelativePath relativePath) { String sourceFileName = relativePath.getLastName(); String destinationFileNameBase = sourceFileName; if (sourceFileName.endsWith(".coffee")) { destinationFileNameBase = sourceFileName.substring(0, sourceFileName.length() - 7); } String destinationFileName = destinationFileNameBase + ".js"; RelativePath destinationRelativePath = relativePath.replaceLastName(destinationFileName); return new File(destination, destinationRelativePath.getPathString()); }
public static Transformer<Transformer<File, RelativePath>, File> asFactory() { return new Transformer<Transformer<File, RelativePath>, File>() { public Transformer<File, RelativePath> transform(File original) { return new CoffeeScriptCompileDestinationCalculator(original); } }; }
protected Spec<FileTreeElement> createSpec(Collection<String> patterns, boolean include, boolean caseSensitive) { if (patterns.isEmpty()) { return include ? Specs.<FileTreeElement>satisfyAll() : Specs.<FileTreeElement>satisfyNone(); } List<Spec<RelativePath>> matchers = new ArrayList<Spec<RelativePath>>(patterns.size()); for (String pattern : patterns) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(include, caseSensitive, pattern); matchers.add(patternMatcher); } return new RelativePathSpec(Specs.union(matchers)); }
public boolean isSatisfiedBy(RelativePath element) { if (element.isFile() || !partialMatchDirs) { return pathMatcher.matches(element.getSegments(), 0); } else { return pathMatcher.isPrefix(element.getSegments(), 0); } }
public DefaultFileVisitDetails(File file, RelativePath relativePath, AtomicBoolean stop, Chmod chmod, Stat stat, boolean isDirectory, long lastModified, long size) { super(file, relativePath, chmod, stat); this.stop = stop; this.isDirectory = isDirectory; this.lastModified = lastModified; this.size = size; }
public Set<File> getFilesWithoutCreating() { return CollectionUtils.collect(elements.keySet(), new Transformer<File, RelativePath>() { @Override public File transform(RelativePath relativePath) { return createFileInstance(relativePath); } }); }
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)); }
public FileVisitDetailsImpl(RelativePath path, Action<OutputStream> generator, AtomicBoolean stopFlag, Chmod chmod) { super(chmod); this.path = path; this.generator = generator; this.stopFlag = stopFlag; this.isDirectory = !path.isFile(); }
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 walkDir(File file, RelativePath path, FileVisitor visitor, Spec<? super FileTreeElement> spec, AtomicBoolean stopFlag, boolean postfix) { File[] children = file.listFiles(); 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); } } }
/** * Process the specified file or directory. If it is a directory, then its contents * (but not the directory itself) will be checked with {@link #isAllowed(FileTreeElement, Spec)} and notified to * the listener. If it is a file, the file will be checked and notified. */ public void visitFrom(FileVisitor visitor, File fileOrDirectory, RelativePath path) { AtomicBoolean stopFlag = new AtomicBoolean(); Spec<FileTreeElement> spec = patternSet.getAsSpec(); if (fileOrDirectory.exists()) { if (fileOrDirectory.isFile()) { processSingleFile(fileOrDirectory, visitor, spec, stopFlag); } else { walkDir(fileOrDirectory, path, visitor, spec, stopFlag); } } else { LOGGER.info("file or directory '{}', not found", fileOrDirectory); } }
private void processSingleFile(File file, FileVisitor visitor, Spec<FileTreeElement> spec, AtomicBoolean stopFlag) { RelativePath path = new RelativePath(true, file.getName()); FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem, false); if (isAllowed(details, spec)) { visitor.visitFile(details); } }
public RelativePath getRelativePath() { if (relativePath == null) { RelativePath path = fileDetails.getRelativePath(); relativePath = specResolver.getDestPath().append(path.isFile(), path.getSegments()); } return relativePath; }
public CopySpec filesNotMatching(Iterable<String> patterns, Action<? super FileCopyDetails> action) { if (!patterns.iterator().hasNext()) { throw new InvalidUserDataException("must provide at least one pattern to not match"); } List<Spec> matchers = new ArrayList<Spec>(); for (String pattern : patterns) { matchers.add(PatternMatcherFactory.getPatternMatcher(true, isCaseSensitive(), pattern)); } Spec unionMatcher = Specs.union(matchers.toArray(new Spec[matchers.size()])); return eachFile(new MatchingCopyAction(Specs.<RelativePath>negate(unionMatcher), action)); }
public void execute(FileCopyDetails fileCopyDetails) { RelativePath path = fileCopyDetails.getRelativePath(); String newName = transformer.transform(path.getLastName()); if (newName != null) { path = path.replaceLastName(newName); fileCopyDetails.setRelativePath(path); } }
public WorkResult execute(final CopyActionProcessingStream stream) { final Set<RelativePath> visitedFiles = new HashSet<RelativePath>(); return delegate.execute(new CopyActionProcessingStream() { public void process(final CopyActionProcessingStreamAction action) { stream.process(new CopyActionProcessingStreamAction() { public void processFile(FileCopyDetailsInternal details) { if (!details.isDirectory()) { DuplicatesStrategy strategy = details.getDuplicatesStrategy(); if (!visitedFiles.add(details.getRelativePath())) { if (strategy == DuplicatesStrategy.EXCLUDE) { return; } else if (strategy == DuplicatesStrategy.FAIL) { throw new DuplicateFileCopyingException(String.format("Encountered duplicate path \"%s\" during copy operation configured with DuplicatesStrategy.FAIL", details.getRelativePath())); } else if (strategy == DuplicatesStrategy.WARN) { LOGGER.warn("Encountered duplicate path \"{}\" during copy operation configured with DuplicatesStrategy.WARN", details.getRelativePath()); } } } action.processFile(details); } }); } }); }
private SyncCopyActionDecoratorFileVisitor(Set<RelativePath> visited, PatternFilterable preserveSpec) { this.visited = visited; PatternSet preserveSet = new PatternSet(); if (preserveSpec != null) { preserveSet.include(preserveSpec.getIncludes()); preserveSet.exclude(preserveSpec.getExcludes()); } this.preserveSet = preserveSet; this.preserveSpec = preserveSet.getAsSpec(); }
private void maybeDelete(FileVisitDetails fileDetails, boolean isDir) { RelativePath path = fileDetails.getRelativePath(); if (!visited.contains(path)) { if (preserveSet.isEmpty() || !preserveSpec.isSatisfiedBy(fileDetails)) { if (isDir) { GFileUtils.deleteDirectory(fileDetails.getFile()); } else { GFileUtils.deleteQuietly(fileDetails.getFile()); } didWork = true; } } }
private Spec<RelativePath> getAsExcludeSpec(boolean caseSensitive) { Collection<String> allExcludes = new LinkedHashSet<String>(myFilePattern.excludes); List<Spec<RelativePath>> matchers = new ArrayList<Spec<RelativePath>>(); for (String exclude : allExcludes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(false, caseSensitive, exclude); matchers.add(patternMatcher); } return Specs.or(false, matchers); }
private Spec<RelativePath> getAsIncludeSpec(boolean caseSensitive) { List<Spec<RelativePath>> matchers = new ArrayList<Spec<RelativePath>>(); for (String include : myFilePattern.includes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(true, caseSensitive, include); matchers.add(patternMatcher); } return Specs.or(true, matchers); }
public File transform(RelativePath relativePath) { String sourceFileName = relativePath.getLastName(); String destinationFileNameBase = sourceFileName; if (sourceFileName.endsWith(".coffee")) { destinationFileNameBase = sourceFileName.substring(0, sourceFileName.length() - 7); } String destinationFileName = String.format("%s.js", destinationFileNameBase); RelativePath destinationRelativePath = relativePath.replaceLastName(destinationFileName); return new File(destination, destinationRelativePath.getPathString()); }
public Spec<FileTreeElement> getAsIncludeSpec() { List<Spec<FileTreeElement>> matchers = Lists.newArrayList(); for (String include : includes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(true, caseSensitive, include); matchers.add(new RelativePathSpec(patternMatcher)); } matchers.addAll(includeSpecs); return new OrSpec<FileTreeElement>(matchers); }
public Spec<FileTreeElement> getAsExcludeSpec() { Collection<String> allExcludes = Sets.newLinkedHashSet(excludes); Collections.addAll(allExcludes, DirectoryScanner.getDefaultExcludes()); List<Spec<FileTreeElement>> matchers = Lists.newArrayList(); for (String exclude : allExcludes) { Spec<RelativePath> patternMatcher = PatternMatcherFactory.getPatternMatcher(false, caseSensitive, exclude); matchers.add(new RelativePathSpec(patternMatcher)); } matchers.addAll(excludeSpecs); return new OrSpec<FileTreeElement>(matchers); }
public FileVisitDetailsImpl(RelativePath path, Action<OutputStream> generator, AtomicBoolean stopFlag, Chmod chmod) { super(chmod); this.path = path; this.generator = generator; this.stopFlag = stopFlag; // round to nearest second lastModified = System.currentTimeMillis() / 1000 * 1000; }
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(); } }
private void maybeDelete(FileVisitDetails fileDetails, boolean isDir) { RelativePath path = fileDetails.getRelativePath(); if (!visited.contains(path)) { if (isDir) { GFileUtils.deleteDirectory(fileDetails.getFile()); } else { GFileUtils.deleteQuietly(fileDetails.getFile()); } didWork = true; } }