Skip to content

Commit

Permalink
Fix ProtobufExtract for Gradle Configuration Cache
Browse files Browse the repository at this point in the history
by using an ArchiveActionFacade in the same vein as CopyActionFacade
in order to remove usage of Project at execution time

ArchiveActionFacade uses the internal FileOperations type from Gradle.
FileOperations is internal but quite stable and this plugin is already
making use of other internals. Moreover, a public alternative will be
available in Gradle 6.6

Signed-off-by: Paul Merlin <[email protected]>
  • Loading branch information
eskatos committed Jun 11, 2020
1 parent 7c89f81 commit dea2e91
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.google.protobuf.gradle;

import org.gradle.api.Project;
import org.gradle.api.file.FileTree;
import org.gradle.api.internal.file.FileOperations;

import javax.inject.Inject;

public interface ArchiveActionFacade {

FileTree zipTree(Object path);

FileTree tarTree(Object path);

class ProjectBased implements ArchiveActionFacade {

private final Project project;

ProjectBased(Project project) {
this.project = project;
}

@Override
public FileTree zipTree(Object path) {
return project.zipTree(path);
}

@Override
public FileTree tarTree(Object path) {
return project.tarTree(path);
}
}

abstract class ServiceBased implements ArchiveActionFacade {

// TODO Use public ArchiveOperations from Gradle 6.6 instead
@Inject
public abstract FileOperations getFileOperations();

@Override
public FileTree zipTree(Object path) {
return getFileOperations().zipTree(path);
}

@Override
public FileTree tarTree(Object path) {
return getFileOperations().tarTree(path);
}
}
}
20 changes: 18 additions & 2 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ abstract class ProtobufExtract extends DefaultTask {
private Boolean isTest = null
private final ConfigurableFileCollection inputFiles = objectFactory.fileCollection()
private final CopyActionFacade copyActionFacade = instantiateCopyActionFacade()
private final ArchiveActionFacade archiveActionFacade = instantiateArchiveActionFacade()

public void setIsTest(boolean isTest) {
this.isTest = isTest
Expand All @@ -79,6 +80,11 @@ abstract class ProtobufExtract extends DefaultTask {
return copyActionFacade
}

@Internal
ArchiveActionFacade getArchiveActionFacade() {
return archiveActionFacade
}

@Inject
abstract ObjectFactory getObjectFactory()

Expand Down Expand Up @@ -112,9 +118,10 @@ abstract class ProtobufExtract extends DefaultTask {
spec.into(destDir)
}
} else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) {
def zipTree = archiveActionFacade.zipTree(file.path)
copyActionFacade.copy { spec ->
spec.includeEmptyDirs = false
spec.from(project.zipTree(file.path)) {
spec.from(zipTree) {
include '**/*.proto'
}
spec.into(destDir)
Expand All @@ -123,9 +130,10 @@ abstract class ProtobufExtract extends DefaultTask {
|| file.path.endsWith('.tar.gz')
|| file.path.endsWith('.tar.bz2')
|| file.path.endsWith('.tgz')) {
def tarTree = archiveActionFacade.tarTree(file.path)
copyActionFacade.copy { spec ->
spec.includeEmptyDirs = false
spec.from(project.tarTree(file.path)) {
spec.from(tarTree) {
include '**/*.proto'
}
spec.into(destDir)
Expand Down Expand Up @@ -154,4 +162,12 @@ abstract class ProtobufExtract extends DefaultTask {
}
return new CopyActionFacade.ProjectBased(project)
}

private ArchiveActionFacade instantiateArchiveActionFacade() {
if (Utils.compareGradleVersion(project, "6.0") > 0) {
// Use object factory to instantiate as that will inject the necessary service.
return objectFactory.newInstance(ArchiveActionFacade.ServiceBased)
}
return new ArchiveActionFacade.ProjectBased(project)
}
}

0 comments on commit dea2e91

Please sign in to comment.