11package love.forte.plugin.suspendtrans.gradle
22
33import love.forte.plugin.suspendtrans.CliOptions
4+ import love.forte.plugin.suspendtrans.gradle.DependencyConfigurationName.*
45import org.gradle.api.Project
56import org.gradle.api.provider.Provider
67import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
@@ -18,7 +19,12 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
1819 }
1920
2021 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
2228 }
2329
2430 override fun getCompilerPluginId (): String = SuspendTransPluginConstants .KOTLIN_PLUGIN_ID
@@ -68,6 +74,15 @@ private fun SuspendTransformGradleExtension.toSubpluginOptions(): List<Subplugin
6874
6975private fun Project.configureDependencies () {
7076 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+
7186 if (conf.includeAnnotation) {
7287 val notation = getDependencyNotation(
7388 SuspendTransPluginConstants .ANNOTATION_GROUP ,
@@ -78,7 +93,7 @@ private fun Project.configureDependencies() {
7893 if (platform == Platform .JVM ) {
7994 dependencies.add(conf.annotationConfigurationName, notation)
8095 } else {
81- // JS, native 似乎不支持其他的 name,例如 compileOnly
96+ // JS, native 似乎不支持 compileOnly
8297 dependencies.add(" implementation" , notation)
8398 }
8499 dependencies.add(" testImplementation" , notation)
@@ -131,7 +146,20 @@ fun Project.withPluginWhenEvaluatedConf(
131146 }
132147}
133148
149+ private enum class DependencyConfigurationName {
150+ API , IMPLEMENTATION , COMPILE_ONLY
151+ }
152+
134153fun 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+ // 时间久远,已经忘记为什么要做这个判断了,也忘记这段是在哪儿参考来的了💀
135163 if (rootProject.getBooleanProperty(" kotlin.mpp.enableGranularSourceSetsMetadata" )) {
136164 val multiplatformExtensions = project.extensions.getByType(KotlinMultiplatformExtension ::class .java)
137165
@@ -197,51 +225,102 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtensi
197225 } else {
198226 sourceSetsByCompilation().forEach { (sourceSet, compilations) ->
199227 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) {
208253 KotlinPlatformType .common -> Platform .MULTIPLATFORM
209254 KotlinPlatformType .jvm, KotlinPlatformType .androidJvm -> Platform .JVM
210255 KotlinPlatformType .js -> Platform .JS
211256 KotlinPlatformType .native, KotlinPlatformType .wasm -> Platform .NATIVE
212257 }
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+ }
213293
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+ )
222301 }
223302
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
232305
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+ }
235315
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+ }
243323 }
244-
245324 }
246325 }
247326}
@@ -255,7 +334,7 @@ fun Project.withKotlinTargets(fn: (KotlinTarget) -> Unit) {
255334}
256335
257336fun Project.sourceSetsByCompilation (): Map <KotlinSourceSet , List <KotlinCompilation <* >>> {
258- val sourceSetsByCompilation = hashMapOf <KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
337+ val sourceSetsByCompilation = mutableMapOf <KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
259338 withKotlinTargets { target ->
260339 target.compilations.forEach { compilation ->
261340 compilation.allKotlinSourceSets.forEach { sourceSet ->
@@ -275,7 +354,10 @@ private fun String.compilationNameToType(): CompilationType? = when (this) {
275354}
276355
277356private 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)
279361
280362private enum class Platform (val suffix : String ) {
281363 JVM (" -jvm" ), JS (" -js" ), NATIVE (" " ), MULTIPLATFORM (" " )
0 commit comments