Skip to content

Commit

Permalink
Change CompositeFileCollection to visit the collection's children d…
Browse files Browse the repository at this point in the history
…irectly rather than first collecting the children into a list.
  • Loading branch information
adammurdoch committed Jul 3, 2020
1 parent 289460d commit 221893b
Show file tree
Hide file tree
Showing 63 changed files with 865 additions and 1,278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.gradle.api.internal.artifacts;

import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.file.UnionFileCollection;
import org.gradle.internal.graph.CachingDirectedGraphWalker;
import org.gradle.internal.graph.DirectedGraph;
Expand All @@ -28,7 +29,7 @@

public class CachingDependencyResolveContext implements DependencyResolveContext {
private final List<Object> queue = new ArrayList<Object>();
private final CachingDirectedGraphWalker<Object, FileCollection> walker = new CachingDirectedGraphWalker<Object, FileCollection>(new DependencyGraph());
private final CachingDirectedGraphWalker<Object, FileCollectionInternal> walker = new CachingDirectedGraphWalker<Object, FileCollectionInternal>(new DependencyGraph());
private final boolean transitive;
private final Map<String, String> attributes;

Expand Down Expand Up @@ -61,11 +62,11 @@ public void add(Object dependency) {
queue.add(dependency);
}

private class DependencyGraph implements DirectedGraph<Object, FileCollection> {
private class DependencyGraph implements DirectedGraph<Object, FileCollectionInternal> {
@Override
public void getNodeValues(Object node, Collection<? super FileCollection> values, Collection<? super Object> connectedNodes) {
if (node instanceof FileCollection) {
FileCollection fileCollection = (FileCollection) node;
public void getNodeValues(Object node, Collection<? super FileCollectionInternal> values, Collection<? super Object> connectedNodes) {
if (node instanceof FileCollectionInternal) {
FileCollectionInternal fileCollection = (FileCollectionInternal) node;
values.add(fileCollection);
} else if (node instanceof ResolvableDependency) {
ResolvableDependency resolvableDependency = (ResolvableDependency) node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

import com.google.common.collect.ImmutableSet;
import org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection;
import org.gradle.api.internal.file.collections.DefaultFileCollectionResolveContext;
import org.gradle.api.internal.file.collections.FileCollectionResolveContext;
import org.gradle.api.internal.file.collections.FileCollectionAdapter;
import org.gradle.api.internal.file.collections.ListBackedFileSet;
import org.gradle.api.internal.file.collections.MinimalFileSet;
import org.gradle.api.internal.provider.PropertyHost;
import org.gradle.api.internal.tasks.TaskDependencyFactory;
import org.gradle.api.internal.tasks.properties.LifecycleAwareValue;
Expand All @@ -30,6 +28,7 @@
import org.gradle.internal.file.PathToFileResolver;

import java.io.File;
import java.util.function.Consumer;

/**
* A {@link org.gradle.api.file.ConfigurableFileCollection} that can be used as a task input property. Caches the matching set of files during task execution, and discards the result after task execution.
Expand All @@ -39,28 +38,24 @@
*/
public class CachingTaskInputFileCollection extends DefaultConfigurableFileCollection implements LifecycleAwareValue {
private boolean canCache;
private MinimalFileSet cachedValue;
private FileCollectionInternal cachedValue;

// TODO - display name
public CachingTaskInputFileCollection(PathToFileResolver fileResolver, Factory<PatternSet> patternSetFactory, TaskDependencyFactory taskDependencyFactory, PropertyHost propertyHost) {
super(null, fileResolver, taskDependencyFactory, patternSetFactory, propertyHost);
}

@Override
public void visitContents(FileCollectionResolveContext context) {
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
if (canCache) {
if (cachedValue == null) {
DefaultFileCollectionResolveContext nested = new DefaultFileCollectionResolveContext(patternSetFactory);
super.visitContents(nested);
ImmutableSet.Builder<File> files = ImmutableSet.builder();
for (FileCollectionInternal fileCollection : nested.resolveAsFileCollections()) {
files.addAll(fileCollection);
}
this.cachedValue = new ListBackedFileSet(files.build());
super.visitChildren(files::addAll);
this.cachedValue = new FileCollectionAdapter(new ListBackedFileSet(files.build()), patternSetFactory);
}
context.add(cachedValue);
visitor.accept(cachedValue);
} else {
super.visitContents(context);
super.visitChildren(visitor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DeleteSpec;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.file.archive.TarFileTree;
import org.gradle.api.internal.file.archive.ZipFileTree;
import org.gradle.api.internal.file.collections.DirectoryFileTreeFactory;
Expand Down Expand Up @@ -152,19 +151,19 @@ public ConfigurableFileTree fileTree(Map<String, ?> args) {
}

@Override
public FileTree zipTree(Object zipPath) {
public FileTreeInternal zipTree(Object zipPath) {
return new FileTreeAdapter(new ZipFileTree(file(zipPath), getExpandDir(), fileSystem, directoryFileTreeFactory, fileHasher), patternSetFactory);
}

@Override
public FileTree tarTree(Object tarPath) {
public FileTreeInternal tarTree(Object tarPath) {
File tarFile = null;
ReadableResourceInternal resource;
if (tarPath instanceof ReadableResourceInternal) {
resource = (ReadableResourceInternal) tarPath;
} else if (tarPath instanceof ReadableResource) {
// custom type
resource = new UnknownBackingFileReadableResource((ReadableResource)tarPath);
resource = new UnknownBackingFileReadableResource((ReadableResource) tarPath);
} else {
tarFile = file(tarPath);
resource = new LocalResourceAdapter(new LocalFileStandInExternalResource(tarFile, fileSystem));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.gradle.api.internal.file.collections.DirectoryFileTree;
import org.gradle.api.internal.file.collections.DirectoryFileTreeFactory;
import org.gradle.api.internal.file.collections.FileCollectionAdapter;
import org.gradle.api.internal.file.collections.FileCollectionResolveContext;
import org.gradle.api.internal.file.collections.MinimalFileSet;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;
import org.gradle.api.model.ObjectFactory;
Expand All @@ -48,6 +47,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

public class DefaultSourceDirectorySet extends CompositeFileTree implements SourceDirectorySet {
Expand Down Expand Up @@ -210,20 +210,20 @@ public <T extends Task> void compiledBy(TaskProvider<T> taskProvider, Function<T
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()) {
for (DirectoryTree tree : getSourceTrees()) {
if (!trees.containsKey(tree.getDir())) {
trees.put(tree.getDir(), tree);
}
}
return new LinkedHashSet<DirectoryTree>(trees.values());
return new LinkedHashSet<>(trees.values());
}

private Set<DirectoryTree> doGetSrcDirTrees() {
Set<DirectoryTree> result = new LinkedHashSet<DirectoryTree>();
protected Set<DirectoryFileTree> getSourceTrees() {
Set<DirectoryFileTree> result = new LinkedHashSet<>();
for (Object path : source) {
if (path instanceof SourceDirectorySet) {
SourceDirectorySet nested = (SourceDirectorySet) path;
result.addAll(nested.getSrcDirTrees());
if (path instanceof DefaultSourceDirectorySet) {
DefaultSourceDirectorySet nested = (DefaultSourceDirectorySet) path;
result.addAll(nested.getSourceTrees());
} else {
for (File srcDir : fileCollectionFactory.resolving(path)) {
if (srcDir.exists() && !srcDir.isDirectory()) {
Expand All @@ -248,9 +248,9 @@ public void visitDependencies(TaskDependencyResolveContext context) {
}

@Override
public void visitContents(FileCollectionResolveContext context) {
for (DirectoryTree directoryTree : doGetSrcDirTrees()) {
context.add(((DirectoryFileTree) directoryTree).filter(filter));
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
for (DirectoryFileTree directoryTree : getSourceTrees()) {
visitor.accept(fileCollectionFactory.treeOf(directoryTree.filter(filter)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DeleteSpec;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.resources.ResourceHandler;
import org.gradle.api.tasks.WorkResult;
import org.gradle.api.tasks.util.PatternSet;
Expand Down Expand Up @@ -59,9 +58,9 @@ public interface FileOperations {

ConfigurableFileTree fileTree(Map<String, ?> args);

FileTree zipTree(Object zipPath);
FileTreeInternal zipTree(Object zipPath);

FileTree tarTree(Object tarPath);
FileTreeInternal tarTree(Object tarPath);

CopySpec copySpec();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DeleteSpec;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.api.internal.DynamicObjectAware;
Expand All @@ -56,6 +55,7 @@
import org.gradle.api.internal.file.DefaultProjectLayout;
import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.file.FileTreeInternal;
import org.gradle.api.internal.initialization.ClassLoaderScope;
import org.gradle.api.internal.initialization.ScriptHandlerFactory;
import org.gradle.api.internal.initialization.ScriptHandlerInternal;
Expand Down Expand Up @@ -936,12 +936,12 @@ public ConfigurableFileTree fileTree(Map<String, ?> args) {
}

@Override
public FileTree zipTree(Object zipPath) {
public FileTreeInternal zipTree(Object zipPath) {
return getFileOperations().zipTree(zipPath);
}

@Override
public FileTree tarTree(Object tarPath) {
public FileTreeInternal tarTree(Object tarPath) {
return getFileOperations().tarTree(tarPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.internal.file.CompositeFileCollection;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.collections.FileCollectionResolveContext;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.tasks.properties.FileParameterUtils;
import org.gradle.api.internal.tasks.properties.GetInputFilesVisitor;
import org.gradle.api.internal.tasks.properties.GetInputPropertiesVisitor;
Expand All @@ -41,6 +41,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

@NonNullApi
public class DefaultTaskInputs implements TaskInputsInternal {
Expand Down Expand Up @@ -226,13 +227,13 @@ public String getDisplayName() {
}

@Override
public void visitContents(final FileCollectionResolveContext context) {
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
TaskPropertyUtils.visitProperties(propertyWalker, task, new PropertyVisitor.Adapter() {
@Override
public void visitInputFileProperty(final String propertyName, boolean optional, boolean skipWhenEmpty, boolean incremental, @Nullable Class<? extends FileNormalizer> fileNormalizer, PropertyValue value, InputFilePropertyType filePropertyType) {
if (!TaskInputUnionFileCollection.this.skipWhenEmptyOnly || skipWhenEmpty) {
FileCollection actualValue = FileParameterUtils.resolveInputFileValue(fileCollectionFactory, filePropertyType, value);
context.add(new PropertyFileCollection(task.toString(), propertyName, "input", actualValue));
FileCollectionInternal actualValue = FileParameterUtils.resolveInputFileValue(fileCollectionFactory, filePropertyType, value);
visitor.accept(new PropertyFileCollection(task.toString(), propertyName, "input", actualValue));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.gradle.api.internal.TaskOutputsInternal;
import org.gradle.api.internal.file.CompositeFileCollection;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.collections.FileCollectionResolveContext;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.tasks.execution.SelfDescribingSpec;
import org.gradle.api.internal.tasks.properties.GetOutputFilesVisitor;
import org.gradle.api.internal.tasks.properties.OutputFilePropertySpec;
Expand All @@ -45,6 +45,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

@NonNullApi
public class DefaultTaskOutputs implements TaskOutputsInternal {
Expand Down Expand Up @@ -253,9 +254,9 @@ public String getDisplayName() {
}

@Override
public void visitContents(FileCollectionResolveContext context) {
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
for (OutputFilePropertySpec propertySpec : getFileProperties()) {
context.add(propertySpec.getPropertyFiles());
visitor.accept(propertySpec.getPropertyFiles());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@

package org.gradle.api.internal.tasks;

import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.CompositeFileCollection;
import org.gradle.api.internal.file.collections.FileCollectionResolveContext;
import org.gradle.api.internal.file.FileCollectionInternal;

import java.util.function.Consumer;

public class PropertyFileCollection extends CompositeFileCollection {
private final String ownerDisplayName;
private final String type;
private final String propertyName;
private final FileCollection files;
private final FileCollectionInternal files;
private String displayName;

public PropertyFileCollection(String ownerDisplayName, String propertyName, String type, FileCollection files) {
public PropertyFileCollection(String ownerDisplayName, String propertyName, String type, FileCollectionInternal files) {
this.ownerDisplayName = ownerDisplayName;
this.type = type;
this.propertyName = propertyName;
Expand All @@ -43,7 +44,7 @@ public String getDisplayName() {
}

@Override
public void visitContents(FileCollectionResolveContext context) {
context.add(files);
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
visitor.accept(files);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.internal.TaskOutputsInternal;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.FileCollectionInternal;
import org.gradle.api.internal.file.FileOperations;
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection;
import org.gradle.api.internal.project.taskfactory.IncrementalInputsTaskAction;
Expand Down Expand Up @@ -555,9 +556,9 @@ private void executeAction(String actionDisplayName, TaskInternal task, InputCha
@Override
public BuildOperationDescriptor.Builder description() {
return BuildOperationDescriptor
.displayName(actionDisplayName + " for " + task.getIdentityPath().getPath())
.name(actionDisplayName)
.details(ExecuteTaskActionBuildOperationType.DETAILS_INSTANCE);
.displayName(actionDisplayName + " for " + task.getIdentityPath().getPath())
.name(actionDisplayName)
.details(ExecuteTaskActionBuildOperationType.DETAILS_INSTANCE);
}

@Override
Expand Down Expand Up @@ -623,7 +624,7 @@ public PreviousOutputFileCollection(TaskInternal task, AfterPreviousExecutionSta
}

@Override
public FileCollection createDelegate() {
public FileCollectionInternal createDelegate() {
ImmutableCollection<FileCollectionFingerprint> outputFingerprints = previousExecution.getOutputFileProperties().values();
Set<File> outputs = new HashSet<>();
for (FileCollectionFingerprint fileCollectionFingerprint : outputFingerprints) {
Expand Down
Loading

0 comments on commit 221893b

Please sign in to comment.