17
17
18
18
package com.squareup.wire.gradle
19
19
20
+ import java.lang.reflect.Array as JavaArray
21
+ import com.android.build.api.attributes.BuildTypeAttr
20
22
import com.squareup.wire.VERSION
21
23
import com.squareup.wire.gradle.internal.libraryProtoOutputPath
22
24
import com.squareup.wire.gradle.internal.targetDefaultOutputPath
@@ -26,7 +28,6 @@ import com.squareup.wire.schema.ProtoTarget
26
28
import com.squareup.wire.schema.Target
27
29
import com.squareup.wire.schema.newEventListenerFactory
28
30
import java.io.File
29
- import java.lang.reflect.Array as JavaArray
30
31
import java.util.concurrent.atomic.AtomicBoolean
31
32
import org.gradle.api.Plugin
32
33
import org.gradle.api.Project
@@ -37,6 +38,7 @@ import org.gradle.api.tasks.SourceSetContainer
37
38
import org.gradle.api.tasks.compile.JavaCompile
38
39
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
39
40
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
41
+ import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
40
42
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
41
43
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
42
44
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
@@ -68,7 +70,11 @@ class WirePlugin : Plugin<Project> {
68
70
// TODO(Benoit) If another project, on which this one depends, exposes multiple variants,
69
71
// Wire won't be able to pick one. We force the resolution to JVM. On the other hand, this
70
72
// breaks inter-module dependencies for non-jvm modules. We need to fix it.
71
- attributesContainer.attribute(KotlinPlatformType .attribute, KotlinPlatformType .jvm)
73
+ attributesContainer.attribute(KotlinPlatformType .attribute, KotlinPlatformType .androidJvm)
74
+ attributesContainer.attribute(
75
+ BuildTypeAttr .ATTRIBUTE ,
76
+ project.objects.named(BuildTypeAttr ::class .java, " debug" ),
77
+ )
72
78
}
73
79
}
74
80
@@ -120,7 +126,8 @@ class WirePlugin : Plugin<Project> {
120
126
121
127
val protoSourceConfiguration = project.configurations.getByName(" protoSource" )
122
128
val protoPathConfiguration = project.configurations.getByName(" protoPath" )
123
- val projectDependenciesJvmConfiguration = project.configurations.getByName(" protoProjectDependenciesJvm" )
129
+ val projectDependenciesJvmConfiguration =
130
+ project.configurations.getByName(" protoProjectDependenciesJvm" )
124
131
125
132
val outputs = extension.outputs
126
133
check(outputs.isNotEmpty()) {
@@ -258,7 +265,8 @@ class WirePlugin : Plugin<Project> {
258
265
task.projectDirProperty.set(project.layout.projectDirectory)
259
266
task.buildDirProperty.set(project.layout.buildDirectory)
260
267
261
- val factories = extension.eventListenerFactories + extension.eventListenerFactoryClasses().map(::newEventListenerFactory)
268
+ val factories = extension.eventListenerFactories + extension.eventListenerFactoryClasses()
269
+ .map(::newEventListenerFactory)
262
270
task.eventListenerFactories.set(factories)
263
271
}
264
272
@@ -271,17 +279,27 @@ class WirePlugin : Plugin<Project> {
271
279
272
280
val protoOutputDirectory = task.map { it.protoLibraryOutput }
273
281
if (extension.protoLibrary) {
274
- val sourceSets = project.extensions.getByType(SourceSetContainer ::class .java)
275
- // Note that there are no source sets for some platforms such as native.
276
- // TODO(Benoit) Probably should be checking for other names than `main`. As well, source
277
- // sets might be created 'afterEvaluate'. Does that mean we should do this work in
278
- // `afterEvaluate` as well? See: https://kotlinlang.org/docs/multiplatform-dsl-reference.html#source-sets
279
- if (sourceSets.findByName(" main" ) != null ) {
280
- sourceSets.getByName(" main" ) { main: SourceSet ->
281
- main.resources.srcDir(protoOutputDirectory)
282
+ val sourceContainerNames = project.extensions.extensionsSchema.map { it.name }
283
+ sourceContainerNames.forEach { sourceContainerName ->
284
+ val extension = project.extensions.getByName(sourceContainerName)
285
+ when (extension) {
286
+ is SourceSetContainer -> {
287
+ extension.findByName(" main" )?.let { main: SourceSet ->
288
+ main.resources.srcDir(protoOutputDirectory)
289
+ }
290
+ }
291
+
292
+ is KotlinProjectExtension -> {
293
+ extension.sourceSets.findByName(" main" )?.resources?.srcDir(protoOutputDirectory)
294
+ }
295
+
296
+ else -> {
297
+ project.logger.warn(
298
+ " ${project.displayName} doesn't have a 'main' source sets. The .proto files will not automatically be added to the artifact." ,
299
+ )
300
+ }
282
301
}
283
- } else {
284
- project.logger.warn(" ${project.displayName} doesn't have a 'main' source sets. The .proto files will not automatically be added to the artifact." )
302
+ (project.extensions.getByName(" kotlin" ) as ? KotlinProjectExtension )?.sourceSets
285
303
}
286
304
}
287
305
0 commit comments