Skip to content

Commit

Permalink
Move protobufExtension to ProtobufPlugin field
Browse files Browse the repository at this point in the history
  • Loading branch information
rougsig committed Jul 12, 2022
1 parent fe768bf commit 20f56ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
62 changes: 25 additions & 37 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ProtobufPlugin implements Plugin<Project> {

private final FileResolver fileResolver
private Project project
private ProtobufExtension protobufExtension
private boolean wasApplied = false

@Inject
Expand All @@ -85,7 +86,7 @@ class ProtobufPlugin implements Plugin<Project> {
"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
Expand All @@ -100,7 +101,7 @@ class ProtobufPlugin implements Plugin<Project> {
} else {
wasApplied = true

doApply(protobufExtension)
doApply()
}
}

Expand All @@ -121,7 +122,7 @@ class ProtobufPlugin implements Plugin<Project> {
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])

Expand All @@ -142,18 +143,18 @@ class ProtobufPlugin implements Plugin<Project> {
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()
Expand Down Expand Up @@ -248,29 +249,26 @@ class ProtobufPlugin implements Plugin<Project> {
/**
* 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)
}
}
}

/**
* 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 {
Expand All @@ -293,13 +291,9 @@ class ProtobufPlugin implements Plugin<Project> {
/**
* 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')) {
Expand Down Expand Up @@ -353,25 +347,21 @@ class ProtobufPlugin implements Plugin<Project> {
* 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<Object> sourceSets
) {
private Task addGenerateProtoTask(String sourceSetOrVariantName, Collection<Object> 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<ExecutableLocator>) protobufExtension.tools.plugins).asMap
((NamedDomainObjectContainer<ExecutableLocator>) this.protobufExtension.tools.plugins).asMap
})
}
}
Expand Down Expand Up @@ -475,12 +465,10 @@ class ProtobufPlugin implements Plugin<Project> {
}
}

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)
Expand All @@ -490,7 +478,7 @@ class ProtobufPlugin implements Plugin<Project> {
}

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) {
Expand All @@ -504,7 +492,7 @@ class ProtobufPlugin implements Plugin<Project> {
}
} 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)
}
Expand Down
7 changes: 2 additions & 5 deletions testProjectBase/build_base.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 20f56ca

Please sign in to comment.