From d0925e6aa8c7e6d90da809eb8c6dbbdd2f6e8da1 Mon Sep 17 00:00:00 2001 From: rougsig Date: Wed, 6 Jul 2022 18:30:50 +0300 Subject: [PATCH 1/6] Migrate from convention to extension More details can be found here: https://github.com/google/protobuf-gradle-plugin/issues/505 --- .../protobuf/gradle/ProtobufConvention.groovy | 51 ---------- ...urator.groovy => ProtobufExtension.groovy} | 70 +++++++------ .../protobuf/gradle/ProtobufPlugin.groovy | 65 ++++++++----- .../gradle/ProtobufConfiguratorExts.kt | 97 ++++++------------- 4 files changed, 110 insertions(+), 173 deletions(-) delete mode 100644 src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy rename src/main/groovy/com/google/protobuf/gradle/{ProtobufConfigurator.groovy => ProtobufExtension.groovy} (73%) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy deleted file mode 100644 index 92a0c3c9..00000000 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufConvention.groovy +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Original work copyright (c) 2015, Alex Antonov. All rights reserved. - * Modified work copyright (c) 2015, Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package com.google.protobuf.gradle - -import groovy.transform.CompileStatic -import org.gradle.api.Project -import org.gradle.api.internal.file.FileResolver -import org.gradle.util.ConfigureUtil - -/** - * Adds the protobuf {} block as a property of the project. - */ -@CompileStatic -class ProtobufConvention { - ProtobufConvention(Project project, FileResolver fileResolver) { - protobuf = new ProtobufConfigurator(project, fileResolver) - } - - final ProtobufConfigurator protobuf - - ProtobufConfigurator protobuf(Closure configureClosure) { - ConfigureUtil.configure(configureClosure, protobuf) - } -} diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy similarity index 73% rename from src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy rename to src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy index 1e8dbe15..b4823fb0 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufConfigurator.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy @@ -29,44 +29,57 @@ package com.google.protobuf.gradle import groovy.transform.CompileStatic +import groovy.transform.PackageScope import groovy.transform.TypeChecked import groovy.transform.TypeCheckingMode +import org.gradle.api.Action +import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project -import org.gradle.api.internal.file.FileResolver import org.gradle.api.tasks.TaskCollection -import org.gradle.util.ConfigureUtil /** - * The main configuration block exposed as {@code protobuf} in the build script. + * Adds the protobuf {} block as a property of the project. */ @CompileStatic -public class ProtobufConfigurator { +abstract class ProtobufExtension { private final Project project private final GenerateProtoTaskCollection tasks private final ToolsLocator tools - private final ArrayList taskConfigClosures + private final ArrayList> taskConfigActions /** * The base directory of generated files. The default is * "${project.buildDir}/generated/source/proto". */ - String generatedFilesBaseDir + private String generatedFilesBaseDir - public ProtobufConfigurator(Project project, FileResolver fileResolver) { + public ProtobufExtension(final Project project) { this.project = project - if (Utils.isAndroidProject(project)) { - tasks = new AndroidGenerateProtoTaskCollection() - } else { - tasks = new JavaGenerateProtoTaskCollection() - } - tools = new ToolsLocator(project) - taskConfigClosures = [] - generatedFilesBaseDir = "${project.buildDir}/generated/source/proto" + this.tasks = Utils.isAndroidProject(project) + ? new AndroidGenerateProtoTaskCollection() + : new JavaGenerateProtoTaskCollection() + this.tools = new ToolsLocator(project) + this.taskConfigActions = [] + this.generatedFilesBaseDir = "${project.buildDir}/generated/source/proto" + } + + @PackageScope + ToolsLocator getTools() { + return tools + } + + String getGeneratedFilesBaseDir() { + return generatedFilesBaseDir + } + + void setGeneratedFilesBaseDir(String generatedFilesBaseDir) { + this.generatedFilesBaseDir = generatedFilesBaseDir } - void runTaskConfigClosures() { - taskConfigClosures.each { closure -> - ConfigureUtil.configure(closure, tasks) + @PackageScope + void configureTasks() { + this.taskConfigActions.each { action -> + action.execute(tasks) } } @@ -78,16 +91,16 @@ public class ProtobufConfigurator { * Locates the protoc executable. The closure will be manipulating an * ExecutableLocator. */ - public void protoc(Closure configureClosure) { - ConfigureUtil.configure(configureClosure, tools.protoc) + public void protoc(Action configureAction) { + configureAction.execute(tools.protoc) } /** * Locate the codegen plugin executables. The closure will be manipulating a * NamedDomainObjectContainer. */ - public void plugins(Closure configureClosure) { - ConfigureUtil.configure(configureClosure, tools.plugins) + public void plugins(Action> configureAction) { + configureAction.execute(tools.plugins) } /** @@ -101,8 +114,8 @@ public class ProtobufConfigurator { * change the task in your own afterEvaluate closure, as the change may not * be picked up correctly by the wired javaCompile task. */ - public void generateProtoTasks(Closure configureClosure) { - taskConfigClosures.add(configureClosure) + public void generateProtoTasks(Action configureAction) { + taskConfigActions.add(configureAction) } /** @@ -123,8 +136,7 @@ public class ProtobufConfigurator { } } - public class AndroidGenerateProtoTaskCollection - extends GenerateProtoTaskCollection { + public class AndroidGenerateProtoTaskCollection extends GenerateProtoTaskCollection { public TaskCollection ofFlavor(String flavor) { return all().matching { GenerateProtoTask task -> task.flavors.contains(flavor) @@ -137,7 +149,8 @@ public class ProtobufConfigurator { } } - @TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP + @TypeChecked(TypeCheckingMode.SKIP) + // Don't depend on AGP public TaskCollection ofVariant(String variant) { return all().matching { GenerateProtoTask task -> task.variant.name == variant @@ -157,8 +170,7 @@ public class ProtobufConfigurator { } } - public class JavaGenerateProtoTaskCollection - extends GenerateProtoTaskCollection { + public class JavaGenerateProtoTaskCollection extends GenerateProtoTaskCollection { public TaskCollection ofSourceSet(String sourceSet) { return all().matching { GenerateProtoTask task -> task.sourceSet.name == sourceSet diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index df85ec61..e829bb85 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -85,7 +85,7 @@ class ProtobufPlugin implements Plugin { "Gradle version is ${project.gradle.gradleVersion}. Minimum supported version is 5.6") } - this.project = project + this.project = project // At least one of the prerequisite plugins must by applied before this plugin can be applied, so // we will use the PluginManager.withPlugin() callback mechanism to delay applying this plugin until // after that has been achieved. If project evaluation completes before one of the prerequisite plugins @@ -122,8 +122,7 @@ class ProtobufPlugin implements Plugin { private void doApply() { // Provides the osdetector extension project.apply([plugin:com.google.gradle.osdetector.OsDetectorPlugin]) - - project.convention.plugins.protobuf = new ProtobufConvention(project, fileResolver) + ProtobufExtension protobufExtension = project.extensions.create("protobuf", ProtobufExtension, project) addSourceSetExtensions() getSourceSets().all { sourceSet -> @@ -142,18 +141,18 @@ class ProtobufPlugin implements Plugin { createCompileProtoPathConfiguration(sourceSet.name) } } - addProtoTasks() - project.protobuf.runTaskConfigClosures() + addProtoTasks(protobufExtension) + 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. - project.protobuf.generateProtoTasks.all()*.doneConfig() - linkGenerateProtoTasksToSourceCompile() + protobufExtension.generateProtoTasks.all()*.doneConfig() + linkGenerateProtoTasksToSourceCompile(protobufExtension) // protoc and codegen plugin configuration may change through the protobuf{} // block. Only at this point the configuration has been finalized. - project.protobuf.tools.registerTaskDependencies(project.protobuf.getGenerateProtoTasks().all()) + protobufExtension.tools.registerTaskDependencies(protobufExtension.getGenerateProtoTasks().all()) // Register proto and generated sources with IDE addSourcesToIde() @@ -248,17 +247,17 @@ class ProtobufPlugin implements Plugin { /** * Adds Protobuf-related tasks to the project. */ - private void addProtoTasks() { + private void addProtoTasks(final ProtobufExtension protobufExtension) { if (Utils.isAndroidProject(project)) { getNonTestVariants().each { variant -> - addTasksForVariant(variant, false) + addTasksForVariant(protobufExtension, variant, false) } (project.android.unitTestVariants + project.android.testVariants).each { variant -> - addTasksForVariant(variant, true) + addTasksForVariant(protobufExtension, variant, true) } } else { getSourceSets().each { sourceSet -> - addTasksForSourceSet(sourceSet) + addTasksForSourceSet(protobufExtension, sourceSet) } } } @@ -266,8 +265,11 @@ class ProtobufPlugin implements Plugin { /** * Creates Protobuf tasks for a sourceSet in a Java project. */ - private void addTasksForSourceSet(final SourceSet sourceSet) { - Task generateProtoTask = addGenerateProtoTask(sourceSet.name, [sourceSet]) + private void addTasksForSourceSet( + final ProtobufExtension protobufExtension, + final SourceSet sourceSet + ) { + Task generateProtoTask = addGenerateProtoTask(protobufExtension, sourceSet.name, [sourceSet]) generateProtoTask.sourceSet = sourceSet generateProtoTask.doneInitializing() generateProtoTask.builtins { @@ -290,9 +292,13 @@ class ProtobufPlugin implements Plugin { /** * Creates Protobuf tasks for a variant in an Android project. */ - private void addTasksForVariant(final Object variant, boolean isTestVariant) { + private void addTasksForVariant( + final ProtobufExtension protobufExtension, + final Object variant, + final boolean isTestVariant + ) { // GenerateProto task, one per variant (compilation unit). - Task generateProtoTask = addGenerateProtoTask(variant.name, variant.sourceSets) + Task generateProtoTask = addGenerateProtoTask(protobufExtension, variant.name, variant.sourceSets) generateProtoTask.setVariant(variant, isTestVariant) generateProtoTask.flavors = ImmutableList.copyOf(variant.productFlavors.collect { it.name } ) if (variant.hasProperty('buildType')) { @@ -346,22 +352,25 @@ 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(String sourceSetOrVariantName, Collection sourceSets) { + private Task addGenerateProtoTask( + ProtobufExtension protobufExtension, + String sourceSetOrVariantName, + Collection sourceSets + ) { String generateProtoTaskName = 'generate' + Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto' return project.tasks.create(generateProtoTaskName, GenerateProtoTask) { description = "Compiles Proto source for '${sourceSetOrVariantName}'" - outputBaseDir = "${project.protobuf.generatedFilesBaseDir}/${sourceSetOrVariantName}" + outputBaseDir = "${protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}" it.fileResolver = this.fileResolver sourceSets.each { sourceSet -> addSourceFiles(sourceSet.proto) SourceDirectorySet protoSrcDirSet = sourceSet.proto addIncludeDir(protoSrcDirSet.sourceDirectories) } - ProtobufConfigurator extension = project.protobuf - protocLocator.set(project.providers.provider { extension.tools.protoc }) + protocLocator.set(project.providers.provider { protobufExtension.tools.protoc }) pluginsExecutableLocators.set(project.providers.provider { - ((NamedDomainObjectContainer) extension.tools.plugins).asMap + ((NamedDomainObjectContainer) protobufExtension.tools.plugins).asMap }) } } @@ -375,7 +384,9 @@ class ProtobufPlugin implements Plugin { * its own extraction task. */ private Task setupExtractProtosTask( - GenerateProtoTask generateProtoTask, String sourceSetName) { + final GenerateProtoTask generateProtoTask, + final String sourceSetName + ) { String extractProtosTaskName = 'extract' + Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto' Task task = project.tasks.findByName(extractProtosTaskName) @@ -463,10 +474,12 @@ class ProtobufPlugin implements Plugin { } } - private void linkGenerateProtoTasksToSourceCompile() { + private void linkGenerateProtoTasksToSourceCompile( + ProtobufExtension protobufExtension + ) { if (Utils.isAndroidProject(project)) { (getNonTestVariants() + project.android.testVariants).each { variant -> - project.protobuf.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> + 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) @@ -476,7 +489,7 @@ class ProtobufPlugin implements Plugin { } project.android.unitTestVariants.each { variant -> - project.protobuf.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> + protobufExtension.generateProtoTasks.ofVariant(variant.name).each { GenerateProtoTask genProtoTask -> // unit test variants do not implement registerJavaGeneratingTask Task javaCompileTask = variant.javaCompileProvider.get() if (javaCompileTask != null) { @@ -490,7 +503,7 @@ class ProtobufPlugin implements Plugin { } } else { project.sourceSets.each { SourceSet sourceSet -> - project.protobuf.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProtoTask -> + protobufExtension.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProtoTask -> SUPPORTED_LANGUAGES.each { String lang -> linkGenerateProtoTasksToTaskName(sourceSet.getCompileTaskName(lang), genProtoTask) } diff --git a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt index 7270b672..fb106458 100644 --- a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt +++ b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt @@ -12,9 +12,9 @@ import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.invoke /** - * Applies the supplied action to the project's instance of [ProtobufConfigurator]. + * Applies the supplied action to the project's instance of [ProtobufExtension]. * - * @since 0.8.7 + * @since 0.9.0 * @usage * ``` * protobuf { @@ -24,12 +24,12 @@ import org.gradle.kotlin.dsl.invoke * ``` * * @receiver [Project] The project for which the plugin configuration will be applied - * @param action A configuration lambda to apply on a receiver of type [ProtobufConfigurator] + * @param action A configuration lambda to apply on a receiver of type [ProtobufExtension] * * @return [Unit] */ -fun Project.protobuf(action: ProtobufConfigurator.()->Unit) { - project.convention.getPlugin(ProtobufConvention::class.java).protobuf.apply(action) +fun Project.protobuf(action: ProtobufExtension.() -> Unit) { + project.extensions.getByType(ProtobufExtension::class.java).apply(action) } /** @@ -58,7 +58,7 @@ fun SourceSet.proto(action: SourceDirectorySet.() -> Unit) { (this as? ExtensionAware) ?.extensions ?.getByName("proto") - ?.let { it as? SourceDirectorySet } + ?.let { it as? SourceDirectorySet } ?.apply(action) } @@ -90,52 +90,17 @@ fun AndroidSourceSet.proto(action: SourceDirectorySet.() -> Unit) { (this as? ExtensionAware) ?.extensions ?.getByName("proto") - ?.let { it as? SourceDirectorySet } + ?.let { it as? SourceDirectorySet } ?.apply(action) } -/** - * Uses the supplied action to configure the [ExecutableLocator] for protoc. - * - * @since 0.8.7 - * @usage - * ``` - * protobuf { - * protoc { - * artifact = "com.google.protobuf:protoc:3.6.1" - * } - * } - * ``` - * - * @receiver [ProtobufConfigurator] The protobuf plugin configuration instance - * for the project. - * - * @param action A lambda with receiver of type [ExecutableLocator] used - * for configuring the locator for protoc - * - * @return [Unit] - */ -fun ProtobufConfigurator.protoc(action: ExecutableLocator.() -> Unit) { - protoc(closureOf(action)) -} - -fun ProtobufConfigurator.plugins(action: NamedDomainObjectContainerScope.() -> Unit) { - plugins(closureOf> { - this.invoke(action) - }) -} - -fun ProtobufConfigurator.generateProtoTasks(action: ProtobufConfigurator.GenerateProtoTaskCollection.()->Unit) { - generateProtoTasks(closureOf(action)) -} - -fun GenerateProtoTask.builtins(action: NamedDomainObjectContainerScope.()->Unit) { +fun GenerateProtoTask.builtins(action: NamedDomainObjectContainerScope.() -> Unit) { builtins(closureOf> { this.invoke(action) }) } -fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope.()-> Unit) { +fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope.() -> Unit) { plugins(closureOf> { this.invoke(action) }) @@ -144,7 +109,7 @@ fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope NamedDomainObjectContainerScope.id(id: String, action: (T.() -> Unit)? = null) { - action?.let { create(id, it) } - ?: create(id) +fun NamedDomainObjectContainer.id(id: String, action: (T.() -> Unit)? = null) { + action?.let { create(id, it) } ?: create(id) } - /** * An extension for removing an element by id on an instance of [NamedDomainObjectContainer]. * - * @since 0.8.7 + * @since 0.9.0 * @usage * ``` * protobuf { @@ -187,46 +150,46 @@ fun NamedDomainObjectContainerScope.id(id: String, action: (T.() -> * } * ``` * - * @receiver [NamedDomainObjectContainerScope] The scope of the [NamedDomainObjectContainer] + * @receiver [NamedDomainObjectContainer] The scope of the [NamedDomainObjectContainer] * on which to remove an element. * * @param id The string id of the element to remove. * * @return [Unit] */ -fun NamedDomainObjectContainerScope.remove(id: String) { +fun NamedDomainObjectContainer.remove(id: String) { remove(this[id]) } /** * The method generatorProtoTasks applies the supplied closure to the - * instance of [ProtobufConfigurator.GenerateProtoTaskCollection]. + * instance of [ProtobufExtension.GenerateProtoTaskCollection]. * - * Since [ProtobufConfigurator.JavaGenerateProtoTaskCollection] and [ProtobufConfigurator.AndroidGenerateProtoTaskCollection] + * Since [ProtobufExtension.JavaGenerateProtoTaskCollection] and [ProtobufExtension.AndroidGenerateProtoTaskCollection] * each have unique methods, and only one instance is allocated per project, we need a way to statically resolve the * available methods. This is a necessity since Kotlin does not have any dynamic method resolution capabilities. */ -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofSourceSet(sourceSet: String): Collection = - if (this is ProtobufConfigurator.JavaGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofSourceSet(sourceSet: String): Collection = + if (this is ProtobufExtension.JavaGenerateProtoTaskCollection) this.ofSourceSet(sourceSet) else emptyList() -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofFlavor(flavor: String): Collection = - if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofFlavor(flavor: String): Collection = + if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) this.ofFlavor(flavor) else emptyList() -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofBuildType(buildType: String): Collection = - if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofBuildType(buildType: String): Collection = + if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) this.ofBuildType(buildType) else emptyList() -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofVariant(variant: String): Collection = - if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofVariant(variant: String): Collection = + if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) this.ofVariant(variant) else emptyList() -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofNonTest(): Collection = - if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofNonTest(): Collection = + if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) this.ofNonTest() else emptyList() -fun ProtobufConfigurator.GenerateProtoTaskCollection.ofTest(): Collection = - if (this is ProtobufConfigurator.AndroidGenerateProtoTaskCollection) +fun ProtobufExtension.GenerateProtoTaskCollection.ofTest(): Collection = + if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) this.ofTest() else emptyList() From bf3c78f1b00317e495a76243773c35d808d95f6f Mon Sep 17 00:00:00 2001 From: rougsig Date: Sun, 10 Jul 2022 12:45:10 +0300 Subject: [PATCH 2/6] Merge Android/JavaGenerateProtoTaskCollection to common GenerateProtoTaskCollection --- .../protobuf/gradle/GenerateProtoTask.groovy | 15 +++---- .../protobuf/gradle/ProtobufExtension.groovy | 38 ++++++++-------- .../gradle/ProtobufConfiguratorExts.kt | 45 ------------------- testProjectAndroidKotlinDsl/build.gradle.kts | 7 --- testProjectKotlinDslBase/build.gradle.kts | 5 --- testProjectKotlinDslCopySpec/build.gradle.kts | 4 -- 6 files changed, 27 insertions(+), 87 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index 840960ed..8f86cc78 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -36,6 +36,9 @@ import com.google.common.collect.ImmutableList import groovy.transform.CompileStatic import groovy.transform.TypeChecked import groovy.transform.TypeCheckingMode +import javax.annotation.Nullable +import javax.inject.Inject +import org.gradle.api.Action import org.gradle.api.DefaultTask import org.gradle.api.GradleException import org.gradle.api.Named @@ -65,10 +68,6 @@ import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskAction -import org.gradle.util.ConfigureUtil - -import javax.annotation.Nullable -import javax.inject.Inject /** * The task that compiles proto files into Java files. @@ -463,9 +462,9 @@ public abstract class GenerateProtoTask extends DefaultTask { * Configures the protoc builtins in a closure, which will be manipulating a * NamedDomainObjectContainer. */ - public void builtins(Closure configureClosure) { + public void builtins(Action> configureAction) { checkCanConfig() - ConfigureUtil.configure(configureClosure, builtins) + configureAction.execute(this.builtins) } /** @@ -481,9 +480,9 @@ public abstract class GenerateProtoTask extends DefaultTask { * Configures the protoc plugins in a closure, which will be maniuplating a * NamedDomainObjectContainer. */ - public void plugins(Closure configureClosure) { + public void plugins(Action> configureAction) { checkCanConfig() - ConfigureUtil.configure(configureClosure, plugins) + configureAction.execute(this.plugins) } /** diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy index b4823fb0..07b6fa66 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy @@ -41,6 +41,8 @@ import org.gradle.api.tasks.TaskCollection * Adds the protobuf {} block as a property of the project. */ @CompileStatic +// gradle require abstract modificator on extensions +@SuppressWarnings(["AbstractClassWithoutAbstractMethod", "AbstractClassWithPublicConstructor"]) abstract class ProtobufExtension { private final Project project private final GenerateProtoTaskCollection tasks @@ -55,9 +57,7 @@ abstract class ProtobufExtension { public ProtobufExtension(final Project project) { this.project = project - this.tasks = Utils.isAndroidProject(project) - ? new AndroidGenerateProtoTaskCollection() - : new JavaGenerateProtoTaskCollection() + this.tasks = new GenerateProtoTaskCollection(project) this.tools = new ToolsLocator(project) this.taskConfigActions = [] this.generatedFilesBaseDir = "${project.buildDir}/generated/source/proto" @@ -131,21 +131,31 @@ abstract class ProtobufExtension { } public class GenerateProtoTaskCollection { + private final Project project + + GenerateProtoTaskCollection(final Project project) { + this.project = project + } + public TaskCollection all() { return project.tasks.withType(GenerateProtoTask) } - } - public class AndroidGenerateProtoTaskCollection extends GenerateProtoTaskCollection { + public TaskCollection ofSourceSet(String sourceSet) { + return all().matching { GenerateProtoTask task -> + !Utils.isAndroidProject(project) && task.sourceSet.name == sourceSet + } + } + public TaskCollection ofFlavor(String flavor) { return all().matching { GenerateProtoTask task -> - task.flavors.contains(flavor) + Utils.isAndroidProject(project) && task.flavors.contains(flavor) } } public TaskCollection ofBuildType(String buildType) { return all().matching { GenerateProtoTask task -> - task.buildType == buildType + Utils.isAndroidProject(project) && task.buildType == buildType } } @@ -153,27 +163,19 @@ abstract class ProtobufExtension { // Don't depend on AGP public TaskCollection ofVariant(String variant) { return all().matching { GenerateProtoTask task -> - task.variant.name == variant + Utils.isAndroidProject(project) && task.variant.name == variant } } public TaskCollection ofNonTest() { return all().matching { GenerateProtoTask task -> - !task.isTestVariant + Utils.isAndroidProject(project) && !task.isTestVariant } } public TaskCollection ofTest() { return all().matching { GenerateProtoTask task -> - task.isTestVariant - } - } - } - - public class JavaGenerateProtoTaskCollection extends GenerateProtoTaskCollection { - public TaskCollection ofSourceSet(String sourceSet) { - return all().matching { GenerateProtoTask task -> - task.sourceSet.name == sourceSet + Utils.isAndroidProject(project) && task.isTestVariant } } } diff --git a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt index fb106458..ae503c68 100644 --- a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt +++ b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt @@ -94,18 +94,6 @@ fun AndroidSourceSet.proto(action: SourceDirectorySet.() -> Unit) { ?.apply(action) } -fun GenerateProtoTask.builtins(action: NamedDomainObjectContainerScope.() -> Unit) { - builtins(closureOf> { - this.invoke(action) - }) -} - -fun GenerateProtoTask.plugins(action: NamedDomainObjectContainerScope.() -> Unit) { - plugins(closureOf> { - this.invoke(action) - }) -} - /** * An extension for creating and configuring the elements of an instance of [NamedDomainObjectContainer]. * @@ -160,36 +148,3 @@ fun NamedDomainObjectContainer.id(id: String, action: (T.() -> Unit fun NamedDomainObjectContainer.remove(id: String) { remove(this[id]) } - -/** - * The method generatorProtoTasks applies the supplied closure to the - * instance of [ProtobufExtension.GenerateProtoTaskCollection]. - * - * Since [ProtobufExtension.JavaGenerateProtoTaskCollection] and [ProtobufExtension.AndroidGenerateProtoTaskCollection] - * each have unique methods, and only one instance is allocated per project, we need a way to statically resolve the - * available methods. This is a necessity since Kotlin does not have any dynamic method resolution capabilities. - */ - -fun ProtobufExtension.GenerateProtoTaskCollection.ofSourceSet(sourceSet: String): Collection = - if (this is ProtobufExtension.JavaGenerateProtoTaskCollection) - this.ofSourceSet(sourceSet) else emptyList() - -fun ProtobufExtension.GenerateProtoTaskCollection.ofFlavor(flavor: String): Collection = - if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) - this.ofFlavor(flavor) else emptyList() - -fun ProtobufExtension.GenerateProtoTaskCollection.ofBuildType(buildType: String): Collection = - if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) - this.ofBuildType(buildType) else emptyList() - -fun ProtobufExtension.GenerateProtoTaskCollection.ofVariant(variant: String): Collection = - if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) - this.ofVariant(variant) else emptyList() - -fun ProtobufExtension.GenerateProtoTaskCollection.ofNonTest(): Collection = - if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) - this.ofNonTest() else emptyList() - -fun ProtobufExtension.GenerateProtoTaskCollection.ofTest(): Collection = - if (this is ProtobufExtension.AndroidGenerateProtoTaskCollection) - this.ofTest() else emptyList() diff --git a/testProjectAndroidKotlinDsl/build.gradle.kts b/testProjectAndroidKotlinDsl/build.gradle.kts index 514388ab..0c1ad414 100644 --- a/testProjectAndroidKotlinDsl/build.gradle.kts +++ b/testProjectAndroidKotlinDsl/build.gradle.kts @@ -1,14 +1,7 @@ import com.android.build.gradle.api.BaseVariant -import com.google.protobuf.gradle.generateProtoTasks import com.google.protobuf.gradle.id -import com.google.protobuf.gradle.ofBuildType -import com.google.protobuf.gradle.ofFlavor -import com.google.protobuf.gradle.ofNonTest -import com.google.protobuf.gradle.ofVariant -import com.google.protobuf.gradle.plugins import com.google.protobuf.gradle.proto import com.google.protobuf.gradle.protobuf -import com.google.protobuf.gradle.protoc plugins { id("com.android.application") diff --git a/testProjectKotlinDslBase/build.gradle.kts b/testProjectKotlinDslBase/build.gradle.kts index 88c36452..21d02289 100644 --- a/testProjectKotlinDslBase/build.gradle.kts +++ b/testProjectKotlinDslBase/build.gradle.kts @@ -8,11 +8,6 @@ plugins { id("com.google.protobuf") } -// This extension is not auto generated when we apply the plugin using -// apply(plugin = "com.google.protobuf") -val Project.protobuf: ProtobufConvention get() = - this.convention.getPlugin(ProtobufConvention::class) - repositories { maven("https://plugins.gradle.org/m2/") } diff --git a/testProjectKotlinDslCopySpec/build.gradle.kts b/testProjectKotlinDslCopySpec/build.gradle.kts index 887598aa..50ad7a3c 100644 --- a/testProjectKotlinDslCopySpec/build.gradle.kts +++ b/testProjectKotlinDslCopySpec/build.gradle.kts @@ -1,9 +1,5 @@ -import com.google.protobuf.gradle.generateProtoTasks import com.google.protobuf.gradle.id -import com.google.protobuf.gradle.ofSourceSet -import com.google.protobuf.gradle.plugins import com.google.protobuf.gradle.protobuf -import com.google.protobuf.gradle.protoc buildscript { repositories { From 768fd67c17679619479c91ad58aad0e41a98c579 Mon Sep 17 00:00:00 2001 From: rougsig Date: Sun, 10 Jul 2022 12:57:11 +0300 Subject: [PATCH 3/6] Replace `protobuf.protobuf` with `protobuf` protobuf.protobuf property was removed --- testProjectAndroidKotlinDsl/build.gradle.kts | 2 +- testProjectKotlinDslBase/build.gradle.kts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/testProjectAndroidKotlinDsl/build.gradle.kts b/testProjectAndroidKotlinDsl/build.gradle.kts index 0c1ad414..620bc5d1 100644 --- a/testProjectAndroidKotlinDsl/build.gradle.kts +++ b/testProjectAndroidKotlinDsl/build.gradle.kts @@ -153,7 +153,7 @@ afterEvaluate { } test { doLast { - val genProtoTasks = project.protobuf.protobuf.generateProtoTasks + val genProtoTasks = project.protobuf.generateProtoTasks val genProtoTaskNames = setOf( "generateArmFreeappDebugAndroidTestProto", diff --git a/testProjectKotlinDslBase/build.gradle.kts b/testProjectKotlinDslBase/build.gradle.kts index 21d02289..badae357 100644 --- a/testProjectKotlinDslBase/build.gradle.kts +++ b/testProjectKotlinDslBase/build.gradle.kts @@ -79,7 +79,7 @@ tasks { "test"{ doLast{ - val generateProtoTasks = project.protobuf.protobuf.generateProtoTasks + val generateProtoTasks = project.protobuf.generateProtoTasks val generateProtoTaskNames = generateProtoTasks.all().map { it.name }.toSet() val generateProtoTaskNamesMain = generateProtoTasks.ofSourceSet("main").map { it.name }.toSet() From 58e505f89aa3aa082723851f6c24fb7b6ab67c35 Mon Sep 17 00:00:00 2001 From: rougsig Date: Sun, 10 Jul 2022 13:19:13 +0300 Subject: [PATCH 4/6] Remove ProtobufExtension registration from afterEvaluate block --- .../protobuf/gradle/GenerateProtoTask.groovy | 5 ++-- .../protobuf/gradle/ProtobufPlugin.groovy | 7 +++--- .../gradle/ProtobufConfiguratorExts.kt | 25 ------------------- testProjectAndroidKotlinDsl/build.gradle.kts | 1 - testProjectKotlinDslCopySpec/build.gradle.kts | 1 - 5 files changed, 7 insertions(+), 32 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy index 8f86cc78..4541dd66 100644 --- a/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy @@ -36,8 +36,6 @@ import com.google.common.collect.ImmutableList import groovy.transform.CompileStatic import groovy.transform.TypeChecked import groovy.transform.TypeCheckingMode -import javax.annotation.Nullable -import javax.inject.Inject import org.gradle.api.Action import org.gradle.api.DefaultTask import org.gradle.api.GradleException @@ -69,6 +67,9 @@ import org.gradle.api.tasks.SkipWhenEmpty import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskAction +import javax.annotation.Nullable +import javax.inject.Inject + /** * The task that compiles proto files into Java files. */ diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index e829bb85..e12e99fe 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -85,6 +85,8 @@ 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.project = project // At least one of the prerequisite plugins must by applied before this plugin can be applied, so // we will use the PluginManager.withPlugin() callback mechanism to delay applying this plugin until @@ -98,7 +100,7 @@ class ProtobufPlugin implements Plugin { } else { wasApplied = true - doApply() + doApply(protobufExtension) } } @@ -119,10 +121,9 @@ class ProtobufPlugin implements Plugin { task.source genProtoTask.getOutputSourceDirectorySet().include("**/*.java", "**/*.kt") } - private void doApply() { + private void doApply(final ProtobufExtension protobufExtension) { // Provides the osdetector extension project.apply([plugin:com.google.gradle.osdetector.OsDetectorPlugin]) - ProtobufExtension protobufExtension = project.extensions.create("protobuf", ProtobufExtension, project) addSourceSetExtensions() getSourceSets().all { sourceSet -> diff --git a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt index ae503c68..be679a2c 100644 --- a/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt +++ b/src/main/kotlin/com/google/protobuf/gradle/ProtobufConfiguratorExts.kt @@ -2,35 +2,10 @@ package com.google.protobuf.gradle import com.android.build.gradle.api.AndroidSourceSet import org.gradle.api.NamedDomainObjectContainer -import org.gradle.api.Project import org.gradle.api.file.SourceDirectorySet import org.gradle.api.plugins.ExtensionAware import org.gradle.api.tasks.SourceSet -import org.gradle.kotlin.dsl.NamedDomainObjectContainerScope -import org.gradle.kotlin.dsl.closureOf import org.gradle.kotlin.dsl.get -import org.gradle.kotlin.dsl.invoke - -/** - * Applies the supplied action to the project's instance of [ProtobufExtension]. - * - * @since 0.9.0 - * @usage - * ``` - * protobuf { - * ... - * generatedFilesBaseDir = files(...) - * } - * ``` - * - * @receiver [Project] The project for which the plugin configuration will be applied - * @param action A configuration lambda to apply on a receiver of type [ProtobufExtension] - * - * @return [Unit] - */ -fun Project.protobuf(action: ProtobufExtension.() -> Unit) { - project.extensions.getByType(ProtobufExtension::class.java).apply(action) -} /** * Applies the supplied action to the "proto" [SourceDirectorySet] extension on diff --git a/testProjectAndroidKotlinDsl/build.gradle.kts b/testProjectAndroidKotlinDsl/build.gradle.kts index 620bc5d1..c9b7ffc1 100644 --- a/testProjectAndroidKotlinDsl/build.gradle.kts +++ b/testProjectAndroidKotlinDsl/build.gradle.kts @@ -1,7 +1,6 @@ import com.android.build.gradle.api.BaseVariant import com.google.protobuf.gradle.id import com.google.protobuf.gradle.proto -import com.google.protobuf.gradle.protobuf plugins { id("com.android.application") diff --git a/testProjectKotlinDslCopySpec/build.gradle.kts b/testProjectKotlinDslCopySpec/build.gradle.kts index 50ad7a3c..e63c9144 100644 --- a/testProjectKotlinDslCopySpec/build.gradle.kts +++ b/testProjectKotlinDslCopySpec/build.gradle.kts @@ -1,5 +1,4 @@ import com.google.protobuf.gradle.id -import com.google.protobuf.gradle.protobuf buildscript { repositories { From fe768bfe1b1133da9fd6017301394e784026b577 Mon Sep 17 00:00:00 2001 From: rougsig Date: Sun, 10 Jul 2022 13:58:15 +0300 Subject: [PATCH 5/6] Add android generate proto tasks filters to java project test --- testProjectBase/build_base.gradle | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/testProjectBase/build_base.gradle b/testProjectBase/build_base.gradle index d29f5127..7bd3d9af 100644 --- a/testProjectBase/build_base.gradle +++ b/testProjectBase/build_base.gradle @@ -76,10 +76,18 @@ 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 + assert [] as Set == protobuf.generateProtoTasks.ofNonTest().collect({ it.name }) as Set + assert [] as Set == protobuf.generateProtoTasks.ofFlavor('flavor-name').collect({ it.name }) as Set + assert [] as Set == protobuf.generateProtoTasks.ofBuildType('buildType-name').collect({ it.name }) as Set + assert [] as Set == protobuf.generateProtoTasks.ofVariant('variant-name').collect({ it.name }) as Set assertJavaCompileHasProtoGeneratedDir('main', ['java']) assertJavaCompileHasProtoGeneratedDir('test', ['java']) From 81fed13c8556c06fc7996821689fe8eba9e8f833 Mon Sep 17 00:00:00 2001 From: rougsig Date: Tue, 12 Jul 2022 19:10:05 +0300 Subject: [PATCH 6/6] Move protobufExtension to ProtobufPlugin field --- .../protobuf/gradle/ProtobufExtension.groovy | 3 +- .../protobuf/gradle/ProtobufPlugin.groovy | 67 +++++++------------ testProjectBase/build_base.gradle | 7 +- 3 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy index 07b6fa66..2ccf4db9 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufExtension.groovy @@ -159,8 +159,7 @@ abstract class ProtobufExtension { } } - @TypeChecked(TypeCheckingMode.SKIP) - // Don't depend on AGP + @TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP public TaskCollection ofVariant(String variant) { return all().matching { GenerateProtoTask task -> Utils.isAndroidProject(project) && task.variant.name == variant diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index e12e99fe..7a82e575 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 }) } } @@ -384,10 +374,7 @@ class ProtobufPlugin implements Plugin { * variant may have multiple sourceSets, each of these sourceSets will have * its own extraction task. */ - private Task setupExtractProtosTask( - final GenerateProtoTask generateProtoTask, - final String sourceSetName - ) { + private Task setupExtractProtosTask(final GenerateProtoTask generateProtoTask, final String sourceSetName) { String extractProtosTaskName = 'extract' + Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto' Task task = project.tasks.findByName(extractProtosTaskName) @@ -475,12 +462,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 +475,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 +489,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