Skip to content

Commit 64b7a4c

Browse files
committed
移除不再使用的代码生成相关类(如 Showcase.ktSimbotCodegen.kt 等),重构并扩展生成逻辑,新增 Spring 组件选择和 Kotlin/Java 特定依赖支持,优化 UI 和表单布局及交互,以提升生成灵活性和用户体验。
1 parent 9de1726 commit 64b7a4c

21 files changed

+590
-240
lines changed

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/App.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package love.forte.simbot.codegen
22

3+
import androidx.compose.animation.ExperimentalSharedTransitionApi
4+
import androidx.compose.animation.SharedTransitionLayout
35
import androidx.compose.foundation.isSystemInDarkTheme
46
import androidx.compose.foundation.layout.Box
57
import androidx.compose.foundation.layout.fillMaxSize
@@ -20,6 +22,7 @@ import simbot_codegen.composeapp.generated.resources.LXGWNeoXiHeiScreen
2022
import simbot_codegen.composeapp.generated.resources.Res
2123

2224

25+
@OptIn(ExperimentalSharedTransitionApi::class)
2326
@Composable
2427
fun App() {
2528
@OptIn(ExperimentalResourceApi::class)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package love.forte.simbot.codegen
2+
3+
import love.forte.codegentle.common.naming.isEmpty
4+
import love.forte.codegentle.common.naming.toRelativePath
5+
import love.forte.codegentle.kotlin.KotlinFile
6+
7+
fun KotlinFile.toRelativePath(
8+
filename: String = type.name,
9+
isScript: Boolean = false,
10+
separator: String = "/"
11+
): String {
12+
val filenameWithExtension = if (filename.contains('.')) {
13+
filename
14+
} else {
15+
filename + if (isScript) ".kts" else ".kt"
16+
}
17+
18+
val packageName = this.packageName
19+
return if (packageName.isEmpty()) {
20+
filenameWithExtension
21+
} else {
22+
packageName.toRelativePath(separator) + separator + filenameWithExtension
23+
}
24+
}

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/codegen/SimbotCodegen.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/gen/SimbotComponent.kt renamed to composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/codegen/SimbotComponent.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
package love.forte.simbot.codegen.gen
2-
1+
package love.forte.simbot.codegen.codegen
32

43
/**
54
*
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package love.forte.simbot.codegen.codegen
2+
3+
4+
/**
5+
*
6+
* @author ForteScarlet
7+
*/
8+
enum class SpringComponent(
9+
val display: String,
10+
val group: String,
11+
val artifactId: String,
12+
/**
13+
* 用在 libs.versions.toml 里的名字,例如 `jackson-module-kotlin`
14+
*/
15+
val depName: String,
16+
/**
17+
* 用在build.gradle.kts 等引用 libs.versions.toml 依赖的路径,例如 `jackson.module.kotlin`
18+
*/
19+
val libPath: String = depName.replace('-', '.'),
20+
val selectable: Boolean = true,
21+
val forJava: Boolean = true,
22+
val forKotlin: Boolean = true,
23+
) {
24+
// for Kotlin
25+
JACKSON_MODULE_KOTLIN(
26+
display = "jackson-module-kotlin",
27+
group = "com.fasterxml.jackson.module",
28+
artifactId = "jackson-module-kotlin",
29+
depName = "jackson-module-kotlin",
30+
selectable = false
31+
),
32+
KOTLIN_REFLECT(
33+
display = "kotlin-reflect",
34+
group = "org.jetbrains.kotlin",
35+
artifactId = "kotlin-reflect",
36+
depName = "kotlin-reflect",
37+
selectable = false
38+
),
39+
40+
// selectable components
41+
42+
WEB(
43+
display = "web",
44+
group = "org.springframework.boot",
45+
artifactId = "spring-boot-starter-web",
46+
depName = "spring-web",
47+
),
48+
49+
// TODO reactive dependencies - will be added later
50+
// WEB_FLUX(
51+
// "webflux",
52+
// "org.springframework.boot",
53+
// "spring-boot-starter-webflux"
54+
// ),
55+
// DATA_R2DBC(
56+
// "data-r2dbc",
57+
// "org.springframework.boot",
58+
// "spring-boot-starter-data-r2dbc"
59+
// ),
60+
61+
// Data access
62+
DATA_JDBC(
63+
display = "data-jdbc",
64+
group = "org.springframework.boot",
65+
artifactId = "spring-boot-starter-data-jdbc",
66+
depName = "spring-data-jdbc"
67+
),
68+
DATA_JPA(
69+
display = "data-jpa",
70+
group = "org.springframework.boot",
71+
artifactId = "spring-boot-starter-data-jpa",
72+
depName = "spring-data-jpa"
73+
),
74+
DATA_JOOQ(
75+
display = "data-jooq",
76+
group = "org.springframework.boot",
77+
artifactId = "spring-boot-starter-jooq",
78+
depName = "spring-data-jooq"
79+
),
80+
81+
// Database drivers
82+
MYSQL(
83+
display = "mysql",
84+
group = "com.mysql",
85+
artifactId = "mysql-connector-j",
86+
depName = "mysql-connector"
87+
),
88+
H2(
89+
display = "h2",
90+
group = "com.h2database",
91+
artifactId = "h2",
92+
depName = "h2-database"
93+
),
94+
POSTGRESQL(
95+
display = "postgresql",
96+
group = "org.postgresql",
97+
artifactId = "postgresql",
98+
depName = "postgresql-driver"
99+
),
100+
SQL_SERVER(
101+
display = "sqlserver",
102+
group = "com.microsoft.sqlserver",
103+
artifactId = "mssql-jdbc",
104+
depName = "mssql-jdbc"
105+
),
106+
ORACLE(
107+
display = "oracle",
108+
group = "com.oracle.database.jdbc",
109+
artifactId = "ojdbc8",
110+
depName = "oracle-jdbc"
111+
),
112+
113+
// NoSQL and caching
114+
DATA_REDIS(
115+
display = "data-redis",
116+
group = "org.springframework.boot",
117+
artifactId = "spring-boot-starter-data-redis",
118+
depName = "spring-data-redis"
119+
),
120+
DATA_MONGODB(
121+
display = "data-mongodb",
122+
group = "org.springframework.boot",
123+
artifactId = "spring-boot-starter-data-mongodb",
124+
depName = "spring-data-mongodb"
125+
),
126+
DATA_ELASTICSEARCH(
127+
display = "data-elasticsearch",
128+
group = "org.springframework.boot",
129+
artifactId = "spring-boot-starter-data-elasticsearch",
130+
depName = "spring-data-elasticsearch"
131+
),
132+
133+
// Communication
134+
WEBSOCKET(
135+
display = "websocket",
136+
group = "org.springframework.boot",
137+
artifactId = "spring-boot-starter-websocket",
138+
depName = "spring-websocket"
139+
),
140+
AMQP(
141+
display = "amqp",
142+
group = "org.springframework.boot",
143+
artifactId = "spring-boot-starter-amqp",
144+
depName = "spring-amqp"
145+
)
146+
}

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/codegen/naming/SpringNames.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ object SpringNames {
3636
private val subPackageAutoconfigureCondition = "autoconfigure.condition".parseToPackageName()
3737
private val springBootAutoconfigureCondition = springBootPkg + subPackageAutoconfigureCondition
3838

39-
val enableAutoConfigurationAno = ClassName(springBootAutoconfigureCondition, "EnableAutoConfiguration")
40-
val springBootApplicationAno = ClassName(springBootAutoconfigureCondition, "SpringBootApplication")
39+
val springBootApplicationAno = ClassName(springBootPkg + "autoconfigure", "SpringBootApplication")
40+
val enableAutoConfigurationAno = ClassName(springBootPkg + "autoconfigure", "EnableAutoConfiguration")
41+
4142
val conditionalOnMissingBeanAno = ClassName(springBootAutoconfigureCondition, "ConditionalOnMissingBean")
4243
val conditionalOnBeanAno = ClassName(springBootAutoconfigureCondition, "ConditionalOnBean")
4344
val conditionalOnPropertyAno = ClassName(springBootAutoconfigureCondition, "ConditionalOnProperty")
4445
val conditionalOnClassAno = ClassName(springBootAutoconfigureCondition, "ConditionalOnClass")
45-
}
46+
}

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/gen/ComponentDependencies.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ val KOTLIN_REFLECT = GradleCatalogVersionDependency(
121121
version = KOTLIN_VERSION
122122
)
123123

124+
val JACKSON_MODULE_KOTLIN = GradleCatalogVersionDependency(
125+
dependencyName = "jackson-module-kotlin",
126+
group = "com.fasterxml.jackson.module",
127+
name = "jackson-module-kotlin",
128+
version = GradleCatalogVersion(
129+
name = "jackson",
130+
version = "2.15.2"
131+
)
132+
)
133+
124134
// plugins
125135

126136
val PLUGIN_KOTLIN = GradleCatalogPlugin(

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/gen/GradleProject.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import jszip.JSZip
1010
import love.forte.codegentle.common.code.*
1111
import love.forte.codegentle.kotlin.KotlinFile
1212
import love.forte.codegentle.kotlin.writeToKotlinString
13-
import love.forte.simbot.codegen.gen.SimbotComponent.*
13+
import love.forte.simbot.codegen.codegen.SimbotComponent
14+
import love.forte.simbot.codegen.codegen.SimbotComponent.*
15+
import love.forte.simbot.codegen.codegen.SpringComponent
1416
import love.forte.simbot.codegen.gen.core.JavaStyle
1517
import love.forte.simbot.codegen.gen.core.ProgrammingLanguage
1618
import org.jetbrains.compose.resources.ExperimentalResourceApi
@@ -67,6 +69,43 @@ class GradleProjectViewModel : ViewModel() {
6769

6870
val components: MutableList<SimbotComponentWithVersion> = mutableStateListOf()
6971

72+
// /**
73+
// * SpringBoot 组件选择
74+
// */
75+
// val selectedSpringComponents: MutableList<SpringComponent> = mutableStateListOf()
76+
77+
/**
78+
* SpringBoot 组件选择
79+
*/
80+
private var selectedSpringComponents: Int by mutableStateOf(0)
81+
82+
val selectedSpringComponentMask: SpringComponentMask
83+
get() = SpringComponentMask(selectedSpringComponents)
84+
85+
value class SpringComponentMask(val mask: Int) {
86+
operator fun contains(component: SpringComponent): Boolean {
87+
return (mask and (1 shl component.ordinal)) != 0
88+
}
89+
90+
fun count(): Int {
91+
return mask.countOneBits()
92+
}
93+
94+
fun components(): List<SpringComponent> {
95+
return SpringComponent.entries.filter { component ->
96+
component in this
97+
}
98+
}
99+
}
100+
101+
fun addSelectedSpringComponent(component: SpringComponent) {
102+
selectedSpringComponents = selectedSpringComponents or (1 shl component.ordinal)
103+
}
104+
105+
fun removeSelectedSpringComponent(component: SpringComponent) {
106+
selectedSpringComponents = selectedSpringComponents and (1 shl component.ordinal).inv()
107+
}
108+
70109
/**
71110
* 额外依赖
72111
*/
@@ -361,7 +400,6 @@ fun doGenerateSpring(
361400

362401
// 示例的配置文件、代码
363402
emitSpringShowcases(
364-
projectName = name,
365403
projectPackage = pkg,
366404
components = components.map { it.component },
367405
sourceSets = sourceDir,

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/gen/Showcases.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import love.forte.codegentle.common.code.*
55
import love.forte.codegentle.common.naming.ArrayTypeName
66
import love.forte.codegentle.common.naming.ClassName
77
import love.forte.codegentle.common.naming.MemberName
8+
import love.forte.codegentle.common.naming.parseToPackageName
89
import love.forte.codegentle.common.ref.addAnnotation
910
import love.forte.codegentle.common.ref.ref
1011
import love.forte.codegentle.kotlin.*
@@ -13,16 +14,16 @@ import love.forte.codegentle.kotlin.spec.KotlinFunctionSpec
1314
import love.forte.codegentle.kotlin.spec.addFunction
1415
import love.forte.codegentle.kotlin.spec.addMainFunction
1516
import love.forte.codegentle.kotlin.spec.addParameter
17+
import love.forte.simbot.codegen.codegen.SimbotComponent
1618
import love.forte.simbot.codegen.codegen.naming.SimbotNames
1719
import love.forte.simbot.codegen.codegen.naming.SpringNames
18-
import love.forte.simbot.codegen.gen.SimbotComponent.*
20+
import love.forte.simbot.codegen.codegen.SimbotComponent.*
1921

2022

2123
/**
2224
* 生成使用Spring时的示例们到 sourceSets 中。
2325
*/
2426
fun emitSpringShowcases(
25-
projectName: String,
2627
projectPackage: String,
2728
components: Collection<SimbotComponent>,
2829
sourceSets: JSZip,
@@ -49,7 +50,7 @@ fun genKotlinSpringMainFile(
4950
name: String,
5051
projectPackage: String
5152
): KotlinFile {
52-
val mainFile = KotlinFile(projectPackage) {
53+
val mainFile = KotlinFile(projectPackage.parseToPackageName()) {
5354
addSimpleClassType(name) {
5455
addAnnotation(SimbotNames.enableSimbotAno)
5556
addAnnotation(SpringNames.springBootApplicationAno)
@@ -74,10 +75,10 @@ fun genKotlinSpringMainFile(
7475

7576
fun emitSpringMainFile(
7677
projectPackage: String,
77-
sourceSets: JSZip
78+
sourceDir: JSZip
7879
) {
7980
val file = genKotlinSpringMainFile("MainApplication", projectPackage)
80-
sourceSets.file(file.toRelativePath(), file.writeToKotlinString())
81+
sourceDir.file(file.toRelativePath(), file.writeToKotlinString())
8182
}
8283

8384
fun genKotlinSpringListenerShowcases(
@@ -87,7 +88,7 @@ fun genKotlinSpringListenerShowcases(
8788
var showcaseCount = 1
8889
val handlePackage = "$projectPackage.handle"
8990

90-
val myHandleFile = KotlinFile(handlePackage) {
91+
val myHandleFile = KotlinFile(handlePackage.parseToPackageName()) {
9192
// Text + Message
9293
addStaticImport("love.forte.simbot.message.plus")
9394

@@ -241,10 +242,10 @@ fun componentShowcase(
241242
fun emitSpringListenerShowcases(
242243
projectPackage: String,
243244
components: Collection<SimbotComponent>,
244-
sourceSets: JSZip
245+
sourceDir: JSZip
245246
) {
246247
val file = genKotlinSpringListenerShowcases(projectPackage, components)
247-
sourceSets.file(file.toRelativePath(), file.writeToKotlinString())
248+
sourceDir.file(file.toRelativePath(), file.writeToKotlinString())
248249
}
249250

250251
/**

0 commit comments

Comments
 (0)