1
1
package love.forte.plugin.suspendtrans.gradle
2
2
3
3
import love.forte.plugin.suspendtrans.CliOptions
4
+ import love.forte.plugin.suspendtrans.gradle.DependencyConfigurationName.*
4
5
import org.gradle.api.Project
5
6
import org.gradle.api.provider.Provider
6
7
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
@@ -18,7 +19,12 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
18
19
}
19
20
20
21
override fun isApplicable (kotlinCompilation : KotlinCompilation <* >): Boolean {
21
- return kotlinCompilation.target.project.plugins.hasPlugin(SuspendTransformGradlePlugin ::class .java)
22
+ val project = kotlinCompilation.target.project
23
+
24
+ val isApplicable = project.plugins.hasPlugin(SuspendTransformGradlePlugin ::class .java)
25
+ && project.configOrNull?.enabled != false
26
+
27
+ return isApplicable
22
28
}
23
29
24
30
override fun getCompilerPluginId (): String = SuspendTransPluginConstants .KOTLIN_PLUGIN_ID
@@ -68,6 +74,15 @@ private fun SuspendTransformGradleExtension.toSubpluginOptions(): List<Subplugin
68
74
69
75
private fun Project.configureDependencies () {
70
76
fun Project.include (platform : Platform , conf : SuspendTransformGradleExtension ) {
77
+ if (! conf.enabled) {
78
+ logger.info(
79
+ " The `SuspendTransformGradleExtension.enable` in project {} for platform {} is `false`, skip config." ,
80
+ this ,
81
+ platform
82
+ )
83
+ return
84
+ }
85
+
71
86
if (conf.includeAnnotation) {
72
87
val notation = getDependencyNotation(
73
88
SuspendTransPluginConstants .ANNOTATION_GROUP ,
@@ -78,7 +93,7 @@ private fun Project.configureDependencies() {
78
93
if (platform == Platform .JVM ) {
79
94
dependencies.add(conf.annotationConfigurationName, notation)
80
95
} else {
81
- // JS, native 似乎不支持其他的 name,例如 compileOnly
96
+ // JS, native 似乎不支持 compileOnly
82
97
dependencies.add(" implementation" , notation)
83
98
}
84
99
dependencies.add(" testImplementation" , notation)
@@ -131,7 +146,20 @@ fun Project.withPluginWhenEvaluatedConf(
131
146
}
132
147
}
133
148
149
+ private enum class DependencyConfigurationName {
150
+ API , IMPLEMENTATION , COMPILE_ONLY
151
+ }
152
+
134
153
fun Project.configureMultiplatformDependency (conf : SuspendTransformGradleExtension ) {
154
+ if (! conf.enabled) {
155
+ logger.info(
156
+ " The `SuspendTransformGradleExtension.enable` in project {} for multiplatform is `false`, skip config." ,
157
+ this ,
158
+ )
159
+ return
160
+ }
161
+
162
+ // 时间久远,已经忘记为什么要做这个判断了,也忘记这段是在哪儿参考来的了💀
135
163
if (rootProject.getBooleanProperty(" kotlin.mpp.enableGranularSourceSetsMetadata" )) {
136
164
val multiplatformExtensions = project.extensions.getByType(KotlinMultiplatformExtension ::class .java)
137
165
@@ -197,51 +225,102 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtensi
197
225
} else {
198
226
sourceSetsByCompilation().forEach { (sourceSet, compilations) ->
199
227
val platformTypes = compilations.map { it.platformType }.toSet()
200
- val compilationNames = compilations.map { it.compilationName }.toSet()
201
- if (compilationNames.size != 1 )
202
- error(" Source set '${sourceSet.name} ' of project '$name ' is part of several compilations $compilationNames " )
203
- val compilationType = compilationNames.single().compilationNameToType()
204
- ? : return @forEach // skip unknown compilations
205
- val platform =
206
- if (platformTypes.size > 1 ) Platform .MULTIPLATFORM else // mix of platform types -> "common"
207
- when (platformTypes.single()) {
228
+ logger.info(
229
+ " Configure sourceSet [{}]. compilations: {}, platformTypes: {}" ,
230
+ sourceSet,
231
+ compilations,
232
+ platformTypes
233
+ )
234
+
235
+ // TODO 可能同一个 sourceSet 会出现重复,但是需要处理吗?
236
+ for (compilation in compilations) {
237
+ val platformType = compilation.platformType
238
+ val compilationName = compilation.compilationName
239
+ val compilationType = compilationName.compilationNameToType()
240
+
241
+ logger.info(
242
+ " compilation platformType: {}, compilationName: {}, compilationType: {}" ,
243
+ platformType,
244
+ compilationName,
245
+ compilationType
246
+ )
247
+
248
+ val platform = if (platformTypes.size > 1 ) {
249
+ Platform .MULTIPLATFORM
250
+ } else {
251
+ // mix of platform types -> "common"
252
+ when (platformType) {
208
253
KotlinPlatformType .common -> Platform .MULTIPLATFORM
209
254
KotlinPlatformType .jvm, KotlinPlatformType .androidJvm -> Platform .JVM
210
255
KotlinPlatformType .js -> Platform .JS
211
256
KotlinPlatformType .native, KotlinPlatformType .wasm -> Platform .NATIVE
212
257
}
258
+ }
259
+
260
+ if (conf.includeAnnotation) {
261
+ val configurationName = when {
262
+ // impl dependency for native (there is no transformation)
263
+ platform == Platform .NATIVE -> IMPLEMENTATION // sourceSet.implementationConfigurationName
264
+ // compileOnly dependency for JVM main compilation (jvmMain, androidMain)
265
+ compilationType == CompilationType .MAIN &&
266
+ platform == Platform .JVM -> COMPILE_ONLY // sourceSet.compileOnlyConfigurationName
267
+ // impl dependency for tests, and others
268
+ else -> IMPLEMENTATION // sourceSet.implementationConfigurationName
269
+ }
270
+
271
+ val notation = getDependencyNotation(
272
+ SuspendTransPluginConstants .ANNOTATION_GROUP ,
273
+ SuspendTransPluginConstants .ANNOTATION_NAME ,
274
+ platform,
275
+ conf.annotationDependencyVersion
276
+ )
277
+
278
+ sourceSet.dependencies {
279
+ when (configurationName) {
280
+ API -> {
281
+ api(notation)
282
+ }
283
+
284
+ IMPLEMENTATION -> {
285
+ implementation(notation)
286
+ }
287
+
288
+ COMPILE_ONLY -> {
289
+ compileOnly(notation)
290
+ }
291
+ }
292
+ }
213
293
214
- if (conf.includeAnnotation) {
215
- val configurationName = when {
216
- // impl dependency for native (there is no transformation)
217
- platform == Platform .NATIVE -> sourceSet.implementationConfigurationName
218
- // compileOnly dependency for main compilation (commonMain, jvmMain, jsMain)
219
- compilationType == CompilationType .MAIN -> sourceSet.compileOnlyConfigurationName
220
- // impl dependency for tests
221
- else -> sourceSet.implementationConfigurationName
294
+ // dependencies.add(configurationName, notation)
295
+ logger.debug(
296
+ " Add annotation dependency: {} {} for sourceSet {}" ,
297
+ configurationName,
298
+ notation,
299
+ sourceSet
300
+ )
222
301
}
223
302
224
- val notation = getDependencyNotation(
225
- SuspendTransPluginConstants .ANNOTATION_GROUP ,
226
- SuspendTransPluginConstants .ANNOTATION_NAME ,
227
- platform,
228
- conf.annotationDependencyVersion
229
- )
230
- dependencies.add(configurationName, notation)
231
- }
303
+ if (conf.includeRuntime) {
304
+ // val configurationName = sourceSet.implementationConfigurationName
232
305
233
- if (conf.includeRuntime) {
234
- val configurationName = sourceSet.implementationConfigurationName
306
+ val notation = getDependencyNotation(
307
+ SuspendTransPluginConstants .RUNTIME_GROUP ,
308
+ SuspendTransPluginConstants .RUNTIME_NAME ,
309
+ platform,
310
+ conf.runtimeDependencyVersion
311
+ )
312
+ sourceSet.dependencies {
313
+ implementation(notation)
314
+ }
235
315
236
- val notation = getDependencyNotation (
237
- SuspendTransPluginConstants . RUNTIME_GROUP ,
238
- SuspendTransPluginConstants . RUNTIME_NAME ,
239
- platform ,
240
- conf.runtimeDependencyVersion
241
- )
242
- dependencies.add(configurationName, notation)
316
+ logger.debug (
317
+ " Add runtime dependency: {} {} for sourceSet {} " ,
318
+ IMPLEMENTATION ,
319
+ notation ,
320
+ sourceSet
321
+ )
322
+ }
243
323
}
244
-
245
324
}
246
325
}
247
326
}
@@ -255,7 +334,7 @@ fun Project.withKotlinTargets(fn: (KotlinTarget) -> Unit) {
255
334
}
256
335
257
336
fun Project.sourceSetsByCompilation (): Map <KotlinSourceSet , List <KotlinCompilation <* >>> {
258
- val sourceSetsByCompilation = hashMapOf <KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
337
+ val sourceSetsByCompilation = mutableMapOf <KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
259
338
withKotlinTargets { target ->
260
339
target.compilations.forEach { compilation ->
261
340
compilation.allKotlinSourceSets.forEach { sourceSet ->
@@ -275,7 +354,10 @@ private fun String.compilationNameToType(): CompilationType? = when (this) {
275
354
}
276
355
277
356
private val Project .config: SuspendTransformGradleExtension
278
- get() = extensions.findByType(SuspendTransformGradleExtension ::class .java) ? : SuspendTransformGradleExtension ()
357
+ get() = configOrNull ? : SuspendTransformGradleExtension ()
358
+
359
+ private val Project .configOrNull: SuspendTransformGradleExtension ?
360
+ get() = extensions.findByType(SuspendTransformGradleExtension ::class .java)
279
361
280
362
private enum class Platform (val suffix : String ) {
281
363
JVM (" -jvm" ), JS (" -js" ), NATIVE (" " ), MULTIPLATFORM (" " )
0 commit comments