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

Feat/643 2 #6

Open
wants to merge 7 commits into
base: feat/643-1
Choose a base branch
from
Open
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
16 changes: 7 additions & 9 deletions examples/exampleKotlinDslProject/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ protobuf {
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.spec {
plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
id("grpc") { }
}
ofSourceSet("main") {
plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
id("grpc") { }
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions examples/exampleProject/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ protobuf {
}
}
generateProtoTasks {
ofSourceSet('main').configureEach {
it.spec {
plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
grpc { }
}
ofSourceSet('main') {
plugins {
// Apply the "grpc" plugin whose spec is defined above, without
// options. Note the braces cannot be omitted, otherwise the
// plugin will not be added. This is because of the implicit way
// NamedDomainObjectContainer binds the methods.
grpc { }
}
}
}
Expand Down
151 changes: 1 addition & 150 deletions src/main/groovy/com/google/protobuf/gradle/GenerateProtoTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ package com.google.protobuf.gradle

import static java.nio.charset.StandardCharsets.US_ASCII

import org.gradle.api.Action
import org.gradle.util.ConfigureUtil
import com.google.protobuf.gradle.internal.DefaultGenerateProtoTaskSpec
import com.google.protobuf.gradle.tasks.GenerateProtoTaskSpec
import com.google.protobuf.gradle.tasks.PluginSpec
Expand All @@ -41,8 +39,6 @@ import org.gradle.api.provider.Property
import org.gradle.api.tasks.Nested
import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.file.ConfigurableFileCollection
Expand All @@ -62,7 +58,6 @@ import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskAction
import javax.inject.Inject

Expand All @@ -72,7 +67,7 @@ import javax.inject.Inject
// TODO(zhangkun83): add per-plugin output dir reconfiguraiton.
@CompileStatic
@CacheableTask
public abstract class GenerateProtoTask extends DefaultTask {
abstract class GenerateProtoTask extends DefaultTask {
// Windows CreateProcess has command line limit of 32768:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
static final int WINDOWS_CMD_LENGTH_LIMIT = 32760
Expand All @@ -98,21 +93,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
// constructor arguments. We use the initializing flag to prevent users from
// accidentally modifying them.
private Provider<String> outputBaseDir
// Tags for selectors inside protobuf.generateProtoTasks; do not serialize with Gradle configuration caching
@SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable
transient private SourceSet sourceSet
@SuppressWarnings("UnnecessaryTransientModifier") // It is not necessary for task to implement Serializable
transient private Object variant
private List<String> flavors
private String buildType
private boolean isTestVariant
private final Provider<Boolean> isAndroidProject = providerFactory.provider { Utils.isAndroidProject(project) }
private final Provider<Boolean> isTestProvider = providerFactory.provider {
if (Utils.isAndroidProject(project)) {
return isTestVariant
}
return Utils.isTest(sourceSet.name)
}

@SuppressWarnings("AbstractClassWithPublicConstructor") // required to configure properties convention values
GenerateProtoTask() {
Expand Down Expand Up @@ -211,7 +191,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
}

void setOutputBaseDir(Provider<String> outputBaseDir) {
checkInitializing()
Preconditions.checkState(this.outputBaseDir == null, 'outputBaseDir is already set')
this.outputBaseDir = outputBaseDir
}
Expand All @@ -221,43 +200,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
return outputBaseDir.get()
}

void setSourceSet(SourceSet sourceSet) {
checkInitializing()
Preconditions.checkState(!isAndroidProject.get(),
'sourceSet should not be set in an Android project')
this.sourceSet = sourceSet
}

void setVariant(Object variant, boolean isTestVariant) {
checkInitializing()
Preconditions.checkState(isAndroidProject.get(),
'variant should not be set in a Java project')
this.variant = variant
this.isTestVariant = isTestVariant
}

void setFlavors(List<String> flavors) {
checkInitializing()
Preconditions.checkState(isAndroidProject.get(),
'flavors should not be set in a Java project')
this.flavors = Collections.unmodifiableList(new ArrayList<String>(flavors))
}

void setBuildType(String buildType) {
checkInitializing()
Preconditions.checkState(isAndroidProject.get(),
'buildType should not be set in a Java project')
this.buildType = buildType
}

@Internal("Inputs tracked in getSourceDirs()")
SourceSet getSourceSet() {
Preconditions.checkState(!isAndroidProject.get(),
'sourceSet should not be used in an Android project')
Preconditions.checkNotNull(sourceSet, 'sourceSet is not set')
return sourceSet
}

@SkipWhenEmpty
@PathSensitive(PathSensitivity.RELATIVE)
@IgnoreEmptyDirectories
Expand All @@ -272,14 +214,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
return includeDirs
}

@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
Object getVariant() {
Preconditions.checkState(isAndroidProject.get(),
'variant should not be used in a Java project')
Preconditions.checkNotNull(variant, 'variant is not set')
return variant
}

/**
* Not for external use. Used to expose inputs to Gradle.
*
Expand Down Expand Up @@ -317,48 +251,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
[toolsLocator.protoc] + requireSpec().plugins.collect { PluginSpec it -> toolsLocator.plugins.getByName(it.name) }
}

@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
Provider<Boolean> getIsAndroidProject() {
return isAndroidProject
}

@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
boolean getIsTestVariant() {
Preconditions.checkState(isAndroidProject.get(),
'isTestVariant should not be used in a Java project')
Preconditions.checkNotNull(variant, 'variant is not set')
return isTestVariant
}

@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
List<String> getFlavors() {
Preconditions.checkState(isAndroidProject.get(),
'flavors should not be used in a Java project')
Preconditions.checkNotNull(flavors, 'flavors is not set')
return flavors
}

@TypeChecked(TypeCheckingMode.SKIP) // Don't depend on AGP
@Internal("Not an actual input to the task, only used to find tasks belonging to a variant")
String getBuildType() {
Preconditions.checkState(isAndroidProject.get(),
'buildType should not be used in a Java project')
Preconditions.checkState(
variant.name == 'test' || buildType,
'buildType is not set and task is not for local unit test variant')
return buildType
}

void doneInitializing() {
Preconditions.checkState(state == State.INIT, "Invalid state: ${state}")
state = State.CONFIG
}

void doneConfig() {
Preconditions.checkState(state == State.CONFIG, "Invalid state: ${state}")
state = State.FINALIZED
}

@Internal("Tracked as an input via getDescriptorSetOptionsForCaching()")
String getDescriptorPath() {
GenerateProtoTaskSpec spec = requireSpec()
Expand All @@ -383,45 +275,20 @@ public abstract class GenerateProtoTask extends DefaultTask {
// Configuration methods
//===========================================================================

@Deprecated // temporary method for refactoring
void spec(Action<GenerateProtoTaskSpec> configureAction) {
configureAction.execute(this.spec.get())
}

@Deprecated // temporary method for refactoring
void spec(@DelegatesTo(GenerateProtoTaskSpec) Closure<GenerateProtoTaskSpec> closure) {
ConfigureUtil.configure(closure, this.spec.get())
}

/**
* Add a directory to protoc's include path.
*/
public void addIncludeDir(FileCollection dir) {
checkCanConfig()
includeDirs.from(dir)
}

/**
* Add a collection of proto source files to be compiled.
*/
public void addSourceDirs(FileCollection dirs) {
checkCanConfig()
sourceDirs.from(dirs)
}

/**
* Returns true if the Java source set or Android variant is test related.
*/
@Input
public boolean getIsTest() {
return isTestProvider.get()
}

@Internal("Already captured with getIsTest()")
Provider<Boolean> getIsTestProvider() {
return isTestProvider
}

//===========================================================================
// protoc invocation logic
//===========================================================================
Expand Down Expand Up @@ -469,7 +336,6 @@ public abstract class GenerateProtoTask extends DefaultTask {

@TaskAction
void compile() {
Preconditions.checkState(state == State.FINALIZED, 'doneConfig() has not been called')
GenerateProtoTaskSpec spec = requireSpec()

copyActionFacade.delete { DeleteSpec deleteSpec ->
Expand Down Expand Up @@ -548,21 +414,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
return spec.get()
}

private static enum State {
INIT, CONFIG, FINALIZED
}

private State state = State.INIT

private void checkInitializing() {
Preconditions.checkState(state == State.INIT, 'Should not be called after initilization has finished')
}

private void checkCanConfig() {
Preconditions.checkState(state == State.CONFIG || state == State.INIT,
'Should not be called after configuration has finished')
}

private void compileFiles(List<String> cmd) {
logger.log(LogLevel.INFO, cmd.toString())

Expand Down
Loading