-
Notifications
You must be signed in to change notification settings - Fork 15
Resolve extra classpath lazily when configuring Protobuf generation tasks #377
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
base: main
Are you sure you want to change the base?
Changes from 14 commits
937a724
4fac433
61c69a7
ee4b46f
92697da
cbb2552
1130721
6fde671
49b68b9
149ea05
e009f08
d7eb937
943e493
e2c9f55
c55b703
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,8 @@ import com.google.protobuf.gradle.ProtobufExtract | |
import com.google.protobuf.gradle.ProtobufPlugin | ||
import com.google.protobuf.gradle.id | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.artifacts.Configuration | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.jvm.tasks.Jar | ||
import org.gradle.kotlin.dsl.apply | ||
import org.gradle.kotlin.dsl.configure | ||
|
@@ -52,51 +53,90 @@ internal fun configureProtobufPlugin( | |
} | ||
|
||
generateProtoTasks { | ||
val mainExtractProtoAdditions = mutableListOf<TaskInputFiles>() | ||
val testExtractProtoAdditions = mutableListOf<TaskInputFiles?>() | ||
|
||
for (task in all()) { | ||
if (disableJava) { | ||
task.builtins { | ||
findByName("java")?.run(::remove) | ||
} | ||
} | ||
|
||
val extensions = resolveExtensions(project, task) | ||
|
||
if (task.isTestTask()) { | ||
testExtractProtoAdditions.add(extensions.test?.let { TaskInputFiles(extensions.taskName, it) }) | ||
} else { | ||
mainExtractProtoAdditions.add(TaskInputFiles(extensions.taskName, extensions.main)) | ||
} | ||
|
||
task.plugins { | ||
id(target.protocPluginName) { | ||
project.afterEvaluate { | ||
option("$KOTLIN_EXTRA_CLASSPATH=${extraClasspath(project, task)}") | ||
option("$GENERATE_TYPES=${ext.generate.types}") | ||
option("$GENERATE_DESCRIPTORS=${ext.generate.descriptors}") | ||
option("$GENERATE_GRPC_DESCRIPTORS=${ext.generate.grpcDescriptors}") | ||
option("$GENERATE_GRPC_KOTLIN_STUBS=${ext.generate.grpcKotlinStubs}") | ||
option("$FORMAT_OUTPUT=${ext.formatOutput}") | ||
option("$KOTLIN_TARGET=$target") | ||
} | ||
option("$KOTLIN_EXTRA_CLASSPATH=${extraClasspath(project, extensions)}") | ||
option("$GENERATE_TYPES=${ext.generate.types}") | ||
option("$GENERATE_DESCRIPTORS=${ext.generate.descriptors}") | ||
option("$GENERATE_GRPC_DESCRIPTORS=${ext.generate.grpcDescriptors}") | ||
option("$GENERATE_GRPC_KOTLIN_STUBS=${ext.generate.grpcKotlinStubs}") | ||
option("$FORMAT_OUTPUT=${ext.formatOutput}") | ||
option("$KOTLIN_TARGET=$target") | ||
} | ||
} | ||
} | ||
|
||
project.handleExtraInputFiles(mainExtractProtoAdditions, testExtractProtoAdditions) | ||
} | ||
} | ||
} | ||
|
||
private fun extraClasspath(project: Project, task: GenerateProtoTask): String { | ||
var extensions: FileCollection = project.configurations.getByName(EXTENSIONS) | ||
|
||
if (task.isTest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out this isn't reliable - extractIncludeJvmTestProto is considered |
||
extensions += project.configurations.getByName(TEST_EXTENSIONS) | ||
private fun Project.handleExtraInputFiles(main: List<TaskInputFiles>, test: List<TaskInputFiles?>) { | ||
afterEvaluate { | ||
handleExtractProtoAdditions(main, false) | ||
handleExtractProtoAdditions(test, true) | ||
} | ||
} | ||
|
||
// Must explicitly register input files here; if any extensions dependencies are project dependencies then Gradle | ||
// won't pick them up as dependencies unless we do this. There may be a better way to do this but for now just | ||
// manually do what protobuf-gradle-plugin used to do. | ||
// https://github.com/google/protobuf-gradle-plugin/commit/0521fe707ccedee7a0b4ce0fb88409eefb04e59d | ||
project.tasks.withType<ProtobufExtract> { | ||
if (name.startsWith("extractInclude")) { | ||
inputFiles.from(extensions) | ||
private fun Project.handleExtractProtoAdditions(additions: List<TaskInputFiles?>, test: Boolean) { | ||
additions.filterNotNull().forEach { | ||
// there is more than one task in multiplatform projects: e.g. extractIncludeProto and extractIncludeJvmMainProto | ||
extractIncludeProtoTasks().forEach { task -> | ||
val isTest = task.isTestTask() | ||
if ((!test && !isTest) || (test && isTest)) { | ||
andrewparmet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.log(DEBUG_LOG_LEVEL, "Adding input files to task ${task.name} from ${it.taskName}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logger.debug? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constant maps to debug right now, but I like to be able to change the value to make logs noisier at will. I can back out that pattern in a separate PR but don't want to right now. |
||
task.inputFiles.from(it.inputFiles) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return extensions.joinToString(";") { URLEncoder.encode(it.path, "UTF-8") } | ||
private class TaskInputFiles( | ||
val taskName: String, | ||
val inputFiles: Any | ||
) | ||
|
||
private fun extraClasspath(project: Project, extensions: ExtensionsConfigurations) = | ||
project.objects.fileCollection().from(extensions.asList()).files.joinToString(";") { URLEncoder.encode(it.path, "UTF-8") } | ||
|
||
private class ExtensionsConfigurations( | ||
val taskName: String, | ||
val main: Provider<Configuration>, | ||
val test: Provider<Configuration>? | ||
) { | ||
fun asList() = | ||
listOfNotNull(main, test) | ||
} | ||
|
||
private fun resolveExtensions(project: Project, task: GenerateProtoTask) = | ||
ExtensionsConfigurations( | ||
task.name, | ||
project.configurations.named(EXTENSIONS), | ||
if (task.isTestTask()) { | ||
project.configurations.named(TEST_EXTENSIONS) | ||
} else { | ||
null | ||
} | ||
) | ||
|
||
private fun configureSources(project: Project) { | ||
project.afterEvaluate { | ||
if (project.tasks.findByName("sourcesJar") != null) { | ||
|
@@ -114,3 +154,15 @@ private fun normalizePath(binaryPath: String) = | |
} else { | ||
binaryPath | ||
} | ||
|
||
private fun GenerateProtoTask.isTestTask() = | ||
name.isTestTask() | ||
|
||
private fun ProtobufExtract.isTestTask() = | ||
name.isTestTask() | ||
|
||
private fun String.isTestTask() = | ||
endsWith("TestProto") | ||
|
||
private fun Project.extractIncludeProtoTasks() = | ||
project.tasks.withType<ProtobufExtract>().filter { it.name.startsWith("extractInclude") } |
Uh oh!
There was an error while loading. Please reload this page.