Skip to content

Commit 207a3aa

Browse files
committed
Improve the Gradle plugin's API and configuration's API.
1 parent 8d3cdee commit 207a3aa

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt

+13-30
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
9494
project.providers.gradleProperty("love.forte.plugin.suspend-transform.suppressDeprecatedExtensionWarn")
9595
.orNull.toBoolean()
9696

97-
9897
val msg = "WARN: The `love.forte.plugin.suspendtrans.gradle.SuspendTransformGradleExtension` " +
9998
"(`suspendTransform { ... }`) is deprecated, \n" +
10099
"please use `love.forte.plugin.suspendtrans.gradle.SuspendTransformPluginExtension` " +
@@ -255,45 +254,29 @@ private fun DeprecatedIncludeAnnotation.toIncludeAnnotation(): IncludeAnnotation
255254
)
256255
}
257256

258-
private fun SuspendTransformPluginExtension.toSubpluginOptions(
259-
target: KotlinTarget,
260-
project: Project
261-
): List<SubpluginOption> {
262-
// If not enabled or transformer is empty.
263-
val cliConfig = SuspendTransformCliOptions.CLI_CONFIGURATION
264-
val configuration = toConfiguration()
265-
return if (configuration.transformers.isNotEmpty()) {
266-
if (project.logger.isInfoEnabled) {
267-
val count = configuration.transformers.values.sumOf { it.size }
268-
project.logger.debug("The suspend transform is enabled with {} transformer(s) for {}", count, target)
269-
}
270-
listOf(SubpluginOption(cliConfig.optionName, configuration.encodeToHex()))
271-
} else {
272-
project.logger.debug("The suspend transform is disabled or transformers are empty for {}.", target)
273-
emptyList()
274-
}
275-
}
276-
277257
private fun SuspendTransformPluginExtension.toSubpluginOptionsProvider(
278258
target: KotlinTarget,
279259
project: Project
280260
): Provider<List<SubpluginOption>> {
281261
return enabled
282-
.map { isEnabled ->
262+
.flatMap { isEnabled ->
283263
if (!isEnabled) {
284264
project.logger.debug("The suspend transform is disabled for {}.", target)
285-
return@map emptyList()
265+
return@flatMap project.provider { emptyList() }
286266
}
287267

288-
val configuration = toConfiguration()
289-
val transformers = configuration.transformers
290-
if (transformers.isEmpty()) {
291-
project.logger.debug("The suspend transform is enabled but transformers are empty for {}.", target)
292-
return@map emptyList()
293-
}
268+
val configurationProvider = toConfigurationProvider(project.objects)
294269

295-
val cliConfig = SuspendTransformCliOptions.CLI_CONFIGURATION
296-
listOf(SubpluginOption(cliConfig.optionName, configuration.encodeToHex()))
270+
configurationProvider.map { configuration ->
271+
val transformers = configuration.transformers
272+
if (transformers.isEmpty()) {
273+
project.logger.debug("The suspend transform is enabled but transformers are empty for {}.", target)
274+
return@map emptyList()
275+
}
276+
277+
val cliConfig = SuspendTransformCliOptions.CLI_CONFIGURATION
278+
listOf(SubpluginOption(cliConfig.optionName, configuration.encodeToHex()))
279+
}
297280
}
298281

299282
}

plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformPluginExtension.kt

+40-7
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ internal interface NamedTransformerSpecListContainerInternal : NamedTransformerS
3535
override val platform: Property<TargetPlatform>
3636
}
3737

38-
@RequiresOptIn(
39-
"This API is an experimental public TransformersContainer's api. " +
40-
"It may be changed in the future without notice."
41-
)
42-
annotation class ExperimentalTransformersContainerApi
43-
4438
/**
4539
* @since 0.12.0
4640
*/
@@ -285,7 +279,7 @@ abstract class SuspendTransformPluginExtension
285279
}
286280
}
287281

288-
@OptIn(InternalSuspendTransformConfigurationApi::class, ExperimentalTransformersContainerApi::class)
282+
@OptIn(InternalSuspendTransformConfigurationApi::class)
289283
internal fun SuspendTransformPluginExtension.toConfiguration(): SuspendTransformConfiguration {
290284
return SuspendTransformConfiguration(
291285
// 此处 Map 可能为 空,但是 List 不会有空的。
@@ -304,6 +298,45 @@ internal fun SuspendTransformPluginExtension.toConfiguration(): SuspendTransform
304298
)
305299
}
306300

301+
internal data class TransformerEntry(
302+
val targetPlatform: TargetPlatform,
303+
val transformers: List<Transformer>
304+
)
305+
306+
@OptIn(InternalSuspendTransformConfigurationApi::class)
307+
internal fun SuspendTransformPluginExtension.toConfigurationProvider(objects: ObjectFactory): Provider<SuspendTransformConfiguration> {
308+
val combines = objects.listProperty(TransformerEntry::class.java)
309+
for ((targetPlatform, transformerListProperty) in transformers._containers) {
310+
combines.addAll(
311+
transformerListProperty.map { list ->
312+
if (list.isEmpty()) {
313+
// Type mismatch: inferred type is TransformerEntry? but TransformerEntry was expected
314+
// if return null with `combines.add`
315+
emptyList()
316+
} else {
317+
listOf(
318+
TransformerEntry(targetPlatform, list.map { it.toTransformer() })
319+
)
320+
}
321+
}
322+
)
323+
}
324+
325+
return combines.map { entries ->
326+
val transformersMap: Map<TargetPlatform, List<Transformer>> = entries.associateBy(
327+
keySelector = { it.targetPlatform },
328+
valueTransform = { it.transformers }
329+
)
330+
331+
// 此处 `Map` 可能为 空,但是 `List` 不会有空的。
332+
// 后续在使用的时候只需要判断一下 transformers 本身是不是空即可。
333+
SuspendTransformConfiguration(
334+
transformers = transformersMap
335+
)
336+
}
337+
338+
}
339+
307340
/**
308341
* @since 0.12.0
309342
*/

settings.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ include(":plugins:suspend-transform-plugin-gradle")
3535
// include(":local-helper")
3636

3737
//Samples
38-
// include(":tests:test-jvm")
39-
// include(":tests:test-js")
40-
// include(":tests:test-kmp")
38+
include(":tests:test-jvm")
39+
include(":tests:test-js")
40+
include(":tests:test-kmp")
4141
// include(":tests:test-android")

tests/test-jvm/build.gradle.kts

+12-12
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ dependencies {
3232
api(libs.kotlinx.coroutines.core)
3333
}
3434

35-
@Suppress("DEPRECATION")
36-
suspendTransform {
37-
enabled = true
38-
includeAnnotation = false
39-
includeRuntime = false
40-
useDefault()
41-
}
42-
43-
// suspendTransformPlugin {
35+
// @Suppress("DEPRECATION")
36+
// suspendTransform {
37+
// enabled = true
4438
// includeAnnotation = false
4539
// includeRuntime = false
46-
// transformers {
47-
// useDefault()
48-
// }
40+
// useDefault()
4941
// }
5042

43+
suspendTransformPlugin {
44+
includeAnnotation = false
45+
includeRuntime = false
46+
transformers {
47+
useDefault()
48+
}
49+
}
50+
5151
/*
5252
> val blockingBaseName: String = "",
5353
> val blockingSuffix: String = "Blocking",

tests/test-kmp/build.gradle.kts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import love.forte.plugin.suspendtrans.ClassInfo
2-
import love.forte.plugin.suspendtrans.SuspendTransformConfiguration
3-
41
plugins {
52
kotlin("multiplatform")
63
id("love.forte.plugin.suspend-transform")
@@ -89,9 +86,9 @@ suspendTransform {
8986
includeAnnotation = false
9087
useJvmDefault()
9188
addJsTransformers(
92-
SuspendTransformConfiguration.jsPromiseTransformer.copy(
89+
love.forte.plugin.suspendtrans.SuspendTransformConfiguration.jsPromiseTransformer.copy(
9390
copyAnnotationExcludes = listOf(
94-
ClassInfo("kotlin.js", "JsExport.Ignore")
91+
love.forte.plugin.suspendtrans.ClassInfo("kotlin.js", "JsExport.Ignore")
9592
)
9693
)
9794
)

0 commit comments

Comments
 (0)