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

Backport much of master to 1.9.x; KotlinCompile type, osdetector, lazy buildDir #678

Merged
merged 6 commits into from
Feb 10, 2023
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
7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ ext.isReleaseVersion = !version.endsWith("SNAPSHOT")

repositories {
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://repo.gradle.org/gradle/libs-releases-local/" }
google()
// Needed for resolving 'kotlinx-metadata-jvm'
// A transitive dependency of gradle Kotlin DSL
maven { url "https://kotlin.bintray.com/kotlinx/" }
}

configurations {
Expand All @@ -55,8 +51,9 @@ configurations {

dependencies {
compileOnly "com.android.tools.build:gradle:4.1.0"
compileOnly "org.jetbrains.kotlin.android:org.jetbrains.kotlin.android.gradle.plugin:1.7.22"

implementation 'com.google.gradle:osdetector-gradle-plugin:1.7.1'
implementation 'com.google.gradle:osdetector-gradle-plugin:1.7.3'

testImplementation 'junit:junit:4.12'
testImplementation('org.spockframework:spock-core:1.0-groovy-2.4') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskCollection

/**
Expand All @@ -54,14 +55,16 @@ abstract class ProtobufExtension {
private final NamedDomainObjectContainer<ProtoSourceSet> sourceSets

@PackageScope
final String defaultGeneratedFilesBaseDir
final Provider<String> defaultGeneratedFilesBaseDir

public ProtobufExtension(final Project project) {
this.project = project
this.tasks = new GenerateProtoTaskCollection(project)
this.tools = new ToolsLocator(project)
this.taskConfigActions = []
this.defaultGeneratedFilesBaseDir = "${project.buildDir}/generated/source/proto"
this.defaultGeneratedFilesBaseDir = project.layout.buildDirectory.dir("generated/source/proto").map {
it.asFile.path
}
this.generatedFilesBaseDirProperty.convention(defaultGeneratedFilesBaseDir)
this.sourceSets = project.objects.domainObjectContainer(ProtoSourceSet) { String name ->
new DefaultProtoSourceSet(name, project.objects)
Expand Down
12 changes: 6 additions & 6 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ import org.gradle.api.internal.artifacts.ArtifactAttributes
import org.gradle.api.plugins.AppliedPlugin
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceTask
import org.gradle.language.jvm.tasks.ProcessResources
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
* The main class for the protobuf plugin.
Expand Down Expand Up @@ -349,7 +349,7 @@ class ProtobufPlugin implements Plugin<Project> {
project.plugins.withId("org.jetbrains.kotlin.android") {
project.afterEvaluate {
String compileKotlinTaskName = Utils.getKotlinAndroidCompileTaskName(project, variant.name)
project.tasks.named(compileKotlinTaskName, SourceTask) { SourceTask task ->
project.tasks.named(compileKotlinTaskName, KotlinCompile) { KotlinCompile task ->
task.dependsOn(generateProtoTask)
task.source(generateProtoTask.get().outputSourceDirectories)
}
Expand All @@ -374,20 +374,20 @@ class ProtobufPlugin implements Plugin<Project> {
) {
String sourceSetName = protoSourceSet.name
String taskName = 'generate' + Utils.getSourceSetSubstringForTaskNames(sourceSetName) + 'Proto'
String defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir
Provider<String> defaultGeneratedFilesBaseDir = protobufExtension.defaultGeneratedFilesBaseDir
Provider<String> generatedFilesBaseDirProvider = protobufExtension.generatedFilesBaseDirProperty
Provider<GenerateProtoTask> task = project.tasks.register(taskName, GenerateProtoTask) {
CopyActionFacade copyActionFacade = CopyActionFacade.Loader.create(it.project, it.objectFactory)
it.description = "Compiles Proto source for '${sourceSetName}'".toString()
it.outputBaseDir = project.providers.provider {
"${defaultGeneratedFilesBaseDir}/${sourceSetName}".toString()
it.outputBaseDir = defaultGeneratedFilesBaseDir.map {
"${it}/${sourceSetName}".toString()
}
it.addSourceDirs(protoSourceSet.proto)
it.addIncludeDir(protoSourceSet.proto.sourceDirectories)
it.addIncludeDir(protoSourceSet.includeProtoDirs)
it.doLast { task ->
String generatedFilesBaseDir = generatedFilesBaseDirProvider.get()
if (generatedFilesBaseDir == defaultGeneratedFilesBaseDir) {
if (generatedFilesBaseDir == defaultGeneratedFilesBaseDir.get()) {
return
}
// Purposefully don't wire this up to outputs, as it can be mixed with other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import spock.lang.Unroll

@CompileDynamic
class ProtobufAndroidPluginKotlinTest extends Specification {
private static final List<String> GRADLE_VERSION = ["5.6", "6.5.1", "7.4.2"]
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0", "4.1.0", "7.2.1"]
private static final List<String> KOTLIN_VERSION = ["1.3.20", "1.3.20", "1.3.40"]
private static final List<String> GRADLE_VERSION = ["5.6", "6.5.1", "7.4.2", "7.6"]
private static final List<String> ANDROID_PLUGIN_VERSION = ["3.5.0", "4.1.0", "7.2.1", "7.3.1"]
private static final List<String> KOTLIN_VERSION = ["1.3.20", "1.3.20", "1.3.40", "1.7.20"]

/**
* This test may take a significant amount of Gradle daemon Metaspace memory in some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import spock.lang.Unroll
@CompileDynamic
class ProtobufJavaPluginTest extends Specification {
// Current supported version is Gradle 5+.
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0", "6.7.1", "7.4.2"]
private static final List<String> KOTLIN_VERSIONS = ["1.3.20", "1.3.31", "1.3.40"]
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0", "6.7.1", "7.4.2", "7.6"]
private static final List<String> KOTLIN_VERSIONS = ["1.3.72", "1.4.32", "1.5.32", "1.6.21", "1.7.21"]

void "testApplying java and com.google.protobuf adds corresponding task to project"() {
given: "a basic project with java and com.google.protobuf"
Expand Down Expand Up @@ -69,6 +69,18 @@ class ProtobufJavaPluginTest extends Specification {
assert project.tasks.extractMain2Proto instanceof ProtobufExtract
}

void "test buildDir is late bound"() {
given: "a basic project with java and com.google.protobuf"
Project project = setupBasicProject()

when: "project evaluated"
project.evaluate()
project.buildDir = "expect-the-unexpected"

then: "generate tasks added"
assert project.tasks.generateProto.outputBaseDir.contains("/expect-the-unexpected/")
}

@Unroll
void "testProject should be successfully executed (java-only project) [gradle #gradleVersion]"() {
given: "project from testProject"
Expand Down Expand Up @@ -196,7 +208,8 @@ class ProtobufJavaPluginTest extends Specification {
ProtobufPluginTestHelper.verifyProjectDir(projectDir)

where:
[gradleVersion, kotlinVersion] << [GRADLE_VERSIONS, KOTLIN_VERSIONS].combinations()
gradleVersion << GRADLE_VERSIONS
kotlinVersion << KOTLIN_VERSIONS
}

@Unroll
Expand All @@ -219,7 +232,8 @@ class ProtobufJavaPluginTest extends Specification {
ProtobufPluginTestHelper.verifyProjectDir(projectDir)

where:
[gradleVersion, kotlinVersion] << [GRADLE_VERSIONS, KOTLIN_VERSIONS].combinations()
gradleVersion << GRADLE_VERSIONS
kotlinVersion << KOTLIN_VERSIONS
}

@Unroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ final class ProtobufPluginTestHelper {
.withProjectDir(projectDir)
.withArguments(args)
.withGradleVersion(gradleVersion)
.withEnvironment(System.getenv()) // Enable forking
.forwardStdOutput(new OutputStreamWriter(System.out))
.forwardStdError(new OutputStreamWriter(System.err))
}
Expand Down