Search in sources :

Example 1 with FileCollectionStructureVisitor

use of org.gradle.api.internal.file.FileCollectionStructureVisitor in project gradle by gradle.

the class FileParameterUtils method resolveCompositeOutputFilePropertySpecs.

private static void resolveCompositeOutputFilePropertySpecs(final String ownerDisplayName, final String propertyName, Object unpackedValue, final TreeType outputType, FileCollectionFactory fileCollectionFactory, Consumer<OutputFilePropertySpec> consumer) {
    if (unpackedValue instanceof Map) {
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) unpackedValue).entrySet()) {
            Object key = entry.getKey();
            if (key == null) {
                throw new IllegalArgumentException(String.format("Mapped output property '%s' has null key", propertyName));
            }
            String id = key.toString();
            FileCollectionInternal outputFiles = fileCollectionFactory.resolving(entry.getValue());
            consumer.accept(new DefaultCacheableOutputFilePropertySpec(propertyName, "." + id, outputFiles, outputType));
        }
    } else {
        FileCollectionInternal outputFileCollection = fileCollectionFactory.resolving(unpackedValue);
        AtomicInteger index = new AtomicInteger(0);
        outputFileCollection.visitStructure(new FileCollectionStructureVisitor() {

            @Override
            public void visitCollection(FileCollectionInternal.Source source, Iterable<File> contents) {
                for (File content : contents) {
                    FileCollectionInternal outputFiles = fileCollectionFactory.fixed(content);
                    consumer.accept(new DefaultCacheableOutputFilePropertySpec(propertyName, "$" + index.incrementAndGet(), outputFiles, outputType));
                }
            }

            @Override
            public void visitGenericFileTree(FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                failOnInvalidOutputType(fileTree);
            }

            @Override
            public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                failOnInvalidOutputType(fileTree);
            }

            @Override
            public void visitFileTree(File root, PatternSet patterns, FileTreeInternal fileTree) {
                // We could support an unfiltered DirectoryFileTree here as a cacheable root,
                // but because @OutputDirectory also doesn't support it we choose not to.
                consumer.accept(new DirectoryTreeOutputFilePropertySpec(propertyName + "$" + index.incrementAndGet(), new PropertyFileCollection(ownerDisplayName, propertyName, "output", fileTree), root));
            }
        });
    }
}
Also used : FileTreeInternal(org.gradle.api.internal.file.FileTreeInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileCollectionInternal(org.gradle.api.internal.file.FileCollectionInternal) PropertyFileCollection(org.gradle.api.internal.tasks.PropertyFileCollection) FileCollectionStructureVisitor(org.gradle.api.internal.file.FileCollectionStructureVisitor) FileSystemMirroringFileTree(org.gradle.api.internal.file.collections.FileSystemMirroringFileTree) Map(java.util.Map) File(java.io.File) PatternSet(org.gradle.api.tasks.util.PatternSet)

Example 2 with FileCollectionStructureVisitor

use of org.gradle.api.internal.file.FileCollectionStructureVisitor in project gradle by gradle.

the class FileCollectionMatchers method sameCollection.

@Factory
public static <T extends FileCollection> Matcher<T> sameCollection(final FileCollection expected) {
    return new BaseMatcher<T>() {

        @Override
        public boolean matches(Object o) {
            FileCollection actual = (FileCollection) o;
            List<Object> actualCollections = unpack(actual);
            List<Object> expectedCollections = unpack(expected);
            boolean equals = actualCollections.equals(expectedCollections);
            if (!equals) {
                System.out.println("expected: " + expectedCollections);
                System.out.println("actual: " + actualCollections);
            }
            return equals;
        }

        private List<Object> unpack(FileCollection expected) {
            if (expected instanceof FileCollectionInternal) {
                FileCollectionInternal collection = (CompositeFileCollection) expected;
                List<Object> collections = new ArrayList<>();
                collection.visitStructure(new FileCollectionStructureVisitor() {

                    @Override
                    public void visitCollection(FileCollectionInternal.Source source, Iterable<File> contents) {
                        for (File content : contents) {
                            collections.add(content);
                        }
                    }

                    @Override
                    public void visitGenericFileTree(FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                        collections.add(fileTree);
                    }

                    @Override
                    public void visitFileTree(File root, PatternSet patterns, FileTreeInternal fileTree) {
                        collections.add(fileTree);
                    }

                    @Override
                    public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                        collections.add(fileTree);
                    }
                });
                return collections;
            }
            throw new RuntimeException("Cannot get children of " + expected);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("same file collection as ").appendValue(expected);
        }
    };
}
Also used : Description(org.hamcrest.Description) ArrayList(java.util.ArrayList) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) FileTreeInternal(org.gradle.api.internal.file.FileTreeInternal) BaseMatcher(org.hamcrest.BaseMatcher) FileCollectionInternal(org.gradle.api.internal.file.FileCollectionInternal) CompositeFileCollection(org.gradle.api.internal.file.CompositeFileCollection) FileCollectionStructureVisitor(org.gradle.api.internal.file.FileCollectionStructureVisitor) FileSystemMirroringFileTree(org.gradle.api.internal.file.collections.FileSystemMirroringFileTree) File(java.io.File) PatternSet(org.gradle.api.tasks.util.PatternSet) Factory(org.hamcrest.Factory)

Example 3 with FileCollectionStructureVisitor

use of org.gradle.api.internal.file.FileCollectionStructureVisitor in project gradle by gradle.

the class ArtifactSetToFileCollectionFactory method asFileCollection.

/**
 * Presents the contents of the given artifacts as a partial {@link FileCollectionInternal} implementation.
 *
 * <p>This produces only a minimal implementation to use for artifact sets loaded from the configuration cache
 * Over time, this should be merged with the FileCollection implementation in DefaultConfiguration
 */
public FileCollectionInternal asFileCollection(ResolvedArtifactSet artifacts) {
    return new AbstractFileCollection() {

        @Override
        public String getDisplayName() {
            return "files";
        }

        @Override
        protected void visitContents(FileCollectionStructureVisitor visitor) {
            ResolvedFileCollectionVisitor collectingVisitor = new ResolvedFileCollectionVisitor(visitor);
            ParallelResolveArtifactSet.wrap(artifacts, buildOperationExecutor).visit(collectingVisitor);
            if (!collectingVisitor.getFailures().isEmpty()) {
                throw UncheckedException.throwAsUncheckedException(collectingVisitor.getFailures().iterator().next());
            }
        }
    };
}
Also used : AbstractFileCollection(org.gradle.api.internal.file.AbstractFileCollection) FileCollectionStructureVisitor(org.gradle.api.internal.file.FileCollectionStructureVisitor) ResolvedFileCollectionVisitor(org.gradle.api.internal.artifacts.ivyservice.ResolvedFileCollectionVisitor)

Example 4 with FileCollectionStructureVisitor

use of org.gradle.api.internal.file.FileCollectionStructureVisitor in project gradle by gradle.

the class LocalTaskNodeExecutor method detectMissingDependencies.

private void detectMissingDependencies(LocalTaskNode node, boolean historyMaintained, ExecutionNodeAccessHierarchies.InputNodeAccessHierarchy inputHierarchy, TypeValidationContext validationContext) {
    for (String outputPath : node.getMutationInfo().outputPaths) {
        inputHierarchy.getNodesAccessing(outputPath).stream().filter(consumerNode -> hasNoSpecifiedOrder(node, consumerNode)).filter(LocalTaskNodeExecutor::isEnabled).forEach(consumerWithoutDependency -> collectValidationProblem(node, consumerWithoutDependency, validationContext, outputPath));
    }
    Set<String> taskInputs = new LinkedHashSet<>();
    Set<FilteredTree> filteredFileTreeTaskInputs = new LinkedHashSet<>();
    node.getTaskProperties().getInputFileProperties().forEach(spec -> {
        try {
            spec.getPropertyFiles().visitStructure(new FileCollectionStructureVisitor() {

                @Override
                public void visitCollection(FileCollectionInternal.Source source, Iterable<File> contents) {
                    contents.forEach(location -> taskInputs.add(location.getAbsolutePath()));
                }

                @Override
                public void visitGenericFileTree(FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                    fileTree.forEach(location -> taskInputs.add(location.getAbsolutePath()));
                }

                @Override
                public void visitFileTree(File root, PatternSet patterns, FileTreeInternal fileTree) {
                    if (patterns.isEmpty()) {
                        taskInputs.add(root.getAbsolutePath());
                    } else {
                        filteredFileTreeTaskInputs.add(new FilteredTree(root.getAbsolutePath(), patterns));
                    }
                }

                @Override
                public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
                    taskInputs.add(file.getAbsolutePath());
                }
            });
        } catch (Exception e) {
            if (historyMaintained) {
                // We would later try to snapshot the inputs anyway, no need to suppress the exception
                throw e;
            } else {
                validationContext.visitPropertyProblem(problem -> problem.withId(ValidationProblemId.UNRESOLVABLE_INPUT).forProperty(spec.getPropertyName()).reportAs(Severity.WARNING).withDescription(() -> String.format("cannot be resolved:%n%s%n", TextUtil.indent(e.getMessage(), "  "))).happensBecause("An input file collection couldn't be resolved, making it impossible to determine task inputs").addPossibleSolution("Consider using Task.dependsOn instead").documentedAt("validation_problems", "unresolvable_input"));
            }
        }
    });
    inputHierarchy.recordNodeAccessingLocations(node, taskInputs);
    for (String locationConsumedByThisTask : taskInputs) {
        collectValidationProblemsForConsumer(node, validationContext, locationConsumedByThisTask, outputHierarchy.getNodesAccessing(locationConsumedByThisTask));
    }
    for (FilteredTree filteredFileTreeInput : filteredFileTreeTaskInputs) {
        Spec<FileTreeElement> spec = filteredFileTreeInput.getPatterns().getAsSpec();
        inputHierarchy.recordNodeAccessingFileTree(node, filteredFileTreeInput.getRoot(), spec);
        collectValidationProblemsForConsumer(node, validationContext, filteredFileTreeInput.getRoot(), outputHierarchy.getNodesAccessing(filteredFileTreeInput.getRoot(), spec));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Deque(java.util.Deque) Severity(org.gradle.internal.reflect.validation.Severity) DefaultTaskExecutionContext(org.gradle.api.internal.tasks.execution.DefaultTaskExecutionContext) HashSet(java.util.HashSet) TaskExecutionContext(org.gradle.api.internal.tasks.TaskExecutionContext) NodeExecutionContext(org.gradle.api.internal.tasks.NodeExecutionContext) TextUtil(org.gradle.util.internal.TextUtil) FileTreeElement(org.gradle.api.file.FileTreeElement) FileCollectionInternal(org.gradle.api.internal.file.FileCollectionInternal) LinkedHashSet(java.util.LinkedHashSet) TaskInternal(org.gradle.api.internal.TaskInternal) FileCollectionStructureVisitor(org.gradle.api.internal.file.FileCollectionStructureVisitor) FileSystemMirroringFileTree(org.gradle.api.internal.file.collections.FileSystemMirroringFileTree) PatternSet(org.gradle.api.tasks.util.PatternSet) Collection(java.util.Collection) Set(java.util.Set) TypeValidationContext(org.gradle.internal.reflect.validation.TypeValidationContext) TaskStateInternal(org.gradle.api.internal.tasks.TaskStateInternal) File(java.io.File) Objects(java.util.Objects) Spec(org.gradle.api.specs.Spec) TaskExecuter(org.gradle.api.internal.tasks.TaskExecuter) FileTreeInternal(org.gradle.api.internal.file.FileTreeInternal) Queue(java.util.Queue) ValidationProblemId(org.gradle.internal.reflect.problems.ValidationProblemId) ArrayDeque(java.util.ArrayDeque) FileTreeElement(org.gradle.api.file.FileTreeElement) FileTreeInternal(org.gradle.api.internal.file.FileTreeInternal) FileCollectionInternal(org.gradle.api.internal.file.FileCollectionInternal) FileCollectionStructureVisitor(org.gradle.api.internal.file.FileCollectionStructureVisitor) FileSystemMirroringFileTree(org.gradle.api.internal.file.collections.FileSystemMirroringFileTree) File(java.io.File) PatternSet(org.gradle.api.tasks.util.PatternSet)

Aggregations

FileCollectionStructureVisitor (org.gradle.api.internal.file.FileCollectionStructureVisitor)4 File (java.io.File)3 FileCollectionInternal (org.gradle.api.internal.file.FileCollectionInternal)3 FileTreeInternal (org.gradle.api.internal.file.FileTreeInternal)3 FileSystemMirroringFileTree (org.gradle.api.internal.file.collections.FileSystemMirroringFileTree)3 PatternSet (org.gradle.api.tasks.util.PatternSet)3 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Deque (java.util.Deque)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Queue (java.util.Queue)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FileTreeElement (org.gradle.api.file.FileTreeElement)1 TaskInternal (org.gradle.api.internal.TaskInternal)1 ResolvedFileCollectionVisitor (org.gradle.api.internal.artifacts.ivyservice.ResolvedFileCollectionVisitor)1