Search in sources :

Example 6 with RelativePath

use of org.gradle.api.file.RelativePath in project gradle by gradle.

the class SyncCopyActionDecorator method execute.

@Override
public WorkResult execute(final CopyActionProcessingStream stream) {
    final Set<RelativePath> visited = new HashSet<>();
    WorkResult didWork = delegate.execute(action -> stream.process(details -> {
        visited.add(details.getRelativePath());
        action.processFile(details);
    }));
    SyncCopyActionDecoratorFileVisitor fileVisitor = new SyncCopyActionDecoratorFileVisitor(visited, preserveSpec, deleter);
    MinimalFileTree walker = directoryFileTreeFactory.create(baseDestDir).postfix();
    walker.visit(fileVisitor);
    visited.clear();
    return WorkResults.didWork(didWork.getDidWork() || fileVisitor.didWork);
}
Also used : PatternFilterable(org.gradle.api.tasks.util.PatternFilterable) FileVisitDetails(org.gradle.api.file.FileVisitDetails) PatternSet(org.gradle.api.tasks.util.PatternSet) Set(java.util.Set) IOException(java.io.IOException) WorkResult(org.gradle.api.tasks.WorkResult) File(java.io.File) WorkResults(org.gradle.api.tasks.WorkResults) UncheckedIOException(java.io.UncheckedIOException) HashSet(java.util.HashSet) Deleter(org.gradle.internal.file.Deleter) RelativePath(org.gradle.api.file.RelativePath) Spec(org.gradle.api.specs.Spec) DirectoryFileTreeFactory(org.gradle.api.internal.file.collections.DirectoryFileTreeFactory) FileTreeElement(org.gradle.api.file.FileTreeElement) MinimalFileTree(org.gradle.api.internal.file.collections.MinimalFileTree) FileVisitor(org.gradle.api.file.FileVisitor) Nullable(javax.annotation.Nullable) RelativePath(org.gradle.api.file.RelativePath) WorkResult(org.gradle.api.tasks.WorkResult) MinimalFileTree(org.gradle.api.internal.file.collections.MinimalFileTree) HashSet(java.util.HashSet)

Example 7 with RelativePath

use of org.gradle.api.file.RelativePath in project gradle by gradle.

the class DefaultFileCopyDetails method getRelativePath.

@Override
public RelativePath getRelativePath() {
    if (relativePath == null) {
        RelativePath path = fileDetails.getRelativePath();
        relativePath = specResolver.getDestPath().append(path.isFile(), path.getSegments());
    }
    return relativePath;
}
Also used : RelativePath(org.gradle.api.file.RelativePath)

Example 8 with RelativePath

use of org.gradle.api.file.RelativePath in project gradle by gradle.

the class InstrumentingClasspathFileTransformer method visitEntries.

private void visitEntries(File source, ClasspathBuilder.EntryBuilder builder) throws IOException, FileException {
    classpathWalker.visit(source, entry -> {
        try {
            if (entry.getName().endsWith(".class")) {
                ClassReader reader = new ClassReader(entry.getContent());
                ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                Pair<RelativePath, ClassVisitor> chain = transform.apply(entry, classWriter);
                reader.accept(chain.right, 0);
                byte[] bytes = classWriter.toByteArray();
                builder.put(chain.left.getPathString(), bytes);
            } else {
                builder.put(entry.getName(), entry.getContent());
            }
        } catch (Throwable e) {
            throw new IOException("Failed to process the entry '" + entry.getName() + "' from '" + source + "'", e);
        }
    });
}
Also used : RelativePath(org.gradle.api.file.RelativePath) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ClassWriter(org.objectweb.asm.ClassWriter)

Example 9 with RelativePath

use of org.gradle.api.file.RelativePath in project gradle by gradle.

the class DirectoryTrees method contains.

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));
}
Also used : RelativePath(org.gradle.api.file.RelativePath) DefaultFileTreeElement(org.gradle.api.internal.file.DefaultFileTreeElement)

Example 10 with RelativePath

use of org.gradle.api.file.RelativePath in project gradle by gradle.

the class SingleIncludePatternFileTree method doVisit.

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, fileSystem);
        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();
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) PatternStep(org.gradle.api.internal.file.pattern.PatternStep) GradleException(org.gradle.api.GradleException) PatternSet(org.gradle.api.tasks.util.PatternSet) File(java.io.File)

Aggregations

RelativePath (org.gradle.api.file.RelativePath)25 File (java.io.File)8 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 FileVisitDetails (org.gradle.api.file.FileVisitDetails)5 DefaultFileVisitDetails (org.gradle.api.internal.file.DefaultFileVisitDetails)4 GradleException (org.gradle.api.GradleException)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Spec (org.gradle.api.specs.Spec)2 PatternSet (org.gradle.api.tasks.util.PatternSet)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1