/
build.gradle
60 lines (51 loc) · 1.73 KB
/
build.gradle
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
// tag::unit-of-work[]
// The parameters for a single unit of work
interface ReverseParameters extends WorkParameters {
RegularFileProperty getFileToReverse()
DirectoryProperty getDestinationDir()
}
// The implementation of a single unit of work.
abstract class ReverseFile implements WorkAction<ReverseParameters> {
private final FileSystemOperations fileSystemOperations
@Inject
public ReverseFile(FileSystemOperations fileSystemOperations) {
this.fileSystemOperations = fileSystemOperations
}
@Override
void execute() {
fileSystemOperations.copy {
from parameters.fileToReverse
into parameters.destinationDir
filter { String line -> line.reverse() }
}
}
}
// end::unit-of-work[]
// tag::task-implementation[]
abstract class ReverseFiles extends SourceTask {
private final WorkerExecutor workerExecutor
@OutputDirectory
abstract DirectoryProperty getOutputDir()
// The WorkerExecutor will be injected by Gradle at runtime
@Inject
ReverseFiles(WorkerExecutor workerExecutor) {
this.workerExecutor = workerExecutor
}
@TaskAction
void reverseFiles() {
// Create a WorkQueue to submit work items
WorkQueue workQueue = workerExecutor.noIsolation()
// Create and submit a unit of work for each file
source.each { file ->
workQueue.submit(ReverseFile.class) { ReverseParameters parameters ->
parameters.fileToReverse = file
parameters.destinationDir = outputDir
}
}
}
}
// end::task-implementation[]
tasks.register('reverseFiles', ReverseFiles) {
outputDir = layout.buildDirectory.dir("reversed")
source("sources")
}