Skip to content

Commit

Permalink
Prevent IOException
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Jan 18, 2022
1 parent 6d185bf commit 355c16a
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions ksp/src/main/kotlin/fr/xgouchet/elmyr/ksp/ForgerableProcessor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.xgouchet.elmyr.ksp

import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.Dependencies
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
Expand Down Expand Up @@ -30,7 +31,8 @@ import com.squareup.kotlinpoet.STRING
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.writeTo
import java.io.IOException
import java.io.OutputStreamWriter

/**
* A Kotlin [SymbolProcessor] generating a [ForgeryFactory] implementation for every
Expand Down Expand Up @@ -127,8 +129,29 @@ class ForgerableProcessor(
) {
logger.info("${data.classQualifiedName.asString()}.${function.simpleName.asString()}()")

generateFile(function, data.packageName, data.classQualifiedName)
.writeTo(codeGenerator, false, listOf(data.file))
val fileSpec = generateFile(function, data.packageName, data.classQualifiedName)
val dependencies = Dependencies(false, data.file)
try {
val outputStream = codeGenerator.createNewFile(
dependencies,
fileSpec.packageName,
fileSpec.name
)
val writer = OutputStreamWriter(outputStream, Charsets.UTF_8)
fileSpec.writeTo(writer)
try {
writer.flush()
writer.close()
} catch (e: IOException) {
logger.warn(
"Error flushing writer for file ${fileSpec.packageName}.${fileSpec.name}: " +
"${e.message}."
)
}
} catch (e: IOException) {
logger.error("Error writing file ${fileSpec.packageName}.${fileSpec.name}")
logger.exception(e)
}

logger.info("${data.classQualifiedName.asString()}.${function.simpleName.asString()}()")
}
Expand Down

0 comments on commit 355c16a

Please sign in to comment.