Skip to content

Improved logic for Gradle plugin dependency including and advance enable detection #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/IProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,15 +83,15 @@ private class ResolveBuilder {
outc = block
}

fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String>) {
inc { block().setter.call(it) }
out { block().getter.call() }
}

fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String?>) {
inc { block().setter.call(it.takeIf { it.isNotEmpty() }) }
out { block().getter.call() ?: "" }
}
// fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String>) {
// inc { block().setter.call(it) }
// out { block().getter.call() }
// }
//
// fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String?>) {
// inc { block().setter.call(it.takeIf { it.isNotEmpty() }) }
// out { block().getter.call() ?: "" }
// }
}

private fun option(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -68,6 +74,15 @@ private fun SuspendTransformGradleExtension.toSubpluginOptions(): List<Subplugin

private fun Project.configureDependencies() {
fun Project.include(platform: Platform, conf: SuspendTransformGradleExtension) {
if (!conf.enabled) {
logger.info(
"The `SuspendTransformGradleExtension.enable` in project {} for platform {} is `false`, skip config.",
this,
platform
)
return
}

if (conf.includeAnnotation) {
val notation = getDependencyNotation(
SuspendTransPluginConstants.ANNOTATION_GROUP,
Expand All @@ -78,7 +93,7 @@ private fun Project.configureDependencies() {
if (platform == Platform.JVM) {
dependencies.add(conf.annotationConfigurationName, notation)
} else {
// JS, native 似乎不支持其他的 name,例如 compileOnly
// JS, native 似乎不支持 compileOnly
dependencies.add("implementation", notation)
}
dependencies.add("testImplementation", notation)
Expand Down Expand Up @@ -131,7 +146,20 @@ fun Project.withPluginWhenEvaluatedConf(
}
}

private enum class DependencyConfigurationName {
API, IMPLEMENTATION, COMPILE_ONLY
}

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)

Expand Down Expand Up @@ -197,51 +225,102 @@ 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(
"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
)
}
}

}
}
}
Expand All @@ -255,7 +334,7 @@ fun Project.withKotlinTargets(fn: (KotlinTarget) -> Unit) {
}

fun Project.sourceSetsByCompilation(): Map<KotlinSourceSet, List<KotlinCompilation<*>>> {
val sourceSetsByCompilation = hashMapOf<KotlinSourceSet, MutableList<KotlinCompilation<*>>>()
val sourceSetsByCompilation = mutableMapOf<KotlinSourceSet, MutableList<KotlinCompilation<*>>>()
withKotlinTargets { target ->
target.compilations.forEach { compilation ->
compilation.allKotlinSourceSets.forEach { sourceSet ->
Expand All @@ -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("")
Expand Down
7 changes: 4 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
78 changes: 78 additions & 0 deletions tests/test-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<SuspendTransformGradleExtension>().apply {
// includeRuntime = false
// includeAnnotation = false
//// useDefault()
//}
2 changes: 1 addition & 1 deletion tests/test-js/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test-jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test-kmp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down