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/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 c86f9b9..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 @@ -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 @@ -18,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 @@ -68,6 +74,15 @@ private fun SuspendTransformGradleExtension.toSubpluginOptions(): List 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( + "Configure sourceSet [{}]. compilations: {}, platformTypes: {}", + sourceSet, + compilations, + platformTypes + ) + + // TODO 可能同一个 sourceSet 会出现重复,但是需要处理吗? + 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 -> 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 + ) + + sourceSet.dependencies { + when (configurationName) { + API -> { + api(notation) + } + + IMPLEMENTATION -> { + implementation(notation) + } + + COMPILE_ONLY -> { + compileOnly(notation) + } + } + } - 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 + // dependencies.add(configurationName, notation) + logger.debug( + "Add annotation dependency: {} {} for sourceSet {}", + configurationName, + notation, + sourceSet + ) } - val notation = getDependencyNotation( - SuspendTransPluginConstants.ANNOTATION_GROUP, - SuspendTransPluginConstants.ANNOTATION_NAME, - platform, - conf.annotationDependencyVersion - ) - dependencies.add(configurationName, notation) - } + if (conf.includeRuntime) { + // val configurationName = sourceSet.implementationConfigurationName - if (conf.includeRuntime) { - val configurationName = sourceSet.implementationConfigurationName + val notation = getDependencyNotation( + SuspendTransPluginConstants.RUNTIME_GROUP, + SuspendTransPluginConstants.RUNTIME_NAME, + platform, + conf.runtimeDependencyVersion + ) + sourceSet.dependencies { + implementation(notation) + } - val notation = getDependencyNotation( - SuspendTransPluginConstants.RUNTIME_GROUP, - SuspendTransPluginConstants.RUNTIME_NAME, - platform, - conf.runtimeDependencyVersion - ) - dependencies.add(configurationName, notation) + logger.debug( + "Add runtime dependency: {} {} for sourceSet {}", + IMPLEMENTATION, + notation, + sourceSet + ) + } } - } } } @@ -255,7 +334,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 -> @@ -275,7 +354,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("") 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") } }