Search in sources :

Example 11 with RelativePath

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

the class ReproducibleDirectoryWalker method walkDir.

@Override
public void walkDir(File file, RelativePath path, FileVisitor visitor, Spec<? super FileTreeElement> spec, AtomicBoolean stopFlag, boolean postfix) {
    File[] children = getChildren(file);
    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);
        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);
        }
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) FileVisitDetails(org.gradle.api.file.FileVisitDetails) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) DefaultFileVisitDetails(org.gradle.api.internal.file.DefaultFileVisitDetails) File(java.io.File)

Example 12 with RelativePath

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

the class MapFileTree method visit.

public void visit(FileVisitor visitor) {
    AtomicBoolean stopFlag = new AtomicBoolean();
    Visit visit = new Visit(visitor, stopFlag);
    for (Map.Entry<RelativePath, Action<OutputStream>> entry : elements.entrySet()) {
        if (stopFlag.get()) {
            break;
        }
        RelativePath path = entry.getKey();
        Action<OutputStream> generator = entry.getValue();
        visit.visit(path, generator);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Action(org.gradle.api.Action) RelativePath(org.gradle.api.file.RelativePath) OutputStream(java.io.OutputStream) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 13 with RelativePath

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

the class DefaultCopySpec method filesNotMatching.

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));
}
Also used : RelativePath(org.gradle.api.file.RelativePath) InvalidUserDataException(org.gradle.api.InvalidUserDataException) ArrayList(java.util.ArrayList) CopySpec(org.gradle.api.file.CopySpec) CopyProcessingSpec(org.gradle.api.file.CopyProcessingSpec) Spec(org.gradle.api.specs.Spec)

Example 14 with RelativePath

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

the class RelativePathTest method hasWellBehavedEqualsAndHashCode.

@Test
public void hasWellBehavedEqualsAndHashCode() {
    assertThat(new RelativePath(true), strictlyEqual(new RelativePath(true)));
    assertThat(new RelativePath(true, "one"), strictlyEqual(new RelativePath(true, "one")));
    assertThat(new RelativePath(false, "one", "two"), strictlyEqual(new RelativePath(false, "one", "two")));
    assertThat(new RelativePath(true, "one"), not(equalTo(new RelativePath(true, "two"))));
    assertThat(new RelativePath(true, "one"), not(equalTo(new RelativePath(true, "one", "two"))));
    assertThat(new RelativePath(true, "one"), not(equalTo(new RelativePath(false, "one"))));
}
Also used : RelativePath(org.gradle.api.file.RelativePath) Test(org.junit.Test)

Example 15 with RelativePath

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

the class RelativePathTest method canParsePathIntoRelativePath.

@Test
public void canParsePathIntoRelativePath() {
    RelativePath path;
    path = RelativePath.parse(true, "one");
    assertPathContains(path, true, "one");
    path = RelativePath.parse(true, "one/two");
    assertPathContains(path, true, "one", "two");
    path = RelativePath.parse(true, "one/two/");
    assertPathContains(path, true, "one", "two");
    path = RelativePath.parse(true, String.format("one%stwo%s", File.separator, File.separator));
    assertPathContains(path, true, "one", "two");
    path = RelativePath.parse(false, "");
    assertPathContains(path, false);
    path = RelativePath.parse(false, "/");
    assertPathContains(path, false);
    path = RelativePath.parse(true, "/one");
    assertPathContains(path, true, "one");
    path = RelativePath.parse(true, "/one/two");
    assertPathContains(path, true, "one", "two");
}
Also used : RelativePath(org.gradle.api.file.RelativePath) Test(org.junit.Test)

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