Skip to content

Instantly share code, notes, and snippets.

@ghale
Created June 4, 2020 16:48
Show Gist options
  • Save ghale/cfb402901973cb65743bfb0e6d0c5921 to your computer and use it in GitHub Desktop.
Save ghale/cfb402901973cb65743bfb0e6d0c5921 to your computer and use it in GitHub Desktop.
// Creaate an implementation of the task using all of the types below
task reverseFiles(type: ReverseFiles) {
outputDir = file("$buildDir/reversed")
source("sources")
}
// 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() }
}
}
}
class ReverseFiles extends SourceTask {
private final WorkerExecutor workerExecutor
@OutputDirectory
File outputDir
// 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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment