Skip to content

Commit e673366

Browse files
authored
Merge pull request #86 from ForteScarlet/improve-android-target
Improved logic for Gradle plugin dependency including and advance `enable` detection
2 parents 0d26da8 + 30f9340 commit e673366

File tree

10 files changed

+219
-55
lines changed

10 files changed

+219
-55
lines changed

buildSrc/src/main/kotlin/IProject.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object IProject : ProjectDetail() {
1111

1212
// Remember the libs.versions.toml!
1313
val ktVersion = "2.1.0"
14-
val pluginVersion = "0.11.0"
14+
val pluginVersion = "0.11.1"
1515

1616
override val version: String = "$ktVersion-$pluginVersion"
1717

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/CliOptions.kt

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import kotlinx.serialization.builtins.ListSerializer
44
import kotlinx.serialization.builtins.MapSerializer
55
import kotlinx.serialization.json.Json
66
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
7-
import kotlin.reflect.KMutableProperty
87

98
private val defaultJson = Json {
109
isLenient = true
@@ -84,15 +83,15 @@ private class ResolveBuilder {
8483
outc = block
8584
}
8685

87-
fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String>) {
88-
inc { block().setter.call(it) }
89-
out { block().getter.call() }
90-
}
91-
92-
fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String?>) {
93-
inc { block().setter.call(it.takeIf { it.isNotEmpty() }) }
94-
out { block().getter.call() ?: "" }
95-
}
86+
// fun withProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String>) {
87+
// inc { block().setter.call(it) }
88+
// out { block().getter.call() }
89+
// }
90+
//
91+
// fun withNullableProp(block: SuspendTransformConfiguration.() -> KMutableProperty<String?>) {
92+
// inc { block().setter.call(it.takeIf { it.isNotEmpty() }) }
93+
// out { block().getter.call() ?: "" }
94+
// }
9695
}
9796

9897
private fun option(

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/TransformUtil.kt

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ import org.jetbrains.kotlin.name.Name
1111
fun ClassInfo.toClassId(): ClassId =
1212
ClassId(packageName.fqn, className.fqn, local)
1313

14+
@Suppress("DEPRECATION")
1415
fun FunctionInfo.toCallableId(): CallableId =
1516
CallableId(packageName.fqn, className?.fqn, Name.identifier(functionName))

gradle.properties

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ org.gradle.jvmargs=-Xmx8G -Xms2G -XX:MaxMetaspaceSize=1G -Dfile.encoding=UTF-8
88
#Development
99
#development=true
1010
#kotlin.js.ir.output.granularity=per-file
11+
12+
# https://kotlinlang.org/docs/multiplatform-publish-lib.html#host-requirements
13+
#kotlin.native.enableKlibsCrossCompilation=true

plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt

+120-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package love.forte.plugin.suspendtrans.gradle
22

33
import love.forte.plugin.suspendtrans.CliOptions
4+
import love.forte.plugin.suspendtrans.gradle.DependencyConfigurationName.*
45
import org.gradle.api.Project
56
import org.gradle.api.provider.Provider
67
import 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

6975
private 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+
134153
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+
// 时间久远,已经忘记为什么要做这个判断了,也忘记这段是在哪儿参考来的了💀
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

257336
fun 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

277356
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)
279361

280362
private enum class Platform(val suffix: String) {
281363
JVM("-jvm"), JS("-js"), NATIVE(""), MULTIPLATFORM("")

settings.gradle.kts

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include(":plugins:suspend-transform-plugin-gradle")
3030
// include(":local-helper")
3131

3232
//Samples
33-
// include(":tests:test-jvm")
34-
// include(":tests:test-js")
35-
// include(":tests:test-kmp")
33+
include(":tests:test-jvm")
34+
include(":tests:test-js")
35+
include(":tests:test-kmp")
36+
// include(":tests:test-android")

tests/test-android/build.gradle.kts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import jdk.tools.jlink.resources.plugins
2+
3+
plugins {
4+
kotlin("multiplatform")
5+
kotlin("plugin.serialization")
6+
id("com.android.library") version "8.8.0"
7+
id("maven-publish")
8+
// id("com.github.ben-manes.versions")
9+
// id("love.forte.plugin.suspend-transform")
10+
}
11+
12+
buildscript {
13+
this@buildscript.repositories {
14+
mavenLocal()
15+
mavenCentral()
16+
}
17+
dependencies {
18+
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0")
19+
}
20+
}
21+
22+
//apply(plugin = "love.forte.plugin.suspend-transform")
23+
24+
repositories {
25+
mavenLocal()
26+
mavenCentral()
27+
maven("https://jitpack.io")
28+
}
29+
30+
android {
31+
compileSdk = 34
32+
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
33+
34+
defaultConfig {
35+
minSdk = 28
36+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
37+
}
38+
39+
compileOptions {
40+
sourceCompatibility = JavaVersion.VERSION_1_8
41+
targetCompatibility = JavaVersion.VERSION_1_8
42+
}
43+
packaging {
44+
resources {
45+
excludes += "META-INF/versions/9/OSGI-INF/MANIFEST.MF"
46+
}
47+
}
48+
}
49+
50+
kotlin {
51+
androidTarget {
52+
publishLibraryVariants("release")
53+
}
54+
sourceSets {
55+
val androidMain by getting {
56+
dependencies {
57+
api(project(":waltid-libraries:crypto:waltid-crypto"))
58+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
59+
implementation("io.github.oshai:kotlin-logging:7.0.4")
60+
}
61+
}
62+
val androidInstrumentedTest by getting {
63+
dependencies {
64+
implementation(kotlin("test"))
65+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.1")
66+
implementation("androidx.test.ext:junit:1.2.1")
67+
implementation("androidx.test:runner:1.6.1")
68+
implementation("androidx.test:rules:1.6.1")
69+
}
70+
}
71+
}
72+
}
73+
74+
//extensions.getByType<SuspendTransformGradleExtension>().apply {
75+
// includeRuntime = false
76+
// includeAnnotation = false
77+
//// useDefault()
78+
//}

tests/test-js/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ buildscript {
1515
mavenCentral()
1616
}
1717
dependencies {
18-
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0")
18+
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1")
1919
}
2020
}
2121

tests/test-jvm/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ buildscript {
1919
mavenCentral()
2020
}
2121
dependencies {
22-
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.0")
22+
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1")
2323
}
2424
}
2525

tests/test-kmp/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ buildscript {
1515
mavenCentral()
1616
}
1717
dependencies {
18-
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.10.0")
18+
classpath("love.forte.plugin.suspend-transform:suspend-transform-plugin-gradle:2.1.0-0.11.1")
1919
}
2020
}
2121

0 commit comments

Comments
 (0)