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

Fix ProtobufExtract task for Gradle Configuration Cache #413

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2020, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.protobuf.gradle;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a file header for this new file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed :)


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);
}
}
}
21 changes: 19 additions & 2 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufExtract.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.google.common.base.Preconditions
import groovy.transform.CompileDynamic
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
Expand All @@ -56,6 +57,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 +81,11 @@ abstract class ProtobufExtract extends DefaultTask {
return copyActionFacade
}

@Internal
ArchiveActionFacade getArchiveActionFacade() {
return archiveActionFacade
}

@Inject
abstract ObjectFactory getObjectFactory()

Expand Down Expand Up @@ -112,9 +119,10 @@ abstract class ProtobufExtract extends DefaultTask {
spec.into(destDir)
}
} else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) {
FileTree 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 +131,10 @@ abstract class ProtobufExtract extends DefaultTask {
|| file.path.endsWith('.tar.gz')
|| file.path.endsWith('.tar.bz2')
|| file.path.endsWith('.tgz')) {
FileTree 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 +163,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)
}
}