Skip to content

Commit 5b3175b

Browse files
authored
Merge pull request #93 from ForteScarlet/improve-cli-options
Refactor and update Gradle plugin APIs and configurations
2 parents 5763a14 + 3a61fac commit 5b3175b

File tree

52 files changed

+3675
-2183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3675
-2183
lines changed

README.md

Lines changed: 668 additions & 340 deletions
Large diffs are not rendered by default.

README_CN.md

Lines changed: 503 additions & 496 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* 为了让 gradle 插件可以在 buildSrc 之类的地方使用。
3+
*/
4+
fun org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension.configGradleBuildSrcFriendly() {
5+
coreLibrariesVersion = "1.9.0"
6+
compilerOptions {
7+
apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9)
8+
languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9)
9+
}
10+
}

buildSrc/src/main/kotlin/IProject.kt

Lines changed: 1 addition & 1 deletion
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.20"
14-
val pluginVersion = "0.11.1"
14+
val pluginVersion = "0.12.0"
1515

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CliOption Module
2+
3+
TODO:将 CliOption 相关的内容独立出来,
4+
以避免在 Gradle Plugin 中传递引用 Kotlin compiler。
5+
6+
see also:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
3+
plugins {
4+
kotlin("jvm")
5+
kotlin("plugin.serialization")
6+
// id("com.github.gmazzo.buildconfig")
7+
id("suspend-transform.jvm-maven-publish")
8+
}
9+
10+
dependencies {
11+
compileOnly(kotlin("stdlib"))
12+
compileOnly(kotlin("compiler"))
13+
api(project(":compiler:suspend-transform-plugin-configuration"))
14+
api(libs.kotlinx.serialization.core)
15+
api(libs.kotlinx.serialization.protobuf)
16+
17+
testImplementation(libs.kotlinx.serialization.json)
18+
testImplementation(kotlin("test"))
19+
testImplementation(kotlin("compiler"))
20+
// testImplementation(libs.kotlinx.coroutines.core)
21+
}
22+
23+
kotlin {
24+
configGradleBuildSrcFriendly()
25+
compilerOptions {
26+
jvmTarget.set(JvmTarget.JVM_1_8)
27+
freeCompilerArgs.addAll("-Xjvm-default=all")
28+
}
29+
}
30+
31+
tasks.test {
32+
useJUnitPlatform()
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package love.forte.plugin.suspendtrans.cli
2+
3+
import kotlinx.serialization.ExperimentalSerializationApi
4+
import kotlinx.serialization.decodeFromHexString
5+
import kotlinx.serialization.encodeToHexString
6+
import kotlinx.serialization.protobuf.ProtoBuf
7+
import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfiguration
8+
import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfiguration.Companion.serializer
9+
10+
// Cli Options for gradle plugin
11+
12+
@OptIn(ExperimentalSerializationApi::class)
13+
private val SERIALIZER = ProtoBuf {
14+
encodeDefaults = true
15+
}
16+
17+
@OptIn(ExperimentalSerializationApi::class)
18+
fun SuspendTransformConfiguration.encodeToHex(): String {
19+
return SERIALIZER.encodeToHexString(serializer(), this)
20+
}
21+
22+
@OptIn(ExperimentalSerializationApi::class)
23+
fun decodeSuspendTransformConfigurationFromHex(hex: String): SuspendTransformConfiguration {
24+
return SERIALIZER.decodeFromHexString(serializer(), hex)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package love.forte.plugin.suspendtrans.cli
2+
3+
// Cli Options for Kotlin compiler plugin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package love.forte.plugin.suspendtrans.cli
2+
3+
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
4+
5+
/**
6+
*
7+
*
8+
* @author ForteScarlet
9+
*/
10+
interface SuspendTransformCliOption {
11+
val optionName: String
12+
val valueDescription: String
13+
val description: String
14+
val required: Boolean
15+
val allowMultipleOccurrences: Boolean
16+
}
17+
18+
private data class SimpleSuspendTransformCliOption(
19+
override val allowMultipleOccurrences: Boolean,
20+
override val description: String,
21+
override val optionName: String,
22+
override val required: Boolean,
23+
override val valueDescription: String
24+
) : SuspendTransformCliOption
25+
26+
private data class AbstractCliOptionImpl(
27+
override val allowMultipleOccurrences: Boolean,
28+
override val description: String,
29+
override val optionName: String,
30+
override val required: Boolean,
31+
override val valueDescription: String
32+
) : AbstractCliOption, SuspendTransformCliOption
33+
34+
/**
35+
* Creates an instance of [SuspendTransformCliOption] to describe and define a CLI option.
36+
*
37+
* @param optionName The name of the option used to identify it in the CLI.
38+
* @param valueDescription A description of the option's value, defaults to the option name.
39+
* @param description A textual description of the option, defaults to an empty string.
40+
* @param required Whether this option is mandatory, defaults to not required (`false`).
41+
* @param allowMultipleOccurrences Whether this option can appear multiple times in the CLI,
42+
* defaults to not allowed (`false`).
43+
* @return Returns an instance of [SuspendTransformCliOption] implemented by [SimpleSuspendTransformCliOption].
44+
*/
45+
fun SuspendTransformCliOption(
46+
optionName: String,
47+
valueDescription: String = optionName,
48+
description: String = "",
49+
required: Boolean = false,
50+
allowMultipleOccurrences: Boolean = false
51+
): SuspendTransformCliOption {
52+
// Create and return an instance of the concrete implementation class
53+
return SimpleSuspendTransformCliOption(
54+
allowMultipleOccurrences = allowMultipleOccurrences,
55+
description = description,
56+
optionName = optionName,
57+
required = required,
58+
valueDescription = valueDescription
59+
)
60+
}
61+
62+
/**
63+
* Converts the current [SuspendTransformCliOption] instance to an [AbstractCliOption].
64+
* If the current object is already an [AbstractCliOption], it is returned directly;
65+
* otherwise, a new instance is created and returned.
66+
*
67+
* @return The converted [AbstractCliOption] instance
68+
*/
69+
fun SuspendTransformCliOption.toAbstractCliOption(): AbstractCliOption {
70+
return this as? AbstractCliOption ?: AbstractCliOptionImpl(
71+
allowMultipleOccurrences = allowMultipleOccurrences,
72+
description = description,
73+
optionName = optionName,
74+
required = required,
75+
valueDescription = valueDescription
76+
)
77+
}
78+
79+
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package love.forte.plugin.suspendtrans.cli
2+
3+
object SuspendTransformCliOptions {
4+
const val CONFIGURATION = "configuration"
5+
6+
val CLI_CONFIGURATION = SuspendTransformCliOption(
7+
optionName = "configuration",
8+
valueDescription = "Configuration hex string",
9+
description = "Configuration serialized protobuf hex string value",
10+
required = true,
11+
allowMultipleOccurrences = false,
12+
)
13+
}

0 commit comments

Comments
 (0)