Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from convention to extension #577

Merged
merged 6 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.google.common.collect.ImmutableList
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Named
Expand Down Expand Up @@ -65,7 +66,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
Expand Down Expand Up @@ -463,9 +463,9 @@ public abstract class GenerateProtoTask extends DefaultTask {
* Configures the protoc builtins in a closure, which will be manipulating a
* NamedDomainObjectContainer<PluginOptions>.
*/
public void builtins(Closure configureClosure) {
public void builtins(Action<NamedDomainObjectContainer<PluginOptions>> configureAction) {
checkCanConfig()
ConfigureUtil.configure(configureClosure, builtins)
configureAction.execute(this.builtins)
}

/**
Expand All @@ -481,9 +481,9 @@ public abstract class GenerateProtoTask extends DefaultTask {
* Configures the protoc plugins in a closure, which will be maniuplating a
* NamedDomainObjectContainer<PluginOptions>.
*/
public void plugins(Closure configureClosure) {
public void plugins(Action<NamedDomainObjectContainer<PluginOptions>> configureAction) {
checkCanConfig()
ConfigureUtil.configure(configureClosure, plugins)
configureAction.execute(this.plugins)
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
// gradle require abstract modificator on extensions
@SuppressWarnings(["AbstractClassWithoutAbstractMethod", "AbstractClassWithPublicConstructor"])
abstract class ProtobufExtension {
private final Project project
private final GenerateProtoTaskCollection tasks
private final ToolsLocator tools
private final ArrayList<Closure> taskConfigClosures
private final ArrayList<Action<GenerateProtoTaskCollection>> 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 = new GenerateProtoTaskCollection(project)
this.tools = new ToolsLocator(project)
this.taskConfigActions = []
this.generatedFilesBaseDir = "${project.buildDir}/generated/source/proto"
}

@PackageScope
ToolsLocator getTools() {
return tools
}

void runTaskConfigClosures() {
taskConfigClosures.each { closure ->
ConfigureUtil.configure(closure, tasks)
String getGeneratedFilesBaseDir() {
return generatedFilesBaseDir
}

void setGeneratedFilesBaseDir(String generatedFilesBaseDir) {
this.generatedFilesBaseDir = generatedFilesBaseDir
}

@PackageScope
void configureTasks() {
this.taskConfigActions.each { action ->
action.execute(tasks)
}
}

Expand All @@ -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<ExecutableLocator> configureAction) {
configureAction.execute(tools.protoc)
}

/**
* Locate the codegen plugin executables. The closure will be manipulating a
* NamedDomainObjectContainer<ExecutableLocator>.
*/
public void plugins(Closure configureClosure) {
ConfigureUtil.configure(configureClosure, tools.plugins)
public void plugins(Action<NamedDomainObjectContainer<ExecutableLocator>> configureAction) {
configureAction.execute(tools.plugins)
}

/**
Expand All @@ -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<GenerateProtoTaskCollection> configureAction) {
taskConfigActions.add(configureAction)
}

/**
Expand All @@ -118,50 +131,51 @@ public class ProtobufConfigurator {
}

public class GenerateProtoTaskCollection {
private final Project project

GenerateProtoTaskCollection(final Project project) {
this.project = project
}

public TaskCollection<GenerateProtoTask> all() {
return project.tasks.withType(GenerateProtoTask)
}
}

public class AndroidGenerateProtoTaskCollection
extends GenerateProtoTaskCollection {
public TaskCollection<GenerateProtoTask> ofSourceSet(String sourceSet) {
return all().matching { GenerateProtoTask task ->
!Utils.isAndroidProject(project) && task.sourceSet.name == sourceSet
}
}

public TaskCollection<GenerateProtoTask> ofFlavor(String flavor) {
return all().matching { GenerateProtoTask task ->
task.flavors.contains(flavor)
Utils.isAndroidProject(project) && task.flavors.contains(flavor)
}
}

public TaskCollection<GenerateProtoTask> ofBuildType(String buildType) {
return all().matching { GenerateProtoTask task ->
task.buildType == buildType
Utils.isAndroidProject(project) && task.buildType == buildType
}
}

@TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP
@TypeChecked(TypeCheckingMode.SKIP)
// Don't depend on AGP
public TaskCollection<GenerateProtoTask> ofVariant(String variant) {
return all().matching { GenerateProtoTask task ->
task.variant.name == variant
Utils.isAndroidProject(project) && task.variant.name == variant
}
}

public TaskCollection<GenerateProtoTask> ofNonTest() {
return all().matching { GenerateProtoTask task ->
!task.isTestVariant
Utils.isAndroidProject(project) && !task.isTestVariant
}
}

public TaskCollection<GenerateProtoTask> ofTest() {
return all().matching { GenerateProtoTask task ->
task.isTestVariant
}
}
}

public class JavaGenerateProtoTaskCollection
extends GenerateProtoTaskCollection {
public TaskCollection<GenerateProtoTask> ofSourceSet(String sourceSet) {
return all().matching { GenerateProtoTask task ->
task.sourceSet.name == sourceSet
Utils.isAndroidProject(project) && task.isTestVariant
}
}
}
Expand Down
Loading