Skip to content

Commit

Permalink
Refactor on fileResolver and support suffix in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyukman committed May 29, 2016
1 parent c296131 commit 7479d13
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ package com.google.protobuf.gradle

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.tasks.SourceSet
import org.gradle.util.ConfigureUtil

Expand All @@ -44,21 +43,18 @@ public class ProtobufConfigurator {
private final ToolsLocator tools
private final ArrayList<Closure> taskConfigClosures

final FileResolver fileResolver

/**
* The base directory of generated files. The default is
* "${project.buildDir}/generated/source/proto".
*/
public String generatedFilesBaseDir

public ProtobufConfigurator(Project project, FileResolver fileResolver) {
public ProtobufConfigurator(Project project) {
this.project = project
this.fileResolver = fileResolver
if (Utils.isAndroidProject(project)) {
tasks = new AndroidGenerateProtoTaskCollection()
} else {
tasks = new JavaGenerateProtoTaskCollection()
tasks = new DefaultGenerateProtoTaskCollection()
}
tools = new ToolsLocator(project)
taskConfigClosures = new ArrayList()
Expand Down Expand Up @@ -153,7 +149,7 @@ public class ProtobufConfigurator {
}
}

public class JavaGenerateProtoTaskCollection
public class DefaultGenerateProtoTaskCollection
extends GenerateProtoTaskCollection {
public Collection<GenerateProtoTask> ofSourceSet(String sourceSet) {
return all().findAll { task ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ package com.google.protobuf.gradle
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.Multimap
import org.gradle.api.Project
import org.gradle.api.internal.file.FileResolver
import org.gradle.util.ConfigureUtil

/**
* Adds the protobuf {} block as a property of the project.
*/
class ProtobufConvention {
def ProtobufConvention(Project project, FileResolver fileResolver) {
protobuf = new ProtobufConfigurator(project, fileResolver)
def ProtobufConvention(Project project) {
protobuf = new ProtobufConfigurator(project)
}

def final ProtobufConfigurator protobuf
Expand Down
18 changes: 5 additions & 13 deletions src/main/groovy/com/google/protobuf/gradle/TaskGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class TaskGenerator {
* compiled. For Java it's the sourceSet that sourceSetOrVariantName stands
* for; for Android it's the collection of sourceSets that the variant includes.
*/
static Task addGenerateProtoTask(Project project, String sourceSetOrVariantName, Collection<Object> sourceSets) {
static Task addGenerateProtoTask(Project project, String sourceSetOrVariantName, Collection<Object> sourceSets, String suffix = "") {
def generateProtoTaskName = 'generate' +
Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto'
Utils.getSourceSetSubstringForTaskNames(sourceSetOrVariantName) + 'Proto' + suffix
return project.tasks.create(generateProtoTaskName, GenerateProtoTask) {
description = "Compiles Proto source for '${sourceSetOrVariantName}'"
outputBaseDir = "${project.protobuf.generatedFilesBaseDir}/${sourceSetOrVariantName}"
Expand Down Expand Up @@ -67,17 +67,13 @@ class TaskGenerator {
* variant may have multiple sourceSets, each of these sourceSets will have
* its own extraction task.
*/
static Task maybeAddExtractProtosTask(Project project, String sourceSetName) {
static Task maybeAddExtractProtosTask(Project project, String sourceSetName, String suffix = "") {
def extractProtosTaskName = 'extract' +
Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto'
Task existingTask = project.tasks.findByName(extractProtosTaskName)
if (existingTask != null) {
return existingTask
}
Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto' + suffix
return project.tasks.create(extractProtosTaskName, ProtobufExtract) {
description = "Extracts proto files/dependencies specified by 'protobuf' configuration"
destDir = getExtractedProtosDir(project, sourceSetName) as File
inputs.files project.configurations[Utils.getConfigName(sourceSetName, 'protobuf')]
inputs.files project.configurations[Utils.getConfigName(sourceSetName, 'protobuf', suffix)]
}
}

Expand All @@ -94,10 +90,6 @@ class TaskGenerator {
static Task maybeAddExtractIncludeProtosTask(Project project, String sourceSetName, Object... inputFilesList) {
def extractIncludeProtosTaskName = 'extractInclude' +
Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto'
Task existingTask = project.tasks.findByName(extractIncludeProtosTaskName)
if (existingTask != null) {
return existingTask
}
return project.tasks.create(extractIncludeProtosTaskName, ProtobufExtract) {
description = "Extracts proto files from compile dependencies for includes"
destDir = getExtractedIncludeProtosDir(project, sourceSetName) as File
Expand Down
26 changes: 12 additions & 14 deletions src/main/groovy/com/google/protobuf/gradle/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class Utils {
/**
* Returns the conventional name of a configuration for a sourceSet
*/
static String getConfigName(String sourceSetName, String type) {
return sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME ?
type : (sourceSetName + StringUtils.capitalize(type))
static String getConfigName(String sourceSetName, String type, String suffix = "") {
return (sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME ?
type : (sourceSetName + StringUtils.capitalize(type))) + suffix
}

/**
Expand All @@ -60,14 +60,12 @@ class Utils {
* Creates a configuration if necessary for a source set so that the build
* author can configure dependencies for it.
*/
private static void createConfiguration(Project project, String sourceSetName) {
String configName = getConfigName(sourceSetName, 'protobuf')
if (project.configurations.findByName(configName) == null) {
project.configurations.create(configName) {
visible = false
transitive = false
extendsFrom = []
}
private static void createConfiguration(Project project, String sourceSetName, String suffix) {
String configName = getConfigName(sourceSetName, 'protobuf', suffix)
project.configurations.create(configName) {
visible = false
transitive = false
extendsFrom = []
}
}

Expand All @@ -81,10 +79,10 @@ class Utils {
}
}

static void setupSourceSets(Project project, Object sourceSets, FileResolver fileResolver) {
addSourceSetExtensions(sourceSets, fileResolver)
static void setupSourceSets(Project project, Object sourceSets, String suffix = "") {
addSourceSetExtensions(sourceSets, project.fileResolver)
sourceSets.all { sourceSet ->
createConfiguration(project, sourceSet.name)
createConfiguration(project, sourceSet.name, suffix)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ProtobufAndroidPlugin implements Plugin<Project> {

project.apply plugin: 'com.google.protobuf.base'

Utils.setupSourceSets(project, project.android.sourceSets, project.protobuf.fileResolver)
Utils.setupSourceSets(project, project.android.sourceSets)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@ package com.google.protobuf.gradle.plugins
import com.google.protobuf.gradle.ProtobufConvention
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.file.FileResolver

import javax.inject.Inject

class ProtobufBasePlugin implements Plugin<Project> {

final FileResolver fileResolver

private static final List<String> protobufPlugins = [
'com.google.protobuf.java',
'com.google.protobuf.android']

@Inject
public ProtobufBasePlugin(FileResolver fileResolver) {
this.fileResolver = fileResolver
}
'com.google.protobuf.android',
'com.google.protobuf.csharp']

void apply(final Project project) {
def gv = project.gradle.gradleVersion =~ "(\\d*)\\.(\\d*).*"
Expand All @@ -30,7 +21,7 @@ class ProtobufBasePlugin implements Plugin<Project> {
// Provides the osdetector extension
project.apply plugin: 'osdetector'

project.convention.plugins.protobuf = new ProtobufConvention(project, fileResolver);
project.convention.plugins.protobuf = new ProtobufConvention(project)

project.afterEvaluate {
def appliedPlugins = protobufPlugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ProtobufJavaPlugin implements Plugin<Project> {

project.apply plugin: 'com.google.protobuf.base'

Utils.setupSourceSets(project, project.sourceSets, project.protobuf.fileResolver)
Utils.setupSourceSets(project, project.sourceSets)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import org.apache.commons.io.FileUtils

import spock.lang.Specification

class ProtobufJavaPluginTest extends Specification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ProtobufPluginTestHelper {
}

static void copyTestProject(String testProjectName, File projectDir) {
def baseDir = new File(System.getProperty("user.dir"), "testProject")
def baseDir = new File(System.getProperty("user.dir"), testProjectName)

FileUtils.copyDirectory(baseDir, projectDir)

Expand Down

0 comments on commit 7479d13

Please sign in to comment.