-
Notifications
You must be signed in to change notification settings - Fork 288
Make Protobuf Gradle Plugin AGP 9 compatible #797
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ package com.google.protobuf.gradle | |
|
|
||
| import static java.nio.charset.StandardCharsets.US_ASCII | ||
|
|
||
| import org.gradle.api.file.DirectoryProperty | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import groovy.transform.PackageScope | ||
| import groovy.transform.TypeChecked | ||
|
|
@@ -85,87 +87,6 @@ public abstract class GenerateProtoTask extends DefaultTask { | |
| static final int CMD_ARGUMENT_EXTRA_LENGTH = 3 | ||
| private static final String JAR_SUFFIX = ".jar" | ||
|
|
||
| private final CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(project, objectFactory) | ||
| // include dirs are passed to the '-I' option of protoc. They contain protos | ||
| // that may be "imported" from the source protos, but will not be compiled. | ||
| private final ConfigurableFileCollection includeDirs = objectFactory.fileCollection() | ||
| // source files are proto files that will be compiled by protoc | ||
| private final ConfigurableFileCollection sourceDirs = objectFactory.fileCollection() | ||
| private final NamedDomainObjectContainer<PluginOptions> builtins = objectFactory.domainObjectContainer(PluginOptions) | ||
| private final NamedDomainObjectContainer<PluginOptions> plugins = objectFactory.domainObjectContainer(PluginOptions) | ||
| private final ProjectLayout projectLayout = project.layout | ||
| private final ToolsLocator toolsLocator = project.extensions.findByType(ProtobufExtension).tools | ||
|
|
||
| @Input | ||
| final Property<String> javaExecutablePath = objectFactory.property(String) | ||
| .convention(project.extensions.findByType(ProtobufExtension).javaExecutablePath) | ||
|
|
||
| // These fields are set by the Protobuf plugin only when initializing the | ||
| // task. Ideally they should be final fields, but Gradle task cannot have | ||
| // constructor arguments. We use the initializing flag to prevent users from | ||
| // accidentally modifying them. | ||
| private Provider<String> outputBaseDir | ||
| // Tags for selectors inside protobuf.generateProtoTasks; do not serialize with Gradle configuration caching | ||
| @SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable | ||
| transient private SourceSet sourceSet | ||
|
|
||
| @SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable | ||
| transient private Object variant | ||
| private List<String> flavors | ||
| private String buildType | ||
| private boolean isTestVariant | ||
| private final Provider<Boolean> isAndroidProject = providerFactory.provider { Utils.isAndroidProject(project) } | ||
| private final Provider<Boolean> isTestProvider = providerFactory.provider { | ||
| if (Utils.isAndroidProject(project)) { | ||
| return isTestVariant | ||
| } | ||
| return Utils.isTest(sourceSet.name) | ||
| } | ||
|
|
||
| /** | ||
| * If true, will set the protoc flag | ||
| * --descriptor_set_out="${outputBaseDir}/descriptor_set.desc" | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Internal("Handled as input via getDescriptorSetOptionsForCaching()") | ||
| boolean generateDescriptorSet | ||
|
|
||
| /** | ||
| * Configuration object for descriptor generation details. | ||
| */ | ||
| public class DescriptorSetOptions { | ||
| /** | ||
| * If set, specifies an alternative location than the default for storing the descriptor | ||
| * set. | ||
| * | ||
| * Default: null | ||
| */ | ||
| @Nullable | ||
| @Optional | ||
| @OutputFile | ||
| String path | ||
|
|
||
| /** | ||
| * If true, source information (comments, locations) will be included in the descriptor set. | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Input | ||
| boolean includeSourceInfo | ||
|
|
||
| /** | ||
| * If true, imports are included in the descriptor set, such that it is self-containing. | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Input | ||
| boolean includeImports | ||
| } | ||
|
|
||
| @Internal("Handled as input via getDescriptorSetOptionsForCaching()") | ||
| final DescriptorSetOptions descriptorSetOptions = new DescriptorSetOptions() | ||
|
|
||
| // protoc allows you to prefix comma-delimited options to the path in | ||
| // the --*_out flags, e.g., | ||
| // - Without options: --java_out=/path/to/output | ||
|
|
@@ -241,17 +162,85 @@ public abstract class GenerateProtoTask extends DefaultTask { | |
| } | ||
| } | ||
|
|
||
| void setOutputBaseDir(Provider<String> outputBaseDir) { | ||
| checkInitializing() | ||
| Preconditions.checkState(this.outputBaseDir == null, 'outputBaseDir is already set') | ||
| this.outputBaseDir = outputBaseDir | ||
| } | ||
| private final CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(project, objectFactory) | ||
| // include dirs are passed to the '-I' option of protoc. They contain protos | ||
| // that may be "imported" from the source protos, but will not be compiled. | ||
| private final ConfigurableFileCollection includeDirs = objectFactory.fileCollection() | ||
| // source files are proto files that will be compiled by protoc | ||
| private final ConfigurableFileCollection sourceDirs = objectFactory.fileCollection() | ||
| private final NamedDomainObjectContainer<PluginOptions> builtins = objectFactory.domainObjectContainer(PluginOptions) | ||
| private final NamedDomainObjectContainer<PluginOptions> plugins = objectFactory.domainObjectContainer(PluginOptions) | ||
| private final ProjectLayout projectLayout = project.layout | ||
| private final ToolsLocator toolsLocator = project.extensions.findByType(ProtobufExtension).tools | ||
|
|
||
| @Input | ||
| final Property<String> javaExecutablePath = objectFactory.property(String) | ||
| .convention(project.extensions.findByType(ProtobufExtension).javaExecutablePath) | ||
|
|
||
| @OutputDirectory | ||
| String getOutputBaseDir() { | ||
| return outputBaseDir.get() | ||
| abstract DirectoryProperty getOutputBaseDir() | ||
|
Collaborator
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 is API-breaking. We'd generally follow the same approach that Gradle took when swapping things to properties: use a different name for the property so that you can leave the old API in-place (which then just forwards to the property) for the migration.
Contributor
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. Added back getOutputBaseDir and setOutputBaseDir with old types. Renamed the new property to getGetOutputBaseDirProperty |
||
|
|
||
| // Tags for selectors inside protobuf.generateProtoTasks; do not serialize with Gradle configuration caching | ||
| @SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable | ||
| transient private SourceSet sourceSet | ||
|
|
||
| @SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable | ||
| transient private Object variant | ||
| private List<String> flavors | ||
| private String buildType | ||
| private boolean isTestVariant | ||
| private final Provider<Boolean> isAndroidProject = providerFactory.provider { Utils.isAndroidProject(project) } | ||
| private final Provider<Boolean> isTestProvider = providerFactory.provider { | ||
| if (Utils.isAndroidProject(project)) { | ||
| return isTestVariant | ||
| } | ||
| return Utils.isTest(sourceSet.name) | ||
| } | ||
|
|
||
| /** | ||
| * If true, will set the protoc flag | ||
| * --descriptor_set_out="${outputBaseDir}/descriptor_set.desc" | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Internal("Handled as input via getDescriptorSetOptionsForCaching()") | ||
| boolean generateDescriptorSet | ||
|
omarismail94 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Configuration object for descriptor generation details. | ||
| */ | ||
| public class DescriptorSetOptions { | ||
| /** | ||
| * If set, specifies an alternative location than the default for storing the descriptor | ||
| * set. | ||
| * | ||
| * Default: null | ||
| */ | ||
| @Nullable | ||
| @Optional | ||
| @OutputFile | ||
| String path | ||
|
|
||
| /** | ||
| * If true, source information (comments, locations) will be included in the descriptor set. | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Input | ||
| boolean includeSourceInfo | ||
|
|
||
| /** | ||
| * If true, imports are included in the descriptor set, such that it is self-containing. | ||
| * | ||
| * Default: false | ||
| */ | ||
| @Input | ||
| boolean includeImports | ||
| } | ||
|
|
||
| @Internal("Handled as input via getDescriptorSetOptionsForCaching()") | ||
| final DescriptorSetOptions descriptorSetOptions = new DescriptorSetOptions() | ||
|
|
||
| void setSourceSet(SourceSet sourceSet) { | ||
| checkInitializing() | ||
| Preconditions.checkState(!isAndroidProject.get(), | ||
|
|
@@ -400,7 +389,8 @@ public abstract class GenerateProtoTask extends DefaultTask { | |
| throw new IllegalStateException( | ||
| "requested descriptor path but descriptor generation is off") | ||
| } | ||
| return descriptorSetOptions.path != null ? descriptorSetOptions.path : "${outputBaseDir.get()}/descriptor_set.desc" | ||
| return descriptorSetOptions.path != null ? descriptorSetOptions.path | ||
| : "${outputBaseDir.get().asFile.path}/descriptor_set.desc" | ||
| } | ||
|
|
||
| @Inject | ||
|
|
@@ -543,7 +533,7 @@ public abstract class GenerateProtoTask extends DefaultTask { | |
| //=========================================================================== | ||
|
|
||
| String getOutputDir(PluginOptions plugin) { | ||
| return "${outputBaseDir.get()}/${plugin.outputSubDir}" | ||
| return "${outputBaseDir.get().asFile.path}/${plugin.outputSubDir}" | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,10 @@ abstract class ProtobufExtension { | |
| return this.sourceSets | ||
| } | ||
|
|
||
| ProtoSourceSet getMainSourceSet() { | ||
|
Collaborator
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. Why add this new public API when
Contributor
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. Removed, only used in one place, so it can be inlined |
||
| return sourceSets.findByName("main") | ||
| } | ||
|
|
||
| @PackageScope | ||
| ToolsLocator getTools() { | ||
| return tools | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.