Skip to content

Commit

Permalink
Merge pull request #17578
Browse files Browse the repository at this point in the history
Let FileCollection ElementsProvider expose a changing execution time value when appropriate
  • Loading branch information
bot-gradle authored and eskatos committed Jul 1, 2021
1 parent a36beb9 commit e67008c
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 4 deletions.
Expand Up @@ -312,4 +312,166 @@ class FileCollectionIntegrationTest extends AbstractIntegrationSpec implements T
'three.txt'
)
}

def "can filter a file collection using a closure hitting the filesystem"() {
given:
file("files/file0.txt") << ""
file("files/dir1/file1.txt") << ""
file("files/dir2/file2.txt") << ""

and:
buildFile """
def files = files("files/file0.txt", "files/dir1", "files/dir2", "files/dir3").filter { file ->
file.isDirectory()
}
tasks.register("sync", Sync) {
from files
into "output"
}
"""

when:
run "sync"

then:
file("output").assertHasDescendants("file1.txt", "file2.txt")

when:
run "sync"

then:
result.assertTaskSkipped(':sync')

when:
file("files/dir2").deleteDir()
file("files/dir3/file3.txt") << ""
run "sync"

then:
result.assertTaskNotSkipped(":sync")
file("output").assertHasDescendants("file1.txt", "file3.txt")
}

@ToBeFixedForConfigurationCache(because = "provider assumed to be of fixed value but hits the filesystem")
def "can filter the elements of a file collection using a closure hitting the filesystem"() {
given:
file("files/file0.txt") << ""
file("files/dir1/file1.txt") << ""
file("files/dir2/file2.txt") << ""

and:
buildFile """
def files = files("files/file0.txt", "files/dir1", "files/dir2", "files/dir3").elements.map {
it.findAll { file ->
file.asFile.isDirectory()
}
}
tasks.register("sync", Sync) {
from files
into "output"
}
"""

when:
run "sync"

then:
file("output").assertHasDescendants("file1.txt", "file2.txt")

when:
run "sync"

then:
result.assertTaskSkipped(':sync')

when:
file("files/dir2").deleteDir()
file("files/dir3/file3.txt") << ""
run "sync"

then:
result.assertTaskNotSkipped(":sync")
file("output").assertHasDescendants("file1.txt", "file3.txt")
}

@Issue("https://github.com/gradle/gradle/issues/17542")
def "can map task generated FC to a filtered List of Directory (#useCase)"() {
given:
buildFile """
import static org.gradle.util.internal.TextUtil.normaliseFileSeparators

abstract class Producer extends DefaultTask {
@OutputDirectory abstract DirectoryProperty getOutputClasses()

Producer() { outputs.upToDateWhen { false } } // TODO doesn't matter, remove this

@TaskAction void doStuff() {
File f = outputClasses.get().asFile
f.mkdirs()
new File(f, "some.txt") << "some text"
}
}

TaskProvider<Producer> prod = tasks.register("prod", Producer) {
outputClasses = layout.buildDirectory.dir("producerOutput")
}

abstract class MyTask extends DefaultTask {
@Classpath abstract ConfigurableFileCollection getClasses()
@Inject abstract ProjectLayout getLayout()
@TaskAction void doStuff() {
def root = layout.projectDirectory.asFile
classes.files.each { file ->
println("CONSUMING ${'$'}{normaliseFileSeparators(root.relativePath(file))}")
}
}
}

Provider<List<Directory>> dirsFromFilteredFcMappedElements(FileCollection fc, Directory projectDir) {
def root = layout.projectDirectory.asFile
fc.filter { File f ->
f.isDirectory()
}.elements.map {
it.collect { fileSystemLocation ->
projectDir.dir(fileSystemLocation.asFile.absolutePath)
}
}
}

Provider<List<Directory>> dirsFromFcElementsSetFiltered(FileCollection fc, Directory projectDir) {
fc.elements.map {
it.findAll { FileSystemLocation fsl ->
fsl.asFile.isDirectory()
}.collect { fileSystemLocation ->
projectDir.dir(fileSystemLocation.asFile.absolutePath)
}
}
}

FileCollection myFc = project.files(prod.map { it.outputClasses })

tasks.register("myTask", MyTask.class) {
classes.from(
files(${methodName}(myFc, project.layout.buildDirectory.get()))
)
}
"""
when:
run "myTask"
then:
outputContains("CONSUMING build/producerOutput")
when:
run "myTask"
then:
outputContains("CONSUMING build/producerOutput")
where:
methodName | useCase
"dirsFromFilteredFcMappedElements" | "dirs from filtered FC elements mapped"
"dirsFromFcElementsSetFiltered" | "dirs from FC elements set filtered"
}
}
Expand Up @@ -366,12 +366,10 @@ public void visitProducerTasks(Action<? super Task> visitor) {

@Override
public ExecutionTimeValue<Set<FileSystemLocation>> calculateExecutionTimeValue() {
ExecutionTimeValue<Set<FileSystemLocation>> value = ExecutionTimeValue.fixedValue(get());
if (contentsAreBuiltByTask()) {
return value.withChangingContent();
} else {
return value;
return ExecutionTimeValue.changingValue(this);
}
return ExecutionTimeValue.fixedValue(get());
}

private boolean contentsAreBuiltByTask() {
Expand Down

0 comments on commit e67008c

Please sign in to comment.