From 20f56ca9650be2277efff3a20f7c740988f96a52 Mon Sep 17 00:00:00 2001 From: rougsig Date: Tue, 12 Jul 2022 19:10:05 +0300 Subject: [PATCH] Move protobufExtension to ProtobufPlugin field --- .../protobuf/gradle/ProtobufPlugin.groovy | 62 ++++++++----------- testProjectBase/build_base.gradle | 7 +-- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index e12e99fe..3c05f345 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -72,6 +72,7 @@ class ProtobufPlugin implements Plugin { private final FileResolver fileResolver private Project project + private ProtobufExtension protobufExtension private boolean wasApplied = false @Inject @@ -85,7 +86,7 @@ class ProtobufPlugin implements Plugin { "Gradle version is ${project.gradle.gradleVersion}. Minimum supported version is 5.6") } - ProtobufExtension protobufExtension = project.extensions.create("protobuf", ProtobufExtension, project) + this.protobufExtension = project.extensions.create("protobuf", ProtobufExtension, project) this.project = project // At least one of the prerequisite plugins must by applied before this plugin can be applied, so @@ -100,7 +101,7 @@ class ProtobufPlugin implements Plugin { } else { wasApplied = true - doApply(protobufExtension) + doApply() } } @@ -121,7 +122,7 @@ class ProtobufPlugin implements Plugin { task.source genProtoTask.getOutputSourceDirectorySet().include("**/*.java", "**/*.kt") } - private void doApply(final ProtobufExtension protobufExtension) { + private void doApply() { // Provides the osdetector extension project.apply([plugin:com.google.gradle.osdetector.OsDetectorPlugin]) @@ -142,18 +143,18 @@ class ProtobufPlugin implements Plugin { createCompileProtoPathConfiguration(sourceSet.name) } } - addProtoTasks(protobufExtension) - protobufExtension.configureTasks() + addProtoTasks() + this.protobufExtension.configureTasks() // Disallow user configuration outside the config closures, because // next in linkGenerateProtoTasksToSourceCompile() we add generated, // outputs to the inputs of javaCompile tasks, and any new codegen // plugin output added after this point won't be added to javaCompile // tasks. - protobufExtension.generateProtoTasks.all()*.doneConfig() - linkGenerateProtoTasksToSourceCompile(protobufExtension) + this.protobufExtension.generateProtoTasks.all()*.doneConfig() + linkGenerateProtoTasksToSourceCompile() // protoc and codegen plugin configuration may change through the protobuf{} // block. Only at this point the configuration has been finalized. - protobufExtension.tools.registerTaskDependencies(protobufExtension.getGenerateProtoTasks().all()) + this.protobufExtension.tools.registerTaskDependencies(this.protobufExtension.getGenerateProtoTasks().all()) // Register proto and generated sources with IDE addSourcesToIde() @@ -248,17 +249,17 @@ class ProtobufPlugin implements Plugin { /** * Adds Protobuf-related tasks to the project. */ - private void addProtoTasks(final ProtobufExtension protobufExtension) { + private void addProtoTasks() { if (Utils.isAndroidProject(project)) { getNonTestVariants().each { variant -> - addTasksForVariant(protobufExtension, variant, false) + addTasksForVariant(variant, false) } (project.android.unitTestVariants + project.android.testVariants).each { variant -> - addTasksForVariant(protobufExtension, variant, true) + addTasksForVariant(variant, true) } } else { getSourceSets().each { sourceSet -> - addTasksForSourceSet(protobufExtension, sourceSet) + addTasksForSourceSet(sourceSet) } } } @@ -266,11 +267,8 @@ class ProtobufPlugin implements Plugin { /** * Creates Protobuf tasks for a sourceSet in a Java project. */ - private void addTasksForSourceSet( - final ProtobufExtension protobufExtension, - final SourceSet sourceSet - ) { - Task generateProtoTask = addGenerateProtoTask(protobufExtension, sourceSet.name, [sourceSet]) + private void addTasksForSourceSet(final SourceSet sourceSet) { + Task generateProtoTask = addGenerateProtoTask(sourceSet.name, [sourceSet]) generateProtoTask.sourceSet = sourceSet generateProtoTask.doneInitializing() generateProtoTask.builtins { @@ -293,13 +291,9 @@ class ProtobufPlugin implements Plugin { /** * Creates Protobuf tasks for a variant in an Android project. */ - private void addTasksForVariant( - final ProtobufExtension protobufExtension, - final Object variant, - final boolean isTestVariant - ) { + private void addTasksForVariant(final Object variant, final boolean isTestVariant) { // GenerateProto task, one per variant (compilation unit). - Task generateProtoTask = addGenerateProtoTask(protobufExtension, variant.name, variant.sourceSets) + Task generateProtoTask = addGenerateProtoTask(variant.name, variant.sourceSets) generateProtoTask.setVariant(variant, isTestVariant) generateProtoTask.flavors = ImmutableList.copyOf(variant.productFlavors.collect { it.name } ) if (variant.hasProperty('buildType')) { @@ -353,25 +347,21 @@ class ProtobufPlugin implements Plugin { * compiled. For Java it's the sourceSet that sourceSetOrVariantName stands * for; for Android it's the collection of sourceSets that the variant includes. */ - private Task addGenerateProtoTask( - ProtobufExtension protobufExtension, - String sourceSetOrVariantName, - Collection sourceSets - ) { + private Task addGenerateProtoTask(String sourceSetOrVariantName, Collection sourceSets) { String generateProtoTaskName = 'generate' + Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto' return project.tasks.create(generateProtoTaskName, GenerateProtoTask) { description = "Compiles Proto source for '${sourceSetOrVariantName}'" - outputBaseDir = "${protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}" + outputBaseDir = "${this.protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}" it.fileResolver = this.fileResolver sourceSets.each { sourceSet -> addSourceFiles(sourceSet.proto) SourceDirectorySet protoSrcDirSet = sourceSet.proto addIncludeDir(protoSrcDirSet.sourceDirectories) } - protocLocator.set(project.providers.provider { protobufExtension.tools.protoc }) + protocLocator.set(project.providers.provider { this.protobufExtension.tools.protoc }) pluginsExecutableLocators.set(project.providers.provider { - ((NamedDomainObjectContainer) protobufExtension.tools.plugins).asMap + ((NamedDomainObjectContainer) this.protobufExtension.tools.plugins).asMap }) } } @@ -475,12 +465,10 @@ class ProtobufPlugin implements Plugin { } } - private void linkGenerateProtoTasksToSourceCompile( - ProtobufExtension protobufExtension - ) { + private void linkGenerateProtoTasksToSourceCompile() { if (Utils.isAndroidProject(project)) { (getNonTestVariants() + project.android.testVariants).each { variant -> - protobufExtension.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> + this.protobufExtension.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> SourceDirectorySet generatedSources = genProtoTask.getOutputSourceDirectorySet() // This cannot be called once task execution has started. variant.registerJavaGeneratingTask(genProtoTask, generatedSources.source) @@ -490,7 +478,7 @@ class ProtobufPlugin implements Plugin { } project.android.unitTestVariants.each { variant -> - protobufExtension.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> + this.protobufExtension.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> // unit test variants do not implement registerJavaGeneratingTask Task javaCompileTask = variant.javaCompileProvider.get() if (javaCompileTask != null) { @@ -504,7 +492,7 @@ class ProtobufPlugin implements Plugin { } } else { project.sourceSets.each { SourceSet sourceSet -> - protobufExtension.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProtoTask -> + this.protobufExtension.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProtoTask -> SUPPORTED_LANGUAGES.each { String lang -> linkGenerateProtoTasksToTaskName(sourceSet.getCompileTaskName(lang), genProtoTask) } diff --git a/testProjectBase/build_base.gradle b/testProjectBase/build_base.gradle index 7bd3d9af..9b9fbe57 100644 --- a/testProjectBase/build_base.gradle +++ b/testProjectBase/build_base.gradle @@ -76,11 +76,8 @@ def assertFileExists(boolean exists, String path) { } test.doLast { - assert [ - 'generateProto', - 'generateGrpcProto', - 'generateTestProto' - ] as Set == protobuf.generateProtoTasks.all().collect({ it.name }) as Set + assert ['generateProto', 'generateGrpcProto', 'generateTestProto'] as Set == + protobuf.generateProtoTasks.all().collect({ it.name }) as Set assert ['generateProto'] as Set == protobuf.generateProtoTasks.ofSourceSet('main').collect({ it.name }) as Set assert [] as Set == protobuf.generateProtoTasks.ofTest().collect({ it.name }) as Set