Skip to content

Commit 18dd886

Browse files
Refactor script file creation logic: extract shell and batch script generators for improved readability and maintainability.
1 parent c091eeb commit 18dd886

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

sample-bots/csharp/build.gradle.kts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,29 @@ tasks {
2323
@Suppress("UNCHECKED_CAST")
2424
val copyBotFiles = rootProject.extra["copyBotFiles"] as (Path, Path) -> Unit
2525

26-
fun createScriptFile(projectDir: Path, botArchivePath: Path, fileExt: String, newLine: String) {
27-
val botName = projectDir.botName()
28-
val file = botArchivePath.resolve("$botName.$fileExt").toFile()
29-
val printWriter = object : PrintWriter(file) {
30-
override fun println() {
31-
write(newLine)
32-
}
33-
}
34-
printWriter.use {
35-
when (fileExt) {
36-
"sh" -> {
37-
it.println("""#!/bin/sh
26+
fun generateShellScript(): String = """#!/bin/sh
3827
if [ ! -d "bin" ]; then
3928
dotnet build
4029
fi
4130
dotnet run --no-build
4231
"""
43-
)
44-
}
4532

46-
"cmd" -> {
47-
it.println("""
48-
if not exist bin\ (
33+
fun generateBatchScript(): String = """if not exist bin\ (
4934
dotnet build >nul
5035
)
5136
dotnet run --no-build >nul
5237
"""
53-
)
54-
}
55-
}
38+
39+
fun createScriptFile(projectDir: Path, botArchivePath: Path, fileExt: String, newLine: String) {
40+
val botName = projectDir.botName()
41+
val scriptContent = when (fileExt) {
42+
"sh" -> generateShellScript()
43+
"cmd" -> generateBatchScript()
44+
else -> throw IllegalArgumentException("Unsupported file extension: $fileExt")
5645
}
46+
47+
val file = botArchivePath.resolve("$botName.$fileExt").toFile()
48+
file.writeText(scriptContent.replace("\n", newLine))
5749
}
5850

5951
fun prepareBotFiles() {

0 commit comments

Comments
 (0)