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 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
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
25 changes: 25 additions & 0 deletions src/main/groovy/com/google/protobuf/gradle/CopyActionFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DeleteSpec;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.WorkResult;
import org.gradle.util.GradleVersion;

import javax.inject.Inject;

Expand All @@ -46,6 +49,18 @@
@CompileStatic
interface CopyActionFacade {
WorkResult copy(Action<? super CopySpec> var1);
WorkResult delete(Action<? super DeleteSpec> action);

@CompileStatic
final class Loader {
public static CopyActionFacade create(Project project, ObjectFactory objectFactory) {
if (GradleVersion.current().compareTo(GradleVersion.version("6.0")) >= 0) {
// Use object factory to instantiate as that will inject the necessary service.
return objectFactory.newInstance(CopyActionFacade.FileSystemOperationsBased.class);
}
return new CopyActionFacade.ProjectBased(project);
}
}

@CompileStatic
class ProjectBased implements CopyActionFacade {
Expand All @@ -59,6 +74,11 @@ public ProjectBased(Project project) {
public WorkResult copy(Action<? super CopySpec> action) {
return project.copy(action);
}

@Override
public WorkResult delete(Action<? super DeleteSpec> action) {
return project.delete(action);
}
}

@CompileStatic
Expand All @@ -70,5 +90,10 @@ abstract class FileSystemOperationsBased implements CopyActionFacade {
public WorkResult copy(Action<? super CopySpec> action) {
return getFileSystemOperations().copy(action);
}

@Override
public WorkResult delete(Action<? super DeleteSpec> action) {
return getFileSystemOperations().delete(action);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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()
Expand Down Expand Up @@ -587,6 +588,9 @@ public abstract class GenerateProtoTask extends DefaultTask {
void compile() {
Preconditions.checkState(state == State.FINALIZED, 'doneConfig() has not been called')

copyActionFacade.delete { spec ->
spec.delete(outputBaseDir)
}
// 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 @@ -35,7 +35,11 @@ import groovy.transform.TypeCheckingMode
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.TaskCollection
import java.util.concurrent.atomic.AtomicReference
import javax.inject.Inject

/**
* Adds the protobuf {} block as a property of the project.
Expand All @@ -53,27 +57,34 @@ abstract class ProtobufExtension {
* The base directory of generated files. The default is
* "${project.buildDir}/generated/source/proto".
*/
private String generatedFilesBaseDir
private final AtomicReference<String> generatedFilesBaseDir = new AtomicReference<String>()
ejona86 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 = "${project.buildDir}/generated/source/proto"
this.generatedFilesBaseDir.set(defaultGeneratedFilesBaseDir)
}

@Inject
protected abstract ProviderFactory getProviderFactory()

@PackageScope
ToolsLocator getTools() {
return tools
}

String getGeneratedFilesBaseDir() {
rougsig marked this conversation as resolved.
Show resolved Hide resolved
return generatedFilesBaseDir
return generatedFilesBaseDir.get()
}

@Deprecated
void setGeneratedFilesBaseDir(String generatedFilesBaseDir) {
this.generatedFilesBaseDir = generatedFilesBaseDir
this.generatedFilesBaseDir.set(generatedFilesBaseDir)
}

@PackageScope
Expand All @@ -83,6 +94,13 @@ abstract class ProtobufExtension {
}
}

@PackageScope
Provider<String> getGeneratedFilesBaseDirProvider() {
// Avoid any reference to `project` for compatibility with configuration cache
AtomicReference<String> generatedFilesBaseDir = this.generatedFilesBaseDir
return providerFactory.provider { generatedFilesBaseDir.get() }
}

//===========================================================================
// Configuration methods
//===========================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import javax.inject.Inject
@CompileStatic
abstract class ProtobufExtract extends DefaultTask {

private final CopyActionFacade copyActionFacade = instantiateCopyActionFacade()
private final CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(project, objectFactory)
private final ArchiveActionFacade archiveActionFacade = instantiateArchiveActionFacade()
private final FileCollection filteredProtos = instantiateFilteredProtos()

Expand Down Expand Up @@ -98,14 +98,6 @@ abstract class ProtobufExtract extends DefaultTask {
@Inject
protected abstract ProviderFactory getProviderFactory()

private CopyActionFacade instantiateCopyActionFacade() {
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
// Use object factory to instantiate as that will inject the necessary service.
return objectFactory.newInstance(CopyActionFacade.FileSystemOperationsBased)
}
return new CopyActionFacade.ProjectBased(project)
}

private ArchiveActionFacade instantiateArchiveActionFacade() {
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
// Use object factory to instantiate as that will inject the necessary service.
Expand Down
22 changes: 18 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,31 @@ 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()
}
String defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir
Provider<String> generatedFilesBaseDirProvider = protobufExtension.generatedFilesBaseDirProvider
return project.tasks.register(generateProtoTaskName, GenerateProtoTask) {
CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(it.project, it.objectFactory)
it.description = "Compiles Proto source for '${sourceSetOrVariantName}'".toString()
it.outputBaseDir = outDir
it.outputBaseDir = project.providers.provider {
"${defaultGeneratedFilesBaseDir}/${sourceSetOrVariantName}".toString()
}
it.addSourceDirs(protoSourceSet)
it.addIncludeDir(protoSourceSet.sourceDirectories)
it.addSourceDirs(extractProtosDirs)
it.addIncludeDir(extractProtosDirs)
it.addIncludeDir(project.files(extractIncludeProtosTask))
it.doLast { task ->
String generatedFilesBaseDir = generatedFilesBaseDirProvider.get()
if (generatedFilesBaseDir == defaultGeneratedFilesBaseDir) {
return
}
// Purposefully don't wire this up to outputs, as it can be mixed with other files.
copyActionFacade.copy { CopySpec spec ->
spec.includeEmptyDirs = false
spec.from(it.outputBaseDir)
spec.into("${generatedFilesBaseDir}/${sourceSetOrVariantName}")
}
}
configureAction.execute(it)
}
}
Expand Down