Skip to content

Commit da15eb0

Browse files
committed
Android + KMP setup
1 parent d18c095 commit da15eb0

File tree

13 files changed

+618
-256
lines changed

13 files changed

+618
-256
lines changed

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/BufExecTask.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package kotlinx.rpc.buf.tasks
66

7-
import com.android.build.api.variant.Variant
87
import kotlinx.rpc.buf.BUF_EXECUTABLE_CONFIGURATION
98
import kotlinx.rpc.buf.BufExtension
109
import kotlinx.rpc.buf.execBuf
@@ -40,7 +39,7 @@ import javax.inject.Inject
4039
*/
4140
public abstract class BufExecTask @Inject constructor(
4241
@Internal
43-
public val properties: Properties,
42+
public val properties: Provider<Properties>,
4443
) : DefaultTask() {
4544
init {
4645
group = PROTO_GROUP
@@ -89,29 +88,36 @@ public abstract class BufExecTask @Inject constructor(
8988
public class AndroidProperties internal constructor(
9089
isTest: Boolean,
9190
sourceSetName: String,
91+
9292
/**
9393
* Name of the android flavors this task is associated with.
9494
*
95-
* @see [Variant.productFlavors]
95+
* Can be empty for 'com.android.kotlin.multiplatform.library' source sets.
96+
*
97+
* @see com.android.build.api.variant.Variant.productFlavors
9698
*/
9799
public val flavors: List<String>,
100+
98101
/**
99102
* Name of the android build type this task is associated with.
100103
*
101-
* @see Variant.buildType
104+
* @see com.android.build.api.variant.Variant.buildType
102105
*/
103106
public val buildType: String?,
107+
104108
/**
105109
* Name of the android variant this task is associated with.
106110
*
107-
* @see Variant.name
111+
* Can be `null` for 'com.android.kotlin.multiplatform.library' source sets.
112+
*
113+
* @see com.android.build.api.variant.Variant.name
108114
*/
109-
public val variant: String,
115+
public val variant: String?,
110116

111117
/**
112118
* Whether the task is for instrumentation tests.
113119
*/
114-
public val isAndroidTest: Boolean,
120+
public val isInstrumentedTest: Boolean,
115121

116122
/**
117123
* Whether the task is for unit tests.
@@ -179,7 +185,7 @@ public abstract class BufExecTask @Inject constructor(
179185
public inline fun <reified T : BufExecTask> Project.registerBufExecTask(
180186
name: String,
181187
workingDir: Provider<File>,
182-
properties: BufExecTask.Properties,
188+
properties: Provider<BufExecTask.Properties>,
183189
noinline configuration: T.() -> Unit,
184190
): TaskProvider<T> = registerBufExecTask(T::class, name, workingDir, properties, configuration)
185191

@@ -188,7 +194,7 @@ internal fun <T : BufExecTask> Project.registerBufExecTask(
188194
clazz: KClass<T>,
189195
name: String,
190196
workingDir: Provider<File>,
191-
properties: BufExecTask.Properties,
197+
properties: Provider<BufExecTask.Properties>,
192198
configuration: T.() -> Unit = {},
193199
): TaskProvider<T> = tasks.register(name, clazz, properties).apply {
194200
configure {

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/BufGenerateTask.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import kotlinx.rpc.protoc.DefaultProtoSourceSet
2020
import kotlinx.rpc.protoc.bufExecProperties
2121
import org.gradle.api.file.SourceDirectorySet
2222
import org.gradle.api.provider.Provider
23-
import org.gradle.api.tasks.InputDirectory
2423
import org.gradle.api.tasks.InputFiles
2524
import org.gradle.api.tasks.OutputDirectories
26-
import org.gradle.api.tasks.SkipWhenEmpty
2725
import org.gradle.kotlin.dsl.listProperty
2826
import javax.inject.Inject
2927

@@ -32,7 +30,9 @@ import javax.inject.Inject
3230
*
3331
* @see <a href="https://buf.build/docs/reference/cli/buf/generate/">buf generate</a>
3432
*/
35-
public abstract class BufGenerateTask @Inject internal constructor(properties: Properties) : BufExecTask(properties) {
33+
public abstract class BufGenerateTask @Inject internal constructor(
34+
properties: Provider<Properties>,
35+
) : BufExecTask(properties) {
3636
// used to properly calculate output directories
3737
@get:Input
3838
internal abstract val pluginNames: ListProperty<String>

gradle-plugin/src/main/kotlin/kotlinx/rpc/buf/tasks/BufTasks.kt

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -330,78 +330,68 @@ public sealed interface BufTasks<BufTask : BufExecTask> : TaskCollection<BufTask
330330
*/
331331
public fun nonTestTasks(): BufTasks<BufTask>
332332

333-
// android
334-
335333
/**
336-
* Android only.
337-
*
338334
* Filters tasks by where [BufExecTask.AndroidProperties.isUnitTest] is `true`.
339335
*
340336
* ```kotlin
341337
* rpc.protoc {
342-
* buf.tasks.all().unitTestTasks()
338+
* buf.tasks.all().androidUnitTestTasks()
343339
* }
344340
* ```
345341
*/
346-
public fun unitTestTasks(): BufTasks<BufTask>
342+
public fun androidUnitTestTasks(): BufTasks<BufTask>
347343

348344
/**
349-
* Android only.
350-
*
351-
* Filters tasks by where [BufExecTask.AndroidProperties.isAndroidTest] is `true`.
345+
* Filters tasks by where [BufExecTask.AndroidProperties.isInstrumentedTest] is `true`.
352346
*
353347
* ```kotlin
354348
* rpc.protoc {
355-
* buf.tasks.all().androidTestTasks()
349+
* buf.tasks.all().androidInstrumentedTestTasks()
356350
* }
357351
* ```
358352
*/
359-
public fun androidTestTasks(): BufTasks<BufTask>
353+
public fun androidInstrumentedTestTasks(): BufTasks<BufTask>
360354

361355
/**
362-
* Android only.
363-
*
364356
* Filters tasks by where [BufExecTask.AndroidProperties.flavors] matches the given flavor.
365357
*
358+
* When `null` is passed, only variants without flavors are returned.
359+
*
366360
* Only returns Android tasks.
367361
*
368362
* ```kotlin
369363
* rpc.protoc {
370-
* buf.tasks.all().matchingFlavor("freeApp")
364+
* buf.tasks.all().matchingAndroidFlavor("freeApp")
371365
* }
372366
* ```
373367
*/
374-
public fun matchingFlavor(flavor: String): BufTasks<BufTask>
368+
public fun matchingAndroidFlavor(flavor: String?): BufTasks<BufTask>
375369

376370
/**
377-
* Android only.
378-
*
379371
* Filters tasks by where [BufExecTask.AndroidProperties.buildType] matches the given buildType.
380372
*
381373
* Only returns Android tasks.
382374
*
383375
* ```kotlin
384376
* rpc.protoc {
385-
* buf.tasks.all().matchingBuildType("debug")
377+
* buf.tasks.all().matchingAndroidBuildType("debug")
386378
* }
387379
* ```
388380
*/
389-
public fun matchingBuildType(buildType: String?): BufTasks<BufTask>
381+
public fun matchingAndroidBuildType(buildType: String?): BufTasks<BufTask>
390382

391383
/**
392-
* Android only.
393-
*
394384
* Filters tasks by where [BufExecTask.AndroidProperties.variant] matches the given variant.
395385
*
396386
* Only returns Android tasks.
397387
*
398388
* ```kotlin
399389
* rpc.protoc {
400-
* buf.tasks.all().matchingVariant("freeAppDebug")
390+
* buf.tasks.all().matchingAndroidVariant("freeAppDebug")
401391
* }
402392
* ```
403393
*/
404-
public fun matchingVariant(variant: String): BufTasks<BufTask>
394+
public fun matchingAndroidVariant(variant: String?): BufTasks<BufTask>
405395
}
406396

407397
/**
@@ -503,7 +493,7 @@ internal open class BufTasksImpl<BufTask : BufExecTask> internal constructor(
503493
override fun matchingSourceSet(sourceSetName: String): BufTasks<BufTask> {
504494
return BufTasksImpl(
505495
project,
506-
collection.matching { it.properties.sourceSetName == sourceSetName },
496+
collection.matching { it.properties.get().sourceSetName == sourceSetName },
507497
kClass
508498
)
509499
}
@@ -526,15 +516,15 @@ internal open class BufTasksImpl<BufTask : BufExecTask> internal constructor(
526516

527517
override fun executedForSourceSet(sourceSetName: String): BufTasks<BufTask> {
528518
val allExecuted = project.tasks.withType(kClass.java).matching {
529-
it.properties.sourceSetName == sourceSetName
519+
it.properties.get().sourceSetName == sourceSetName
530520
}.singleOrNull()?.bufDependsOn(kClass) ?: return empty()
531521

532522
val allExecutedLazySet = lazy { allExecuted.map { it.name }.toSet() }
533523

534524
return BufTasksImpl(
535525
project = project,
536526
collection = collection.matching { dependency ->
537-
dependency.properties.sourceSetName == sourceSetName || dependency.name in allExecutedLazySet.value
527+
dependency.properties.get().sourceSetName == sourceSetName || dependency.name in allExecutedLazySet.value
538528
},
539529
kClass = kClass,
540530
)
@@ -557,18 +547,18 @@ internal open class BufTasksImpl<BufTask : BufExecTask> internal constructor(
557547
}
558548

559549
override fun testTasks(): BufTasks<BufTask> {
560-
return BufTasksImpl(project, collection.matching { it.properties.isTest }, kClass)
550+
return BufTasksImpl(project, collection.matching { it.properties.get().isTest }, kClass)
561551
}
562552

563553
override fun nonTestTasks(): BufTasks<BufTask> {
564-
return BufTasksImpl(project, collection.matching { !it.properties.isTest }, kClass)
554+
return BufTasksImpl(project, collection.matching { !it.properties.get().isTest }, kClass)
565555
}
566556

567-
override fun unitTestTasks(): BufTasks<BufTask> {
557+
override fun androidUnitTestTasks(): BufTasks<BufTask> {
568558
return BufTasksImpl(
569559
project = project,
570560
collection = collection.matching {
571-
val properties = it.properties as? BufExecTask.AndroidProperties
561+
val properties = it.properties.get() as? BufExecTask.AndroidProperties
572562
?: return@matching false
573563

574564
properties.isUnitTest
@@ -577,37 +567,41 @@ internal open class BufTasksImpl<BufTask : BufExecTask> internal constructor(
577567
)
578568
}
579569

580-
override fun androidTestTasks(): BufTasks<BufTask> {
570+
override fun androidInstrumentedTestTasks(): BufTasks<BufTask> {
581571
return BufTasksImpl(
582572
project = project,
583573
collection = collection.matching {
584-
val properties = it.properties as? BufExecTask.AndroidProperties
574+
val properties = it.properties.get() as? BufExecTask.AndroidProperties
585575
?: return@matching false
586576

587-
properties.isAndroidTest
577+
properties.isInstrumentedTest
588578
},
589579
kClass = kClass,
590580
)
591581
}
592582

593-
override fun matchingFlavor(flavor: String): BufTasks<BufTask> {
583+
override fun matchingAndroidFlavor(flavor: String?): BufTasks<BufTask> {
594584
return BufTasksImpl(
595585
project = project,
596586
collection = collection.matching {
597-
val properties = it.properties as? BufExecTask.AndroidProperties
587+
val properties = it.properties.get() as? BufExecTask.AndroidProperties
598588
?: return@matching false
599589

590+
if (flavor == null) {
591+
return@matching properties.flavors.isEmpty()
592+
}
593+
600594
properties.flavors.contains(flavor)
601595
},
602596
kClass = kClass,
603597
)
604598
}
605599

606-
override fun matchingBuildType(buildType: String?): BufTasks<BufTask> {
600+
override fun matchingAndroidBuildType(buildType: String?): BufTasks<BufTask> {
607601
return BufTasksImpl(
608602
project = project,
609603
collection = collection.matching {
610-
val properties = it.properties as? BufExecTask.AndroidProperties
604+
val properties = it.properties.get() as? BufExecTask.AndroidProperties
611605
?: return@matching false
612606

613607
properties.buildType == buildType
@@ -616,11 +610,11 @@ internal open class BufTasksImpl<BufTask : BufExecTask> internal constructor(
616610
)
617611
}
618612

619-
override fun matchingVariant(variant: String): BufTasks<BufTask> {
613+
override fun matchingAndroidVariant(variant: String?): BufTasks<BufTask> {
620614
return BufTasksImpl(
621615
project = project,
622616
collection = collection.matching {
623-
val properties = it.properties as? BufExecTask.AndroidProperties
617+
val properties = it.properties.get() as? BufExecTask.AndroidProperties
624618
?: return@matching false
625619

626620
properties.variant == variant

0 commit comments

Comments
 (0)