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 2 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
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,7 +57,7 @@ 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

Expand All @@ -62,22 +66,25 @@ abstract class ProtobufExtension {
this.tasks = new GenerateProtoTaskCollection(project)
this.tools = new ToolsLocator(project)
this.taskConfigActions = []
this.generatedFilesBaseDir = "${project.buildDir}/generated/source/proto"
this.defaultGeneratedFilesBaseDir = generatedFilesBaseDir
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 @@ -88,8 +95,10 @@ abstract class ProtobufExtension {
}

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

//===========================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,26 +383,29 @@ class ProtobufPlugin implements Plugin<Project> {
Action<GenerateProtoTask> configureAction) {
String generateProtoTaskName = 'generate' +
Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto'
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 = project.providers.provider {
"${protobufExtension.defaultGeneratedFilesBaseDir}/${sourceSetOrVariantName}".toString()
"${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()) {
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("${protobufExtension.generatedFilesBaseDir}/${sourceSetOrVariantName}")
spec.into("${generatedFilesBaseDir}/${sourceSetOrVariantName}")
}
}
configureAction.execute(it)
Expand Down