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

Emulate and deprecate generatedFilesBaseDir #636

Merged
merged 8 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 7 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,10 @@ task.plugins {

```gradle
protobuf {
generatedFilesBaseDir = "$projectDir/generated"

generateProtoTasks {
all().each { task ->
task.builtins {
// Generates Python code in the output folder:
// Generates Python code
python { }

// If you wish to avoid generating Java files:
Expand Down Expand Up @@ -410,7 +408,7 @@ protobuf {
```gradle
{ task ->
// If true, will generate a descriptor_set.desc file under
// $generatedFilesBaseDir/$sourceSet. Default is false.
// task.outputBaseDir. Default is false.
// See --descriptor_set_out in protoc documentation about what it is.
task.generateDescriptorSet = true

Expand All @@ -430,19 +428,11 @@ protobuf {

#### Change where files are generated

By default generated Java files are under
``$generatedFilesBaseDir/$sourceSet/$builtinPluginName``, where
``$generatedFilesBaseDir`` is ``$buildDir/generated/source/proto`` by default,
and is configurable. E.g.,

```gradle
protobuf {
...
 generatedFilesBaseDir = "$projectDir/src/generated"
}
```
Generated files are under `task.outputBaseDir` with a subdirectory per
builtin and plugin. This produces a folder structure of
``$buildDir/generated/source/proto/$sourceSet/$builtinPluginName``.

The subdirectory name, which is by default ``$builtinPluginName``, can also be
The subdirectory name, which is by default ``$builtinPluginName``, can be
changed by setting the ``outputSubDir`` property in the ``builtins`` or
``plugins`` block of a task configuration within ``generateProtoTasks`` block
(see previous section). E.g.,
Expand All @@ -451,8 +441,7 @@ changed by setting the ``outputSubDir`` property in the ``builtins`` or
{ task ->
task.plugins {
grpc {
// Write the generated files under
// "$generatedFilesBaseDir/$sourceSet/grpcjava"
// Use subdirectory 'grpcjava' instead of the default 'grpc'
outputSubDir = 'grpcjava'
}
}
Expand Down Expand Up @@ -521,29 +510,6 @@ This plugin integrates with the ``idea`` plugin and automatically
registers the proto files and generated Java code as sources.


```gradle
apply plugin: 'idea'

protobuf {
...
generatedFilesBaseDir = "$projectDir/gen"
}

clean {
delete protobuf.generatedFilesBaseDir
}

idea {
module {
// proto files and generated Java files are automatically added as
// source dirs.
// If you have additional sources, add them here:
sourceDirs += file("/path/to/other/sources");
}
}
```


## Testing the plugin

``testProject*`` are testing projects that uses this plugin to compile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ public abstract class GenerateProtoTask extends DefaultTask {
void compile() {
Preconditions.checkState(state == State.FINALIZED, 'doneConfig() has not been called')

project.delete(outputBaseDir)
rougsig marked this conversation as resolved.
Show resolved Hide resolved
// Sort to ensure generated descriptors have a canonical representation
// to avoid triggering unnecessary rebuilds downstream
List<File> protoFiles = sourceDirs.asFileTree.files.sort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ abstract class ProtobufExtension {
* "${project.buildDir}/generated/source/proto".
*/
private String generatedFilesBaseDir
rougsig marked this conversation as resolved.
Show resolved Hide resolved
@PackageScope
final String defaultGeneratedFilesBaseDir

public ProtobufExtension(final Project project) {
this.project = project
this.tasks = new GenerateProtoTaskCollection(project)
this.tools = new ToolsLocator(project)
this.taskConfigActions = []
this.generatedFilesBaseDir = "${project.buildDir}/generated/source/proto"
this.defaultGeneratedFilesBaseDir = generatedFilesBaseDir
}

@PackageScope
Expand All @@ -83,6 +86,11 @@ abstract class ProtobufExtension {
}
}

@PackageScope
boolean isGeneratedFilesBaseDirChanged() {
return generatedFilesBaseDir != defaultGeneratedFilesBaseDir
}

//===========================================================================
// Configuration methods
//===========================================================================
Expand Down
18 changes: 14 additions & 4 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,27 @@ class ProtobufPlugin implements Plugin<Project> {
Action<GenerateProtoTask> configureAction) {
String generateProtoTaskName = 'generate' +
Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto'
Provider<String> outDir = project.providers.provider {
"${this.protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}".toString()
}
return project.tasks.register(generateProtoTaskName, GenerateProtoTask) {
it.description = "Compiles Proto source for '${sourceSetOrVariantName}'".toString()
it.outputBaseDir = outDir
it.outputBaseDir = project.providers.provider {
"${protobufExtension.defaultGeneratedFilesBaseDir}/${sourceSetOrVariantName}".toString()
}
it.addSourceDirs(protoSourceSet)
it.addIncludeDir(protoSourceSet.sourceDirectories)
it.addSourceDirs(extractProtosDirs)
it.addIncludeDir(extractProtosDirs)
it.addIncludeDir(project.files(extractIncludeProtosTask))
it.doLast { task ->
if (!protobufExtension.isGeneratedFilesBaseDirChanged()) {
return
}
// Purposefully don't wire this up to outputs, as it can be mixed with other files.
project.copy { CopySpec spec ->
spec.includeEmptyDirs = false
spec.from(it.outputBaseDir)
spec.into("${protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}")
}
}
configureAction.execute(it)
}
}
Expand Down