Skip to content

Commit 8d7c1e0

Browse files
add timeout for waiting ktlint process
1 parent af0a176 commit 8d7c1e0

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/KtormProcessorProvider.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class KtormProcessorProvider : SymbolProcessorProvider {
6565
val fileSpec = FileGenerator.generate(table, environment)
6666

6767
// Beautify the generated code.
68-
val formattedCode = formatter.format(fileSpec.toString())
68+
val fileName = fileSpec.packageName.replace('.', '/') + "/" + fileSpec.name + ".kt"
69+
val formattedCode = formatter.format(fileName, fileSpec.toString())
6970

7071
// Output the formatted code.
7172
val dependencies = Dependencies(false, *table.getDependencyFiles().toTypedArray())
@@ -88,7 +89,7 @@ public class KtormProcessorProvider : SymbolProcessorProvider {
8889
} catch (_: NoClassDefFoundError) {
8990
}
9091

91-
return CodeFormatter { code -> code }
92+
return CodeFormatter { _, code -> code }
9293
}
9394

9495
private fun TableMetadata.getDependencyFiles(): List<KSFile> {

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/CodeFormatter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ internal fun interface CodeFormatter {
2424
/**
2525
* Format the generated code to the community recommended coding style.
2626
*/
27-
fun format(code: String): String
27+
fun format(fileName: String, code: String): String
2828
}

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/KtLintCodeFormatter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal class KtLintCodeFormatter(val environment: SymbolProcessorEnvironment)
3838
)
3939
)
4040

41-
override fun format(code: String): String {
41+
override fun format(fileName: String, code: String): String {
4242
try {
4343
// Manually fix some code styles before formatting.
4444
val snippet = code

ktorm-ksp-compiler/src/main/kotlin/org/ktorm/ksp/compiler/formatter/StandaloneKtLintCodeFormatter.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ package org.ktorm.ksp.compiler.formatter
1818

1919
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
2020
import java.io.File
21+
import java.util.concurrent.TimeUnit
2122

2223
internal class StandaloneKtLintCodeFormatter(val environment: SymbolProcessorEnvironment) : CodeFormatter {
24+
private val logger = environment.logger
2325
private val command = buildCommand()
2426

2527
init {
26-
environment.logger.info("[ktorm-ksp-compiler] init ktlint formatter with command: ${command.joinToString(" ")}")
28+
logger.info("[ktorm-ksp-compiler] init ktlint formatter with command: ${command.joinToString(" ")}")
2729
}
2830

29-
override fun format(code: String): String {
31+
override fun format(fileName: String, code: String): String {
3032
try {
3133
val p = ProcessBuilder(command).start()
3234
p.outputStream.bufferedWriter(Charsets.UTF_8).use { it.write(preprocessCode(code)) }
33-
p.waitFor()
35+
36+
if (!p.waitFor(30, TimeUnit.SECONDS)) {
37+
logger.info("[ktorm-ksp-compiler] ktlint execution timeout, skip code formatting for file: $fileName")
38+
return code
39+
}
3440

3541
val formattedCode = p.inputStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
3642
if (p.exitValue() == 0) {
@@ -43,12 +49,12 @@ internal class StandaloneKtLintCodeFormatter(val environment: SymbolProcessorEnv
4349
} else {
4450
// Exit exceptionally.
4551
val msg = p.errorStream.bufferedReader(Charsets.UTF_8).use { it.readText() }
46-
environment.logger.error("[ktorm-ksp-compiler] ktlint exit with code: ${p.exitValue()}\n$msg")
52+
logger.error("[ktorm-ksp-compiler] ktlint exit with code: ${p.exitValue()}\n$msg")
4753
return code
4854
}
4955
}
5056
} catch (e: Throwable) {
51-
environment.logger.exception(e)
57+
logger.exception(e)
5258
return code
5359
}
5460
}

0 commit comments

Comments
 (0)