Skip to content

Commit 6ce4d40

Browse files
committed
优化代码生成逻辑:统一转换方法为 toRelativePath0,新增 JavaFile.toRelativePath0 支持;改进注释和参数描述,更清晰表达生成方法逻辑;完善 JS 文件保存功能引入 @OptIn 注解。
1 parent 578034c commit 6ce4d40

25 files changed

+364
-337
lines changed
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2-
android:width="600dp"
3-
android:height="600dp"
4-
android:viewportWidth="600"
5-
android:viewportHeight="600">
6-
<path
7-
android:pathData="M301.21,418.53C300.97,418.54 300.73,418.56 300.49,418.56C297.09,418.59 293.74,417.72 290.79,416.05L222.6,377.54C220.63,376.43 219,374.82 217.85,372.88C216.7,370.94 216.09,368.73 216.07,366.47L216.07,288.16C216.06,287.32 216.09,286.49 216.17,285.67C216.38,283.54 216.91,281.5 217.71,279.6L199.29,268.27L177.74,256.19C175.72,260.43 174.73,265.23 174.78,270.22L174.79,387.05C174.85,393.89 178.57,400.2 184.53,403.56L286.26,461.02C290.67,463.51 295.66,464.8 300.73,464.76C300.91,464.76 301.09,464.74 301.27,464.74C301.24,449.84 301.22,439.23 301.22,439.23L301.21,418.53Z"
8-
android:fillColor="#041619"
9-
android:fillType="nonZero"/>
10-
<path
11-
android:pathData="M409.45,242.91L312.64,188.23C303.64,183.15 292.58,183.26 283.68,188.51L187.92,245C183.31,247.73 179.93,251.62 177.75,256.17L177.74,256.19L199.29,268.27L217.71,279.6C217.83,279.32 217.92,279.02 218.05,278.74C218.24,278.36 218.43,277.98 218.64,277.62C219.06,276.88 219.52,276.18 220.04,275.51C221.37,273.8 223.01,272.35 224.87,271.25L289.06,233.39C290.42,232.59 291.87,231.96 293.39,231.51C295.53,230.87 297.77,230.6 300,230.72C302.98,230.88 305.88,231.73 308.47,233.2L373.37,269.85C375.54,271.08 377.49,272.68 379.13,274.57C379.68,275.19 380.18,275.85 380.65,276.53C380.86,276.84 381.05,277.15 381.24,277.47L397.79,266.39L420.34,252.93L420.31,252.88C417.55,248.8 413.77,245.35 409.45,242.91Z"
12-
android:fillColor="#37BF6E"
13-
android:fillType="nonZero"/>
14-
<path
15-
android:pathData="M381.24,277.47C381.51,277.92 381.77,278.38 382.01,278.84C382.21,279.24 382.39,279.65 382.57,280.06C382.91,280.88 383.19,281.73 383.41,282.59C383.74,283.88 383.92,285.21 383.93,286.57L383.93,361.1C383.96,363.95 383.35,366.77 382.16,369.36C381.93,369.86 381.69,370.35 381.42,370.83C379.75,373.79 377.32,376.27 374.39,378L310.2,415.87C307.47,417.48 304.38,418.39 301.21,418.53L301.22,439.23C301.22,439.23 301.24,449.84 301.27,464.74C306.1,464.61 310.91,463.3 315.21,460.75L410.98,404.25C419.88,399 425.31,389.37 425.22,379.03L425.22,267.85C425.17,262.48 423.34,257.34 420.34,252.93L397.79,266.39L381.24,277.47Z"
16-
android:fillColor="#3870B2"
17-
android:fillType="nonZero"/>
18-
<path
19-
android:pathData="M177.75,256.17C179.93,251.62 183.31,247.73 187.92,245L283.68,188.51C292.58,183.26 303.64,183.15 312.64,188.23L409.45,242.91C413.77,245.35 417.55,248.8 420.31,252.88L420.34,252.93L498.59,206.19C494.03,199.46 487.79,193.78 480.67,189.75L320.86,99.49C306.01,91.1 287.75,91.27 273.07,99.95L114.99,193.2C107.39,197.69 101.81,204.11 98.21,211.63L177.74,256.19L177.75,256.17ZM301.27,464.74C301.09,464.74 300.91,464.76 300.73,464.76C295.66,464.8 290.67,463.51 286.26,461.02L184.53,403.56C178.57,400.2 174.85,393.89 174.79,387.05L174.78,270.22C174.73,265.23 175.72,260.43 177.74,256.19L98.21,211.63C94.86,218.63 93.23,226.58 93.31,234.82L93.31,427.67C93.42,438.97 99.54,449.37 109.4,454.92L277.31,549.77C284.6,553.88 292.84,556.01 301.2,555.94L301.2,555.8C301.39,543.78 301.33,495.26 301.27,464.74Z"
20-
android:strokeWidth="10"
21-
android:fillColor="#00000000"
22-
android:strokeColor="#083042"
23-
android:fillType="nonZero"/>
24-
<path
25-
android:pathData="M498.59,206.19L420.34,252.93C423.34,257.34 425.17,262.48 425.22,267.85L425.22,379.03C425.31,389.37 419.88,399 410.98,404.25L315.21,460.75C310.91,463.3 306.1,464.61 301.27,464.74C301.33,495.26 301.39,543.78 301.2,555.8L301.2,555.94C309.48,555.87 317.74,553.68 325.11,549.32L483.18,456.06C497.87,447.39 506.85,431.49 506.69,414.43L506.69,230.91C506.6,222.02 503.57,213.5 498.59,206.19Z"
26-
android:strokeWidth="10"
27-
android:fillColor="#00000000"
28-
android:strokeColor="#083042"
29-
android:fillType="nonZero"/>
30-
<path
31-
android:pathData="M301.2,555.94C292.84,556.01 284.6,553.88 277.31,549.76L109.4,454.92C99.54,449.37 93.42,438.97 93.31,427.67L93.31,234.82C93.23,226.58 94.86,218.63 98.21,211.63C101.81,204.11 107.39,197.69 114.99,193.2L273.07,99.95C287.75,91.27 306.01,91.1 320.86,99.49L480.67,189.75C487.79,193.78 494.03,199.46 498.59,206.19C503.57,213.5 506.6,222.02 506.69,230.91L506.69,414.43C506.85,431.49 497.87,447.39 483.18,456.06L325.11,549.32C317.74,553.68 309.48,555.87 301.2,555.94Z"
32-
android:strokeWidth="10"
33-
android:fillColor="#00000000"
34-
android:strokeColor="#083042"
35-
android:fillType="nonZero"/>
36-
</vector>
2+
android:width="600dp"
3+
android:height="600dp"
4+
android:viewportWidth="600"
5+
android:viewportHeight="600">
6+
<path
7+
android:pathData="M301.21,418.53C300.97,418.54 300.73,418.56 300.49,418.56C297.09,418.59 293.74,417.72 290.79,416.05L222.6,377.54C220.63,376.43 219,374.82 217.85,372.88C216.7,370.94 216.09,368.73 216.07,366.47L216.07,288.16C216.06,287.32 216.09,286.49 216.17,285.67C216.38,283.54 216.91,281.5 217.71,279.6L199.29,268.27L177.74,256.19C175.72,260.43 174.73,265.23 174.78,270.22L174.79,387.05C174.85,393.89 178.57,400.2 184.53,403.56L286.26,461.02C290.67,463.51 295.66,464.8 300.73,464.76C300.91,464.76 301.09,464.74 301.27,464.74C301.24,449.84 301.22,439.23 301.22,439.23L301.21,418.53Z"
8+
android:fillColor="#041619"
9+
android:fillType="nonZero"/>
10+
<path
11+
android:pathData="M409.45,242.91L312.64,188.23C303.64,183.15 292.58,183.26 283.68,188.51L187.92,245C183.31,247.73 179.93,251.62 177.75,256.17L177.74,256.19L199.29,268.27L217.71,279.6C217.83,279.32 217.92,279.02 218.05,278.74C218.24,278.36 218.43,277.98 218.64,277.62C219.06,276.88 219.52,276.18 220.04,275.51C221.37,273.8 223.01,272.35 224.87,271.25L289.06,233.39C290.42,232.59 291.87,231.96 293.39,231.51C295.53,230.87 297.77,230.6 300,230.72C302.98,230.88 305.88,231.73 308.47,233.2L373.37,269.85C375.54,271.08 377.49,272.68 379.13,274.57C379.68,275.19 380.18,275.85 380.65,276.53C380.86,276.84 381.05,277.15 381.24,277.47L397.79,266.39L420.34,252.93L420.31,252.88C417.55,248.8 413.77,245.35 409.45,242.91Z"
12+
android:fillColor="#37BF6E"
13+
android:fillType="nonZero"/>
14+
<path
15+
android:pathData="M381.24,277.47C381.51,277.92 381.77,278.38 382.01,278.84C382.21,279.24 382.39,279.65 382.57,280.06C382.91,280.88 383.19,281.73 383.41,282.59C383.74,283.88 383.92,285.21 383.93,286.57L383.93,361.1C383.96,363.95 383.35,366.77 382.16,369.36C381.93,369.86 381.69,370.35 381.42,370.83C379.75,373.79 377.32,376.27 374.39,378L310.2,415.87C307.47,417.48 304.38,418.39 301.21,418.53L301.22,439.23C301.22,439.23 301.24,449.84 301.27,464.74C306.1,464.61 310.91,463.3 315.21,460.75L410.98,404.25C419.88,399 425.31,389.37 425.22,379.03L425.22,267.85C425.17,262.48 423.34,257.34 420.34,252.93L397.79,266.39L381.24,277.47Z"
16+
android:fillColor="#3870B2"
17+
android:fillType="nonZero"/>
18+
<path
19+
android:pathData="M177.75,256.17C179.93,251.62 183.31,247.73 187.92,245L283.68,188.51C292.58,183.26 303.64,183.15 312.64,188.23L409.45,242.91C413.77,245.35 417.55,248.8 420.31,252.88L420.34,252.93L498.59,206.19C494.03,199.46 487.79,193.78 480.67,189.75L320.86,99.49C306.01,91.1 287.75,91.27 273.07,99.95L114.99,193.2C107.39,197.69 101.81,204.11 98.21,211.63L177.74,256.19L177.75,256.17ZM301.27,464.74C301.09,464.74 300.91,464.76 300.73,464.76C295.66,464.8 290.67,463.51 286.26,461.02L184.53,403.56C178.57,400.2 174.85,393.89 174.79,387.05L174.78,270.22C174.73,265.23 175.72,260.43 177.74,256.19L98.21,211.63C94.86,218.63 93.23,226.58 93.31,234.82L93.31,427.67C93.42,438.97 99.54,449.37 109.4,454.92L277.31,549.77C284.6,553.88 292.84,556.01 301.2,555.94L301.2,555.8C301.39,543.78 301.33,495.26 301.27,464.74Z"
20+
android:strokeWidth="10"
21+
android:fillColor="#00000000"
22+
android:strokeColor="#083042"
23+
android:fillType="nonZero"/>
24+
<path
25+
android:pathData="M498.59,206.19L420.34,252.93C423.34,257.34 425.17,262.48 425.22,267.85L425.22,379.03C425.31,389.37 419.88,399 410.98,404.25L315.21,460.75C310.91,463.3 306.1,464.61 301.27,464.74C301.33,495.26 301.39,543.78 301.2,555.8L301.2,555.94C309.48,555.87 317.74,553.68 325.11,549.32L483.18,456.06C497.87,447.39 506.85,431.49 506.69,414.43L506.69,230.91C506.6,222.02 503.57,213.5 498.59,206.19Z"
26+
android:strokeWidth="10"
27+
android:fillColor="#00000000"
28+
android:strokeColor="#083042"
29+
android:fillType="nonZero"/>
30+
<path
31+
android:pathData="M301.2,555.94C292.84,556.01 284.6,553.88 277.31,549.76L109.4,454.92C99.54,449.37 93.42,438.97 93.31,427.67L93.31,234.82C93.23,226.58 94.86,218.63 98.21,211.63C101.81,204.11 107.39,197.69 114.99,193.2L273.07,99.95C287.75,91.27 306.01,91.1 320.86,99.49L480.67,189.75C487.79,193.78 494.03,199.46 498.59,206.19C503.57,213.5 506.6,222.02 506.69,230.91L506.69,414.43C506.85,431.49 497.87,447.39 483.18,456.06L325.11,549.32C317.74,553.68 309.48,555.87 301.2,555.94Z"
32+
android:strokeWidth="10"
33+
android:fillColor="#00000000"
34+
android:strokeColor="#083042"
35+
android:fillType="nonZero"/>
36+
</vector>

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

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

33
import androidx.compose.animation.ExperimentalSharedTransitionApi
4-
import androidx.compose.animation.SharedTransitionLayout
54
import androidx.compose.foundation.isSystemInDarkTheme
65
import androidx.compose.foundation.layout.Box
76
import androidx.compose.foundation.layout.fillMaxSize

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ object SimbotNames {
2020
val eventClassName = ClassName(simbotEventPkg, "Event")
2121
val msgEventClassName = ClassName(simbotEventPkg, "MessageEvent")
2222
val contactMsgEventClassName = ClassName(simbotEventPkg, "ContactMessageEvent")
23-
23+
2424
// Application classes for Core framework
2525
val application = ClassName(simbotApplicationPkg, "Application")
2626
val applicationBuilder = ClassName(simbotApplicationPkg, "ApplicationBuilder")
27-
}
27+
}

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/components/GroupCard.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
88
import androidx.compose.foundation.interaction.collectIsHoveredAsState
99
import androidx.compose.foundation.layout.*
1010
import androidx.compose.foundation.shape.RoundedCornerShape
11-
import androidx.compose.material3.*
12-
import androidx.compose.runtime.*
11+
import androidx.compose.material3.CardDefaults
12+
import androidx.compose.material3.MaterialTheme
13+
import androidx.compose.material3.OutlinedCard
14+
import androidx.compose.material3.Text
15+
import androidx.compose.runtime.Composable
16+
import androidx.compose.runtime.getValue
17+
import androidx.compose.runtime.remember
1318
import androidx.compose.ui.Modifier
1419
import androidx.compose.ui.text.font.FontWeight
1520
import androidx.compose.ui.unit.dp
@@ -87,7 +92,7 @@ fun GroupCard(
8792
fontWeight = FontWeight.Bold,
8893
color = MaterialTheme.colorScheme.primary
8994
)
90-
95+
9196
subtitle?.let {
9297
Text(
9398
text = it,
@@ -96,7 +101,7 @@ fun GroupCard(
96101
)
97102
}
98103
}
99-
104+
100105
// 内容区域
101106
Column(
102107
modifier = Modifier.fillMaxWidth(),
@@ -109,7 +114,7 @@ fun GroupCard(
109114

110115
/**
111116
* 轻量级的分组容器,用于不需要卡片样式的简单分组
112-
*
117+
*
113118
* @param title 分组标题
114119
* @param modifier 修饰符
115120
* @param subtitle 可选的副标题
@@ -136,7 +141,7 @@ fun GroupContainer(
136141
fontWeight = FontWeight.Bold,
137142
color = MaterialTheme.colorScheme.primary
138143
)
139-
144+
140145
subtitle?.let {
141146
Text(
142147
text = it,
@@ -145,7 +150,7 @@ fun GroupContainer(
145150
)
146151
}
147152
}
148-
153+
149154
// 内容区域
150155
Column(
151156
modifier = Modifier.fillMaxWidth(),
@@ -157,7 +162,7 @@ fun GroupContainer(
157162

158163
/**
159164
* 响应式的分组网格,用于在不同屏幕尺寸下展示分组
160-
*
165+
*
161166
* @param modifier 修饰符
162167
* @param windowSize 窗口尺寸
163168
* @param content 分组内容

composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen/components/SearchingIcon.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package love.forte.simbot.codegen.components
22

33
import androidx.compose.animation.animateColor
4-
import androidx.compose.animation.core.InfiniteRepeatableSpec
5-
import androidx.compose.animation.core.RepeatMode
6-
import androidx.compose.animation.core.animateFloat
7-
import androidx.compose.animation.core.infiniteRepeatable
8-
import androidx.compose.animation.core.rememberInfiniteTransition
9-
import androidx.compose.animation.core.tween
4+
import androidx.compose.animation.core.*
105
import androidx.compose.foundation.layout.size
116
import androidx.compose.material.icons.Icons
127
import androidx.compose.material.icons.outlined.Search

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class GradleProjectViewModel : ViewModel() {
5454

5555
var simbotVersion: String? by mutableStateOf(null)
5656
var kotlinVersion: String by mutableStateOf("2.1.20") // TODO initial able?
57-
57+
5858
// 编程语言选择
5959
var programmingLanguage: ProgrammingLanguage by mutableStateOf(ProgrammingLanguage.Kotlin("2.1.20"))
60-
60+
6161
// Java 样式选择(仅在选择 Java 语言时有效)
6262
var javaStyle: JavaStyle by mutableStateOf(JavaStyle.BLOCKING)
6363

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import love.forte.codegentle.common.naming.MemberName
88
import love.forte.codegentle.common.naming.parseToPackageName
99
import love.forte.codegentle.common.ref.addAnnotation
1010
import love.forte.codegentle.common.ref.ref
11-
import love.forte.codegentle.kotlin.*
11+
import love.forte.codegentle.kotlin.KotlinFile
12+
import love.forte.codegentle.kotlin.addSimpleClassType
13+
import love.forte.codegentle.kotlin.modifiers
1214
import love.forte.codegentle.kotlin.naming.KotlinClassNames
1315
import love.forte.codegentle.kotlin.spec.KotlinFunctionSpec
1416
import love.forte.codegentle.kotlin.spec.addFunction
1517
import love.forte.codegentle.kotlin.spec.addMainFunction
1618
import love.forte.codegentle.kotlin.spec.addParameter
19+
import love.forte.codegentle.kotlin.writeToKotlinString
1720
import love.forte.simbot.codegen.codegen.SimbotComponent
21+
import love.forte.simbot.codegen.codegen.SimbotComponent.*
1822
import love.forte.simbot.codegen.codegen.naming.SimbotNames
1923
import love.forte.simbot.codegen.codegen.naming.SpringNames
20-
import love.forte.simbot.codegen.codegen.SimbotComponent.*
2124
import love.forte.simbot.codegen.toRelativePath0
2225

2326

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import love.forte.simbot.codegen.gen.GradleCatalogVersionDependency
55

66
/**
77
* 代码生成器的核心接口。
8-
*
8+
*
99
* 代码生成器负责生成特定类型的代码,例如项目结构、源代码文件、配置文件等。
1010
* 不同的代码生成器可以组合使用,以生成完整的项目。
11-
*
11+
*
1212
* @author ForteScarlet
1313
*/
1414
interface CodeGenerator {
1515
/**
1616
* 生成代码并将结果写入指定的 [JSZip] 对象。
17-
*
17+
*
1818
* @param zip 用于存储生成的文件的 JSZip 对象
1919
* @param context 代码生成的上下文信息
2020
*/
@@ -23,35 +23,35 @@ interface CodeGenerator {
2323

2424
/**
2525
* 代码生成的上下文信息。
26-
*
26+
*
2727
* 上下文包含生成代码所需的所有配置和状态信息。
2828
*/
2929
interface GenerationContext {
3030
/**
3131
* 项目名称
3232
*/
3333
val projectName: String
34-
34+
3535
/**
3636
* 项目包名
3737
*/
3838
val packageName: String
39-
39+
4040
/**
4141
* 项目语言
4242
*/
4343
val language: ProgrammingLanguage
44-
44+
4545
/**
4646
* 项目框架
4747
*/
4848
val framework: Framework
49-
49+
5050
/**
5151
* 项目组件
5252
*/
5353
val components: List<Component>
54-
54+
5555
/**
5656
* 项目依赖
5757
*/
@@ -65,20 +65,20 @@ interface GenerationContext {
6565

6666
/**
6767
* 编程语言。
68-
*
68+
*
6969
* 定义了项目使用的编程语言,例如 Kotlin 或 Java。
7070
*/
7171
sealed class ProgrammingLanguage {
7272
/**
7373
* 语言的名称
7474
*/
7575
abstract val name: String
76-
76+
7777
/**
7878
* 语言的文件扩展名
7979
*/
8080
abstract val fileExtension: String
81-
81+
8282
/**
8383
* Kotlin 语言
8484
*/
@@ -91,7 +91,7 @@ sealed class ProgrammingLanguage {
9191
override val name: String = "Kotlin"
9292
override val fileExtension: String = "kt"
9393
}
94-
94+
9595
/**
9696
* Java 语言
9797
*/
@@ -100,7 +100,7 @@ sealed class ProgrammingLanguage {
100100
* Java 版本
101101
*/
102102
val version: String,
103-
103+
104104
/**
105105
* API 风格:阻塞式或异步式
106106
*/
@@ -119,7 +119,7 @@ enum class JavaStyle {
119119
* 阻塞式 API
120120
*/
121121
BLOCKING,
122-
122+
123123
/**
124124
* 异步式 API
125125
*/
@@ -128,15 +128,15 @@ enum class JavaStyle {
128128

129129
/**
130130
* 框架。
131-
*
131+
*
132132
* 定义了项目使用的框架,例如 Spring 或非 Spring。
133133
*/
134134
sealed class Framework {
135135
/**
136136
* 框架的名称
137137
*/
138138
abstract val name: String
139-
139+
140140
/**
141141
* Spring 框架
142142
*/
@@ -148,7 +148,7 @@ sealed class Framework {
148148
) : Framework() {
149149
override val name: String = "Spring"
150150
}
151-
151+
152152
/**
153153
* 非 Spring 框架(核心库)
154154
*/
@@ -159,15 +159,15 @@ sealed class Framework {
159159

160160
/**
161161
* 组件。
162-
*
162+
*
163163
* 定义了项目使用的 Simbot 组件,例如 QQ、KOOK 等。
164164
*/
165165
interface Component {
166166
/**
167167
* 组件的名称
168168
*/
169169
val name: String
170-
170+
171171
/**
172172
* 组件的版本
173173
*/
@@ -176,25 +176,25 @@ interface Component {
176176

177177
/**
178178
* 依赖。
179-
*
179+
*
180180
* 定义了项目的依赖,例如库、框架等。
181181
*/
182182
interface Dependency {
183183
/**
184184
* 依赖的组 ID
185185
*/
186186
val group: String
187-
187+
188188
/**
189189
* 依赖的名称
190190
*/
191191
val name: String
192-
192+
193193
/**
194194
* 依赖的版本
195195
*/
196196
val version: String
197-
197+
198198
/**
199199
* 依赖的配置名称,例如 implementation、api 等
200200
*/

0 commit comments

Comments
 (0)