File tree Expand file tree Collapse file tree 7 files changed +36
-12
lines changed
composeApp/src/wasmJsMain/kotlin/love/forte/simbot/codegen
file-saver-kotlin/src/commonMain/kotlin/love/forte/simbot/codegen/filesaver Expand file tree Collapse file tree 7 files changed +36
-12
lines changed Original file line number Diff line number Diff line change 11package love.forte.simbot.codegen
22
33import love.forte.codegentle.common.naming.isEmpty
4- import love.forte.codegentle.common.naming.toRelativePath
4+ import love.forte.codegentle.common.naming.nameSequence
5+ import love.forte.codegentle.java.JavaFile
56import love.forte.codegentle.kotlin.KotlinFile
67
7- fun KotlinFile.toRelativePath (
8+ fun KotlinFile.toRelativePath0 (
89 filename : String = type.name,
910 isScript : Boolean = false,
1011 separator : String = "/"
@@ -19,6 +20,21 @@ fun KotlinFile.toRelativePath(
1920 return if (packageName.isEmpty()) {
2021 filenameWithExtension
2122 } else {
22- packageName.toRelativePath(separator) + separator + filenameWithExtension
23+ packageName.nameSequence().joinToString(separator = separator) { it.name } + separator + filenameWithExtension
24+ }
25+ }
26+
27+ fun JavaFile.toRelativePath0 (filename : String = type.name ? : "", separator : String = "/"): String {
28+ val filenameWithExtension = if (filename.contains(' .' )) {
29+ filename
30+ } else {
31+ " $filename .java"
32+ }
33+
34+ val packageName = this .packageName
35+ return if (packageName.isEmpty()) {
36+ filenameWithExtension
37+ } else {
38+ packageName.nameSequence().joinToString(separator = separator) { it.name } + separator + filenameWithExtension
2339 }
2440}
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import love.forte.simbot.codegen.codegen.SimbotComponent
1818import love.forte.simbot.codegen.codegen.naming.SimbotNames
1919import love.forte.simbot.codegen.codegen.naming.SpringNames
2020import love.forte.simbot.codegen.codegen.SimbotComponent.*
21+ import love.forte.simbot.codegen.toRelativePath0
2122
2223
2324/* *
@@ -78,7 +79,7 @@ fun emitSpringMainFile(
7879 sourceDir : JSZip
7980) {
8081 val file = genKotlinSpringMainFile(" MainApplication" , projectPackage)
81- sourceDir.file(file.toRelativePath (), file.writeToKotlinString())
82+ sourceDir.file(file.toRelativePath0 (), file.writeToKotlinString())
8283}
8384
8485fun genKotlinSpringListenerShowcases (
@@ -245,7 +246,7 @@ fun emitSpringListenerShowcases(
245246 sourceDir : JSZip
246247) {
247248 val file = genKotlinSpringListenerShowcases(projectPackage, components)
248- sourceDir.file(file.toRelativePath (), file.writeToKotlinString())
249+ sourceDir.file(file.toRelativePath0 (), file.writeToKotlinString())
249250}
250251
251252/* *
Original file line number Diff line number Diff line change @@ -84,21 +84,22 @@ interface LanguageSpecificSourceCodeGenerator<L : ProgrammingLanguage> : SourceC
8484 /* *
8585 * 生成应用程序入口文件。
8686 *
87- * @param sourceDir 包目录的 JSZip 对象
87+ * @param sourceDir 语言源代码目录的 JSZip 对象(如 src/main/kotlin 或 src/main/java)
8888 * @param context 代码生成的上下文信息
8989 */
9090 suspend fun generateApplicationEntry (sourceDir : JSZip , context : GenerationContext )
9191
9292 /* *
9393 * 生成事件处理器文件。
9494 *
95- * @param sourceDir 包目录的 JSZip 对象
95+ * @param sourceDir 语言源代码目录的 JSZip 对象(如 src/main/kotlin 或 src/main/java)
9696 * @param context 代码生成的上下文信息
9797 */
9898 suspend fun generateEventHandlers (sourceDir : JSZip , context : GenerationContext )
9999
100100 override suspend fun generateSourceCode (sourceDir : JSZip , context : GenerationContext ) {
101- // sourceDir already points to the correct package directory from createSourceDirectory
101+ // sourceDir 指向语言级别的目录(如 src/main/kotlin),而不是包目录
102+ // 具体的包目录结构由各个生成方法内部处理
102103 generateApplicationEntry(sourceDir, context)
103104 generateEventHandlers(sourceDir, context)
104105 }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import love.forte.codegentle.java.toRelativePath
66import love.forte.simbot.codegen.gen.core.Framework
77import love.forte.simbot.codegen.gen.core.GenerationContext
88import love.forte.simbot.codegen.gen.core.ProgrammingLanguage
9+ import love.forte.simbot.codegen.toRelativePath0
910
1011/* *
1112 * Java 事件处理器生成器。
@@ -44,7 +45,7 @@ class JavaEventHandlerGenerator {
4445// val handleDir = sourceDir.folder(JavaTemplates.HANDLER_PACKAGE_SUFFIX)
4546// ?: throw IllegalStateException("无法创建 ${JavaTemplates.HANDLER_PACKAGE_SUFFIX} 目录")
4647// val fileName = JavaTemplates.getHandlerFileName()
47- sourceDir.file(handlerFile.toRelativePath (), handlerFile.toString())
48+ sourceDir.file(handlerFile.toRelativePath0 (), handlerFile.toString())
4849 }
4950
5051}
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import love.forte.simbot.codegen.gen.core.Framework
66import love.forte.simbot.codegen.gen.core.GenerationContext
77import love.forte.simbot.codegen.gen.core.JavaStyle
88import love.forte.simbot.codegen.gen.core.ProgrammingLanguage
9+ import love.forte.simbot.codegen.toRelativePath0
910
1011/* *
1112 * Java 主应用类生成器。
@@ -41,7 +42,7 @@ class JavaMainClassGenerator {
4142
4243 // 生成文件
4344// val fileName = JavaTemplates.getMainClassFileName(mainClassName)
44- packageDir.file(javaFile.toRelativePath (), javaFile.toString())
45+ packageDir.file(javaFile.toRelativePath0 (), javaFile.toString())
4546 }
4647
4748}
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ class KotlinSourceCodeGeneratorImpl : KotlinSourceCodeGenerator {
2626 override suspend fun generateApplicationEntry (sourceDir : JSZip , context : GenerationContext ) {
2727 when (context.framework) {
2828 is Framework .Spring -> {
29+ // sourceDir 已经指向 src/main/kotlin,直接调用 emitSpringMainFile
30+ // emitSpringMainFile 内部会通过 toRelativePath() 创建正确的包目录结构
2931 emitSpringMainFile(context.packageName, sourceDir)
3032 }
3133 is Framework .Core -> {
Original file line number Diff line number Diff line change 1+ @file:OptIn(ExperimentalWasmJsInterop ::class )
2+
13package love.forte.simbot.codegen.filesaver
24
3- import js.date.Date
45import web.blob.Blob
56import web.file.File
67import web.url.URL
8+ import kotlin.js.ExperimentalWasmJsInterop
79import kotlin.js.JsModule
810import kotlin.js.definedExternally
911
@@ -38,4 +40,4 @@ external fun saveTextAs(
3840
3941expect interface FileSaverSaveAsOptions {
4042 var autoBom: Boolean
41- }
43+ }
You can’t perform that action at this time.
0 commit comments