From 021d8086aefafa5f0e08d1c56179bc1e3edea894 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Wed, 5 Mar 2025 16:25:22 +0800 Subject: [PATCH 1/4] improve Gradle plugin sourceSets config --- buildSrc/src/main/kotlin/IProject.kt | 2 +- .../forte/plugin/suspendtrans/CliOptions.kt | 19 ++- .../suspendtrans/utils/TransformUtil.kt | 1 + .../gradle/SuspendTransformGradlePlugin.kt | 118 ++++++++++++------ settings.gradle.kts | 7 +- tests/test-android/build.gradle.kts | 78 ++++++++++++ tests/test-js/build.gradle.kts | 2 +- tests/test-jvm/build.gradle.kts | 2 +- tests/test-kmp/build.gradle.kts | 2 +- 9 files changed, 178 insertions(+), 53 deletions(-) create mode 100644 tests/test-android/build.gradle.kts diff --git a/buildSrc/src/main/kotlin/IProject.kt b/buildSrc/src/main/kotlin/IProject.kt index bb76690..2546c67 100644 --- a/buildSrc/src/main/kotlin/IProject.kt +++ b/buildSrc/src/main/kotlin/IProject.kt @@ -11,7 +11,7 @@ object IProject : ProjectDetail() { // Remember the libs.versions.toml! val ktVersion = "2.1.0" - val pluginVersion = "0.11.0" + val pluginVersion = "0.11.1" override val version: String = "$ktVersion-$pluginVersion" diff --git a/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/CliOptions.kt b/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/CliOptions.kt index c114185..03ab61e 100644 --- a/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/CliOptions.kt +++ b/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/CliOptions.kt @@ -4,7 +4,6 @@ import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.MapSerializer import kotlinx.serialization.json.Json import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption -import kotlin.reflect.KMutableProperty private val defaultJson = Json { isLenient = true @@ -84,15 +83,15 @@ private class ResolveBuilder { outc = block } - fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty) { - inc { block().setter.call(it) } - out { block().getter.call() } - } - - fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty) { - inc { block().setter.call(it.takeIf { it.isNotEmpty() }) } - out { block().getter.call() ?: "" } - } +// fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty) { +// inc { block().setter.call(it) } +// out { block().getter.call() } +// } +// +// fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty) { +// inc { block().setter.call(it.takeIf { it.isNotEmpty() }) } +// out { block().getter.call() ?: "" } +// } } private fun option( diff --git a/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/TransformUtil.kt b/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/TransformUtil.kt index 42ee932..8ee48b4 100644 --- a/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/TransformUtil.kt +++ b/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/TransformUtil.kt @@ -11,5 +11,6 @@ import org.jetbrains.kotlin.name.Name fun ClassInfo.toClassId(): ClassId = ClassId(packageName.fqn, className.fqn, local) +@Suppress("DEPRECATION") fun FunctionInfo.toCallableId(): CallableId = CallableId(packageName.fqn, className?.fqn, Name.identifier(functionName)) diff --git a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt index c86f9b9..17eb496 100644 --- a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt +++ b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt @@ -1,6 +1,7 @@ package love.forte.plugin.suspendtrans.gradle import love.forte.plugin.suspendtrans.CliOptions +import love.forte.plugin.suspendtrans.gradle.DependencyConfigurationName.* import org.gradle.api.Project import org.gradle.api.provider.Provider import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension @@ -131,6 +132,10 @@ fun Project.withPluginWhenEvaluatedConf( } } +private enum class DependencyConfigurationName { + API, IMPLEMENTATION, COMPILE_ONLY +} + fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtension) { if (rootProject.getBooleanProperty("kotlin.mpp.enableGranularSourceSetsMetadata")) { val multiplatformExtensions = project.extensions.getByType(KotlinMultiplatformExtension::class.java) @@ -197,51 +202,92 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtensi } else { sourceSetsByCompilation().forEach { (sourceSet, compilations) -> val platformTypes = compilations.map { it.platformType }.toSet() - val compilationNames = compilations.map { it.compilationName }.toSet() - if (compilationNames.size != 1) - error("Source set '${sourceSet.name}' of project '$name' is part of several compilations $compilationNames") - val compilationType = compilationNames.single().compilationNameToType() - ?: return@forEach // skip unknown compilations - val platform = - if (platformTypes.size > 1) Platform.MULTIPLATFORM else // mix of platform types -> "common" - when (platformTypes.single()) { + logger.info("platformTypes: {}", platformTypes) + +// val compilationNames = compilations.map { it.compilationName }.toSet() +// logger.info("compilationNames: {}", compilationNames) +// +// if (compilationNames.size != 1) { +// // TODO error or warn or nothing? +// //error("Source set '${sourceSet.name}' of project '$name' is part of several compilations $compilationNames") +// } + + for (compilation in compilations) { + val platformType = compilation.platformType + val compilationName = compilation.compilationName + val compilationType = compilationName.compilationNameToType() + + logger.info( + "compilation platformType: {}, compilationName: {}, compilationType: {}", + platformType, + compilationName, + compilationType + ) + + val platform = if (platformTypes.size > 1) { + Platform.MULTIPLATFORM + } else { + // mix of platform types -> "common" + when (platformType) { KotlinPlatformType.common -> Platform.MULTIPLATFORM KotlinPlatformType.jvm, KotlinPlatformType.androidJvm -> Platform.JVM KotlinPlatformType.js -> Platform.JS KotlinPlatformType.native, KotlinPlatformType.wasm -> Platform.NATIVE } - - if (conf.includeAnnotation) { - val configurationName = when { - // impl dependency for native (there is no transformation) - platform == Platform.NATIVE -> sourceSet.implementationConfigurationName - // compileOnly dependency for main compilation (commonMain, jvmMain, jsMain) - compilationType == CompilationType.MAIN -> sourceSet.compileOnlyConfigurationName - // impl dependency for tests - else -> sourceSet.implementationConfigurationName } - val notation = getDependencyNotation( - SuspendTransPluginConstants.ANNOTATION_GROUP, - SuspendTransPluginConstants.ANNOTATION_NAME, - platform, - conf.annotationDependencyVersion - ) - dependencies.add(configurationName, notation) - } + if (conf.includeAnnotation) { + val configurationName = when { + // impl dependency for native (there is no transformation) + platform == Platform.NATIVE -> IMPLEMENTATION // sourceSet.implementationConfigurationName + // compileOnly dependency for JVM main compilation (jvmMain, androidMain) + compilationType == CompilationType.MAIN && + platform == Platform.JVM -> COMPILE_ONLY // sourceSet.compileOnlyConfigurationName + // impl dependency for tests, and others + else -> IMPLEMENTATION // sourceSet.implementationConfigurationName + } + + val notation = getDependencyNotation( + SuspendTransPluginConstants.ANNOTATION_GROUP, + SuspendTransPluginConstants.ANNOTATION_NAME, + platform, + conf.annotationDependencyVersion + ) - if (conf.includeRuntime) { - val configurationName = sourceSet.implementationConfigurationName + sourceSet.dependencies { + when (configurationName) { + API -> { + api(notation) + } - val notation = getDependencyNotation( - SuspendTransPluginConstants.RUNTIME_GROUP, - SuspendTransPluginConstants.RUNTIME_NAME, - platform, - conf.runtimeDependencyVersion - ) - dependencies.add(configurationName, notation) - } + IMPLEMENTATION -> { + implementation(notation) + } + + COMPILE_ONLY -> { + compileOnly(notation) + } + } + } + + // dependencies.add(configurationName, notation) + } + + if (conf.includeRuntime) { + // val configurationName = sourceSet.implementationConfigurationName + val notation = getDependencyNotation( + SuspendTransPluginConstants.RUNTIME_GROUP, + SuspendTransPluginConstants.RUNTIME_NAME, + platform, + conf.runtimeDependencyVersion + ) + sourceSet.dependencies { + implementation(notation) + } + // dependencies.add(configurationName, notation) + } + } } } } @@ -255,7 +301,7 @@ fun Project.withKotlinTargets(fn: (KotlinTarget) -> Unit) { } fun Project.sourceSetsByCompilation(): Map>> { - val sourceSetsByCompilation = hashMapOf>>() + val sourceSetsByCompilation = mutableMapOf>>() withKotlinTargets { target -> target.compilations.forEach { compilation -> compilation.allKotlinSourceSets.forEach { sourceSet -> diff --git a/settings.gradle.kts b/settings.gradle.kts index b22ef89..782473b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -30,6 +30,7 @@ include(":plugins:suspend-transform-plugin-gradle") // include(":local-helper") //Samples -// include(":tests:test-jvm") -// include(":tests:test-js") -// include(":tests:test-kmp") + include(":tests:test-jvm") + include(":tests:test-js") + include(":tests:test-kmp") +// include(":tests:test-android") diff --git a/tests/test-android/build.gradle.kts b/tests/test-android/build.gradle.kts new file mode 100644 index 0000000..ca67463 --- /dev/null +++ b/tests/test-android/build.gradle.kts @@ -0,0 +1,78 @@ +import jdk.tools.jlink.resources.plugins + +plugins { + kotlin("multiplatform") + kotlin("plugin.serialization") + id("com.android.library") version "8.8.0" + id("maven-publish") + // id("com.github.ben-manes.versions") + // id("love.forte.plugin.suspend-transform") +} + +buildscript { + this@buildscript.repositories { + mavenLocal() + mavenCentral() + } + dependencies { + classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0") + } +} + +//apply(plugin = "love.forte.plugin.suspend-transform") + +repositories { + mavenLocal() + mavenCentral() + maven("https://jitpack.io") +} + +android { + compileSdk = 34 + sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") + + defaultConfig { + minSdk = 28 + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + packaging { + resources { + excludes += "META-INF/versions/9/OSGI-INF/MANIFEST.MF" + } + } +} + +kotlin { + androidTarget { + publishLibraryVariants("release") + } + sourceSets { + val androidMain by getting { + dependencies { + api(project(":waltid-libraries:crypto:waltid-crypto")) + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0") + implementation("io.github.oshai:kotlin-logging:7.0.4") + } + } + val androidInstrumentedTest by getting { + dependencies { + implementation(kotlin("test")) + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.1") + implementation("androidx.test.ext:junit:1.2.1") + implementation("androidx.test:runner:1.6.1") + implementation("androidx.test:rules:1.6.1") + } + } + } +} + +//extensions.getByType().apply { +// includeRuntime = false +// includeAnnotation = false +//// useDefault() +//} diff --git a/tests/test-js/build.gradle.kts b/tests/test-js/build.gradle.kts index f215be2..a5cbd9b 100644 --- a/tests/test-js/build.gradle.kts +++ b/tests/test-js/build.gradle.kts @@ -15,7 +15,7 @@ buildscript { mavenCentral() } dependencies { - classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0") + classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1") } } diff --git a/tests/test-jvm/build.gradle.kts b/tests/test-jvm/build.gradle.kts index 5dbc007..979f8b1 100644 --- a/tests/test-jvm/build.gradle.kts +++ b/tests/test-jvm/build.gradle.kts @@ -19,7 +19,7 @@ buildscript { mavenCentral() } dependencies { - classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0") + classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1") } } diff --git a/tests/test-kmp/build.gradle.kts b/tests/test-kmp/build.gradle.kts index 41a784d..5ddee77 100644 --- a/tests/test-kmp/build.gradle.kts +++ b/tests/test-kmp/build.gradle.kts @@ -15,7 +15,7 @@ buildscript { mavenCentral() } dependencies { - classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.10.0") + classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1") } } From 679da3d5a57d35f8ca66b5bea025f57359efdc1e Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Wed, 5 Mar 2025 22:35:53 +0800 Subject: [PATCH 2/4] Improve Gradle plugin sourceSets' dependencies config --- gradle.properties | 3 ++ .../gradle/SuspendTransformGradlePlugin.kt | 42 ++++++++++++++----- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/gradle.properties b/gradle.properties index 9f43649..c327f14 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,3 +8,6 @@ org.gradle.jvmargs=-Xmx8G -Xms2G -XX:MaxMetaspaceSize=1G -Dfile.encoding=UTF-8 #Development #development=true #kotlin.js.ir.output.granularity=per-file + +# https://kotlinlang.org/docs/multiplatform-publish-lib.html#host-requirements +#kotlin.native.enableKlibsCrossCompilation=true diff --git a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt index 17eb496..e1c0933 100644 --- a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt +++ b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt @@ -69,6 +69,15 @@ private fun SuspendTransformGradleExtension.toSubpluginOptions(): List val platformTypes = compilations.map { it.platformType }.toSet() - logger.info("platformTypes: {}", platformTypes) - -// val compilationNames = compilations.map { it.compilationName }.toSet() -// logger.info("compilationNames: {}", compilationNames) -// -// if (compilationNames.size != 1) { -// // TODO error or warn or nothing? -// //error("Source set '${sourceSet.name}' of project '$name' is part of several compilations $compilationNames") -// } + logger.info( + "Configure sourceSet [{}]. compilations: {}, platformTypes: {}", + sourceSet, + compilations, + platformTypes + ) + // TODO 可能同一个 sourceSet 会出现重复,但是需要处理吗? for (compilation in compilations) { val platformType = compilation.platformType val compilationName = compilation.compilationName @@ -271,6 +279,12 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtensi } // dependencies.add(configurationName, notation) + logger.debug( + "Add annotation dependency: {} {} for sourceSet {}", + configurationName, + notation, + sourceSet + ) } if (conf.includeRuntime) { @@ -285,7 +299,13 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtensi sourceSet.dependencies { implementation(notation) } - // dependencies.add(configurationName, notation) + + logger.debug( + "Add runtime dependency: {} {} for sourceSet {}", + IMPLEMENTATION, + notation, + sourceSet + ) } } } From 572bcdcec0e31afc496b3c0009a0313854cda7de Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Wed, 5 Mar 2025 22:55:39 +0800 Subject: [PATCH 3/4] Improve Gradle plugin sourceSets' dependencies config --- .../gradle/SuspendTransformGradlePlugin.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt index e1c0933..8f053ac 100644 --- a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt +++ b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt @@ -19,7 +19,12 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin { } override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean { - return kotlinCompilation.target.project.plugins.hasPlugin(SuspendTransformGradlePlugin::class.java) + val project = kotlinCompilation.target.project + + val isApplicable = project.plugins.hasPlugin(SuspendTransformGradlePlugin::class.java) + && project.configOrNull?.enabled != false + + return isApplicable } override fun getCompilerPluginId(): String = SuspendTransPluginConstants.KOTLIN_PLUGIN_ID @@ -341,7 +346,10 @@ private fun String.compilationNameToType(): CompilationType? = when (this) { } private val Project.config: SuspendTransformGradleExtension - get() = extensions.findByType(SuspendTransformGradleExtension::class.java) ?: SuspendTransformGradleExtension() + get() = configOrNull ?: SuspendTransformGradleExtension() + +private val Project.configOrNull: SuspendTransformGradleExtension? + get() = extensions.findByType(SuspendTransformGradleExtension::class.java) private enum class Platform(val suffix: String) { JVM("-jvm"), JS("-js"), NATIVE(""), MULTIPLATFORM("") From 30f9340393f1e624997a27943adcd85917737cd0 Mon Sep 17 00:00:00 2001 From: ForteScarlet Date: Wed, 5 Mar 2025 23:03:29 +0800 Subject: [PATCH 4/4] Improve Gradle plugin sourceSets' dependencies config --- .../suspendtrans/gradle/SuspendTransformGradlePlugin.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt index 8f053ac..e5b7ec5 100644 --- a/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt +++ b/plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt @@ -151,6 +151,14 @@ private enum class DependencyConfigurationName { } fun Project.configureMultiplatformDependency(conf: SuspendTransformGradleExtension) { + if (!conf.enabled) { + logger.info( + "The `SuspendTransformGradleExtension.enable` in project {} for multiplatform is `false`, skip config.", + this, + ) + return + } + // 时间久远,已经忘记为什么要做这个判断了,也忘记这段是在哪儿参考来的了💀 if (rootProject.getBooleanProperty("kotlin.mpp.enableGranularSourceSetsMetadata")) { val multiplatformExtensions = project.extensions.getByType(KotlinMultiplatformExtension::class.java)