Skip to content

Commit fb71f67

Browse files
Vampireejona86
authored andcommitted
Revert work-around removal from #719 and properly fix #711
1 parent 665de2f commit fb71f67

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

config/codenarc/codenarc.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7777
<exclude name="CyclomaticComplexity"/>
7878
<exclude name="NestedBlockDepth"/>
7979
<exclude name="MethodCount"/>
80+
<exclude name="ParameterCount"/>
8081
</ruleset-ref>
8182
<ruleset-ref path='rulesets/unused.xml'/>
8283
<ruleset-ref path='rulesets/unnecessary.xml'>

src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ import org.gradle.api.file.ConfigurableFileCollection
3535
import org.gradle.api.file.DirectoryProperty
3636
import org.gradle.api.file.DuplicatesStrategy
3737
import org.gradle.api.file.FileCollection
38-
import org.gradle.api.file.FileSystemLocation
3938
import org.gradle.api.file.FileTree
4039
import org.gradle.api.logging.Logger
4140
import org.gradle.api.model.ObjectFactory
42-
import org.gradle.api.provider.ProviderFactory
4341
import org.gradle.api.tasks.InputFiles
4442
import org.gradle.api.tasks.Internal
4543
import org.gradle.api.tasks.OutputDirectory
@@ -71,6 +69,13 @@ abstract class ProtobufExtract extends DefaultTask {
7169
@Internal
7270
public abstract ConfigurableFileCollection getInputFiles()
7371

72+
/**
73+
* A dummy task dependency to delay provider calculation for input proto files to execution time
74+
* even with configuration cache enabled.
75+
*/
76+
@Internal
77+
abstract ConfigurableFileCollection getDummyTaskDependency()
78+
7479
/**
7580
* Inputs for this task containing only proto files, which is enough for up-to-date checks.
7681
* Add inputs to inputFiles. Uses relative path sensitivity as directory layout changes impact output.
@@ -96,9 +101,6 @@ abstract class ProtobufExtract extends DefaultTask {
96101
@Inject
97102
protected abstract ObjectFactory getObjectFactory()
98103

99-
@Inject
100-
protected abstract ProviderFactory getProviderFactory()
101-
102104
private ArchiveActionFacade instantiateArchiveActionFacade() {
103105
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
104106
// Use object factory to instantiate as that will inject the necessary service.
@@ -114,14 +116,17 @@ abstract class ProtobufExtract extends DefaultTask {
114116
// Provider.map seems broken for excluded tasks. Add inputFiles with all contents excluded for
115117
// the dependency it provides, but then provide the files we actually care about in our own
116118
// provider. https://github.com/google/protobuf-gradle-plugin/issues/550
117-
PatternSet protoFilter = new PatternSet().include("**/*.proto")
119+
// Additionally depend on a dummy task for the own provider so that it is executed at
120+
// execution time even with configuration cache enabled.
121+
// https://github.com/google/protobuf-gradle-plugin/issues/711
122+
FileCollection inputFiles = this.inputFiles
118123
return objectFactory.fileCollection()
119124
.from(inputFiles.filter { false })
120-
.from(
121-
inputFiles.getElements().map { elements ->
125+
.from(dummyTaskDependency.elements.map { unused ->
126+
Set<File> files = inputFiles.files
127+
PatternSet protoFilter = new PatternSet().include("**/*.proto")
122128
Set<Object> protoInputs = [] as Set
123-
for (FileSystemLocation e : elements) {
124-
File file = e.asFile
129+
for (File file : files) {
125130
if (file.isDirectory()) {
126131
protoInputs.add(file)
127132
} else if (file.path.endsWith('.proto')) {
@@ -148,7 +153,7 @@ abstract class ProtobufExtract extends DefaultTask {
148153
|| file.path.endsWith('.tgz')) {
149154
protoInputs.add(archiveFacade.tarTree(file.path).matching(protoFilter))
150155
} else {
151-
logger.debug "Skipping unsupported file type (${file.path});" +
156+
logger.debug "Skipping unsupported file type (${file.path}); " +
152157
"handles only jar, tar, tar.gz, tar.bz2 & tgz"
153158
}
154159
}

src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import org.gradle.api.GradleException
4444
import org.gradle.api.NamedDomainObjectContainer
4545
import org.gradle.api.Plugin
4646
import org.gradle.api.Project
47+
import org.gradle.api.Task
4748
import org.gradle.api.artifacts.Configuration
4849
import org.gradle.api.artifacts.type.ArtifactTypeDefinition
4950
import org.gradle.api.attributes.LibraryElements
@@ -129,28 +130,29 @@ class ProtobufPlugin implements Plugin<Project> {
129130
// extract included protos from {@code variant.compileConfiguration}
130131
// of each variant.
131132
Collection<Closure> postConfigure = []
133+
Provider<Task> dummyTask = project.tasks.register("protobufDummy")
132134
if (isAndroid) {
133135
project.android.sourceSets.configureEach { sourceSet ->
134136
ProtoSourceSet protoSourceSet = protobufExtension.sourceSets.create(sourceSet.name)
135137
addSourceSetExtension(sourceSet, protoSourceSet)
136138
Configuration protobufConfig = createProtobufConfiguration(protoSourceSet)
137-
setupExtractProtosTask(protoSourceSet, protobufConfig)
139+
setupExtractProtosTask(protoSourceSet, protobufConfig, dummyTask)
138140
}
139141

140142
NamedDomainObjectContainer<ProtoSourceSet> variantSourceSets =
141143
project.objects.domainObjectContainer(ProtoSourceSet) { String name ->
142144
new DefaultProtoSourceSet(name, project.objects)
143145
}
144146
ProjectExt.forEachVariant(this.project) { BaseVariant variant ->
145-
addTasksForVariant(variant, variantSourceSets, postConfigure)
147+
addTasksForVariant(variant, variantSourceSets, postConfigure, dummyTask)
146148
}
147149
} else {
148150
project.sourceSets.configureEach { sourceSet ->
149151
ProtoSourceSet protoSourceSet = protobufExtension.sourceSets.create(sourceSet.name)
150152
addSourceSetExtension(sourceSet, protoSourceSet)
151153
Configuration protobufConfig = createProtobufConfiguration(protoSourceSet)
152154
Configuration compileProtoPath = createCompileProtoPathConfiguration(protoSourceSet)
153-
addTasksForSourceSet(sourceSet, protoSourceSet, protobufConfig, compileProtoPath, postConfigure)
155+
addTasksForSourceSet(sourceSet, protoSourceSet, protobufConfig, compileProtoPath, postConfigure, dummyTask)
154156
}
155157
}
156158
project.afterEvaluate {
@@ -237,11 +239,11 @@ class ProtobufPlugin implements Plugin<Project> {
237239
*/
238240
private void addTasksForSourceSet(
239241
SourceSet sourceSet, ProtoSourceSet protoSourceSet, Configuration protobufConfig,
240-
Configuration compileProtoPath, Collection<Closure> postConfigure) {
241-
Provider<ProtobufExtract> extractProtosTask = setupExtractProtosTask(protoSourceSet, protobufConfig)
242+
Configuration compileProtoPath, Collection<Closure> postConfigure, Provider<Task> dummyTask) {
243+
Provider<ProtobufExtract> extractProtosTask = setupExtractProtosTask(protoSourceSet, protobufConfig, dummyTask)
242244

243245
Provider<ProtobufExtract> extractIncludeProtosTask = setupExtractIncludeProtosTask(
244-
protoSourceSet, compileProtoPath)
246+
protoSourceSet, compileProtoPath, dummyTask)
245247

246248
// Make protos in 'test' sourceSet able to import protos from the 'main' sourceSet.
247249
// Pass include proto files from main to test.
@@ -295,7 +297,8 @@ class ProtobufPlugin implements Plugin<Project> {
295297
private void addTasksForVariant(
296298
Object variant,
297299
NamedDomainObjectContainer<ProtoSourceSet> variantSourceSets,
298-
Collection<Closure> postConfigure
300+
Collection<Closure> postConfigure,
301+
Provider<Task> dummyTask
299302
) {
300303
Boolean isTestVariant = variant instanceof TestVariant || variant instanceof UnitTestVariant
301304
ProtoSourceSet variantSourceSet = variantSourceSets.create(variant.name)
@@ -318,7 +321,7 @@ class ProtobufPlugin implements Plugin<Project> {
318321
}
319322
}
320323

321-
setupExtractIncludeProtosTask(variantSourceSet, classPathConfig)
324+
setupExtractIncludeProtosTask(variantSourceSet, classPathConfig, dummyTask)
322325

323326
// GenerateProto task, one per variant (compilation unit).
324327
variant.sourceSets.each { SourceProvider sourceProvider ->
@@ -422,14 +425,16 @@ class ProtobufPlugin implements Plugin<Project> {
422425
*/
423426
private Provider<ProtobufExtract> setupExtractProtosTask(
424427
ProtoSourceSet protoSourceSet,
425-
Configuration protobufConfig
428+
Configuration protobufConfig,
429+
Provider<Task> dummyTask
426430
) {
427431
String sourceSetName = protoSourceSet.name
428432
String taskName = getExtractProtosTaskName(sourceSetName)
429433
Provider<ProtobufExtract> task = project.tasks.register(taskName, ProtobufExtract) {
430434
it.description = "Extracts proto files/dependencies specified by 'protobuf' configuration"
431435
it.destDir.set(getExtractedProtosDir(sourceSetName) as File)
432436
it.inputFiles.from(protobufConfig)
437+
it.dummyTaskDependency.from(dummyTask)
433438
}
434439
protoSourceSet.proto.srcDir(task)
435440
return task
@@ -448,13 +453,15 @@ class ProtobufPlugin implements Plugin<Project> {
448453
*/
449454
private Provider<ProtobufExtract> setupExtractIncludeProtosTask(
450455
ProtoSourceSet protoSourceSet,
451-
FileCollection archives
456+
FileCollection archives,
457+
Provider<Task> dummyTask
452458
) {
453459
String taskName = 'extractInclude' + Utils.getSourceSetSubstringForTaskNames(protoSourceSet.name) + 'Proto'
454460
Provider<ProtobufExtract> task = project.tasks.register(taskName, ProtobufExtract) {
455461
it.description = "Extracts proto files from compile dependencies for includes"
456462
it.destDir.set(getExtractedIncludeProtosDir(protoSourceSet.name) as File)
457463
it.inputFiles.from(archives)
464+
it.dummyTaskDependency.from(dummyTask)
458465
}
459466
protoSourceSet.includeProtoDirs.from(task)
460467
return task

0 commit comments

Comments
 (0)