-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AbstractFileCollection.java
367 lines (321 loc) · 13.5 KB
/
AbstractFileCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.file;
import com.google.common.collect.ImmutableSet;
import groovy.lang.Closure;
import org.gradle.api.file.DirectoryTree;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileSystemLocation;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.FileVisitor;
import org.gradle.api.internal.file.collections.DirectoryFileTree;
import org.gradle.api.internal.file.collections.FileBackedDirectoryFileTree;
import org.gradle.api.internal.file.collections.FileSystemMirroringFileTree;
import org.gradle.api.internal.provider.BuildableBackedProvider;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.internal.tasks.DefaultTaskDependencyFactory;
import org.gradle.api.internal.tasks.TaskDependencyFactory;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;
import org.gradle.api.provider.Provider;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.gradle.api.tasks.TaskDependency;
import org.gradle.api.tasks.util.PatternSet;
import org.gradle.api.tasks.util.internal.PatternSets;
import org.gradle.internal.Cast;
import org.gradle.internal.Factory;
import org.gradle.internal.MutableBoolean;
import org.gradle.internal.deprecation.DocumentedFailure;
import org.gradle.internal.logging.text.TreeFormatter;
import org.gradle.util.internal.GUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public abstract class AbstractFileCollection implements FileCollectionInternal {
protected final TaskDependencyFactory taskDependencyFactory;
protected final Factory<PatternSet> patternSetFactory;
protected AbstractFileCollection(TaskDependencyFactory taskDependencyFactory, Factory<PatternSet> patternSetFactory) {
this.taskDependencyFactory = taskDependencyFactory;
this.patternSetFactory = patternSetFactory;
}
@SuppressWarnings("deprecation")
protected AbstractFileCollection(TaskDependencyFactory taskDependencyFactory) {
this(taskDependencyFactory, PatternSets.getNonCachingPatternSetFactory());
}
public AbstractFileCollection() {
this(DefaultTaskDependencyFactory.withNoAssociatedProject());
}
@Override
public String toString() {
return getDisplayName();
}
/**
* This is final - override {@link #appendContents(TreeFormatter)} instead to add type specific content.
*/
@Override
public final TreeFormatter describeContents(TreeFormatter formatter) {
formatter.node("collection type: ").appendType(getClass()).append(" (id: ").append(String.valueOf(System.identityHashCode(this))).append(")");
formatter.startChildren();
appendContents(formatter);
formatter.endChildren();
return formatter;
}
protected void appendContents(TreeFormatter formatter) {
}
// This is final - override {@link TaskDependencyContainer#visitDependencies} to provide the dependencies instead.
@Override
public final TaskDependency getBuildDependencies() {
assertCanCarryBuildDependencies();
DefaultTaskDependency result = taskDependencyFactory.visitingDependencies(context -> context.add(AbstractFileCollection.this));
result.setToStringProvider(() -> "Dependencies of " + getDisplayName());
return result;
}
protected void assertCanCarryBuildDependencies() {
}
@Override
public void visitDependencies(TaskDependencyResolveContext context) {
// Assume no dependencies
}
@Override
public FileCollectionInternal replace(FileCollectionInternal original, Supplier<FileCollectionInternal> supplier) {
if (original == this) {
return supplier.get();
}
return this;
}
@Override
public Set<File> getFiles() {
// Use a JVM type here, rather than a Guava type, as some plugins serialize this return value and cannot deserialize the result
Set<File> files = new LinkedHashSet<>();
visitContents(new FileCollectionStructureVisitor() {
@Override
public void visitCollection(Source source, Iterable<File> contents) {
for (File content : contents) {
files.add(content);
}
}
private void addTreeContents(FileTreeInternal fileTree) {
// TODO - add some convenient way to visit the files of the tree without collecting them into a set
files.addAll(fileTree.getFiles());
}
@Override
public void visitFileTree(File root, PatternSet patterns, FileTreeInternal fileTree) {
addTreeContents(fileTree);
}
@Override
public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
addTreeContents(fileTree);
}
});
return files;
}
@Override
public File getSingleFile() throws IllegalStateException {
Iterator<File> iterator = iterator();
if (!iterator.hasNext()) {
throw new IllegalStateException(String.format("Expected %s to contain exactly one file, however, it contains no files.", getDisplayName()));
}
File singleFile = iterator.next();
if (iterator.hasNext()) {
throw new IllegalStateException(String.format("Expected %s to contain exactly one file, however, it contains more than one file.", getDisplayName()));
}
return singleFile;
}
@Override
public Iterator<File> iterator() {
return getFiles().iterator();
}
@Override
public String getAsPath() {
showGetAsPathDeprecationWarning();
return GUtil.asPath(this);
}
private void showGetAsPathDeprecationWarning() {
List<String> filesAsPaths = this.getFiles().stream()
.map(File::getPath)
.filter(path -> path.contains(File.pathSeparator))
.collect(Collectors.toList());
if (!filesAsPaths.isEmpty()) {
String displayedFilePaths = filesAsPaths.stream().map(path -> "'" + path + "'").collect(Collectors.joining(","));
throw DocumentedFailure.builder()
.withSummary(String.format(
"Converting files to a classpath string when their paths contain the path separator '%s' is not supported. " +
"The path separator is not a valid element of a file path.", File.pathSeparator))
.withContext(String.format("Problematic paths in '%s' are: %s.",
getDisplayName(),
displayedFilePaths
))
.withAdvice("Add the individual files to the file collection instead.")
.withUpgradeGuideSection(7, "file_collection_to_classpath")
.build();
}
}
@Override
public boolean contains(File file) {
return getFiles().contains(file);
}
@Override
public FileCollection plus(FileCollection collection) {
return new UnionFileCollection(taskDependencyFactory, this, (FileCollectionInternal) collection);
}
@Override
public Provider<Set<FileSystemLocation>> getElements() {
return new BuildableBackedProvider<>(
this,
Cast.uncheckedCast(Set.class),
new FileCollectionElementsFactory(this)
);
}
private static class FileCollectionElementsFactory implements Factory<Set<FileSystemLocation>> {
private final FileCollection fileCollection;
private FileCollectionElementsFactory(FileCollection fileCollection) {
this.fileCollection = fileCollection;
}
@Override
public Set<FileSystemLocation> create() {
// TODO - visit the contents of this collection instead.
// This is just a super simple implementation for now
Set<File> files = fileCollection.getFiles();
ImmutableSet.Builder<FileSystemLocation> builder = ImmutableSet.builderWithExpectedSize(files.size());
for (File file : files) {
builder.add(new DefaultFileSystemLocation(file));
}
return builder.build();
}
}
@Override
public FileCollection minus(final FileCollection collection) {
return new SubtractingFileCollection(this, collection);
}
@Override
public void addToAntBuilder(Object builder, String nodeName, AntType type) {
if (type == AntType.ResourceCollection) {
addAsResourceCollection(builder, nodeName);
} else if (type == AntType.FileSet) {
addAsFileSet(builder, nodeName);
} else {
addAsMatchingTask(builder, nodeName);
}
}
protected void addAsMatchingTask(Object builder, String nodeName) {
new AntFileCollectionMatchingTaskBuilder(getAsFileTrees()).addToAntBuilder(builder, nodeName);
}
protected void addAsFileSet(Object builder, String nodeName) {
new AntFileSetBuilder(getAsFileTrees()).addToAntBuilder(builder, nodeName);
}
protected void addAsResourceCollection(Object builder, String nodeName) {
new AntFileCollectionBuilder(this).addToAntBuilder(builder, nodeName);
}
/**
* Returns this collection as a set of {@link DirectoryFileTree} instance. These are used to map to Ant types.
*/
protected Collection<DirectoryTree> getAsFileTrees() {
List<DirectoryTree> fileTrees = new ArrayList<>();
visitStructure(new FileCollectionStructureVisitor() {
@Override
public void visitCollection(Source source, Iterable<File> contents) {
for (File file : contents) {
if (file.isFile()) {
fileTrees.add(new FileBackedDirectoryFileTree(file));
}
}
}
@Override
public void visitFileTree(File root, PatternSet patterns, FileTreeInternal fileTree) {
if (root.isFile()) {
fileTrees.add(new FileBackedDirectoryFileTree(root));
} else if (root.isDirectory()) {
fileTrees.add(new DirectoryTree() {
@Override
public File getDir() {
return root;
}
@Override
public PatternSet getPatterns() {
return patterns;
}
});
}
}
@Override
public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, FileSystemMirroringFileTree sourceTree) {
// Visit the contents of the tree to generate the tree
if (visitAll(sourceTree)) {
fileTrees.add(sourceTree.getMirror());
}
}
});
return fileTrees;
}
/**
* Visits all the files of the given tree.
*/
protected static boolean visitAll(FileSystemMirroringFileTree tree) {
final MutableBoolean hasContent = new MutableBoolean();
tree.visit(new FileVisitor() {
@Override
public void visitDir(FileVisitDetails dirDetails) {
dirDetails.getFile();
hasContent.set(true);
}
@Override
public void visitFile(FileVisitDetails fileDetails) {
fileDetails.getFile();
hasContent.set(true);
}
});
return hasContent.get();
}
@Override
public Object addToAntBuilder(Object node, String childNodeName) {
addToAntBuilder(node, childNodeName, AntType.ResourceCollection);
return this;
}
@Override
public boolean isEmpty() {
return getFiles().isEmpty();
}
@Override
public FileTreeInternal getAsFileTree() {
return new FileCollectionBackedFileTree(taskDependencyFactory, patternSetFactory, this);
}
@Override
public FileCollection filter(Closure filterClosure) {
return filter(Specs.convertClosureToSpec(filterClosure));
}
@Override
public FileCollectionInternal filter(final Spec<? super File> filterSpec) {
return new FilteredFileCollection(this, filterSpec);
}
/**
* This is final - override {@link #visitContents(FileCollectionStructureVisitor)} instead to provide the contents.
*/
@Override
public final void visitStructure(FileCollectionStructureVisitor visitor) {
if (visitor.startVisit(OTHER, this)) {
visitContents(visitor);
}
}
protected void visitContents(FileCollectionStructureVisitor visitor) {
visitor.visitCollection(OTHER, this);
}
}