diff --git a/ksp/src/main/kotlin/fr/xgouchet/elmyr/ksp/ForgerableProcessor.kt b/ksp/src/main/kotlin/fr/xgouchet/elmyr/ksp/ForgerableProcessor.kt index 9604f56..55d1a31 100644 --- a/ksp/src/main/kotlin/fr/xgouchet/elmyr/ksp/ForgerableProcessor.kt +++ b/ksp/src/main/kotlin/fr/xgouchet/elmyr/ksp/ForgerableProcessor.kt @@ -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 @@ -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 @@ -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()}()") }