Skip to content

Commit

Permalink
Fix tag generation in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Jul 19, 2024
1 parent 850ef35 commit 2c9af5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class KoinGenerator(
generateDefaultModule: Boolean
) {
logger.logging("generate default file ...")
val defaultModuleFile = codeGenerator.getFile(fileName = "Default${defaultModule.hashCode()}")
val defaultModuleFile = codeGenerator.getNewFile(fileName = "Default${defaultModule.hashCode()}")
defaultModuleFile.generateDefaultModuleHeader(defaultModule.definitions)
generateAllExternalDefinitions(defaultModule, defaultModuleFile)

Expand All @@ -80,7 +80,7 @@ class KoinGenerator(
private fun generateModule(module: KoinMetaData.Module) {
logger.logging("generate $module - ${module.type}")
// generate class module
val moduleFile = codeGenerator.getFile(fileName = module.generateModuleFileName())
val moduleFile = codeGenerator.getNewFile(fileName = module.generateModuleFileName())
//TODO Remove isComposeViewModelActive with Koin 4
generateClassModule(moduleFile, module, isComposeViewModelActive)
}
Expand All @@ -96,7 +96,7 @@ class KoinGenerator(
}
}

fun CodeGenerator.getFile(packageName: String = "org.koin.ksp.generated", fileName: String): OutputStream {
fun CodeGenerator.getNewFile(packageName: String = "org.koin.ksp.generated", fileName: String): OutputStream {
return try {
createNewFile(
Dependencies.ALL_FILES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSDeclaration
import org.koin.compiler.generator.getFile
import org.koin.compiler.generator.getNewFile
import org.koin.compiler.metadata.KoinMetaData
import java.io.OutputStream

Expand All @@ -39,38 +39,42 @@ class KoinConfigVerification(val codeGenerator: CodeGenerator, val logger: KSPLo
val isAlreadyGenerated = codeGenerator.generatedFile.isEmpty()
val alreadyDeclaredTags = arrayListOf<String>()

moduleList
.flatMap { it.definitions }
.forEach { def ->
if (isAlreadyGenerated) {
def.parameters
.filterIsInstance<KoinMetaData.DefinitionParameter.Dependency>()
.forEach { param ->
if (!param.hasDefault && !param.isNullable && !param.alreadyProvided) {
checkDependencyIsDefined(param, resolver, def)
}
//TODO Check Cycle
val allDefinitions = moduleList.flatMap { it.definitions }

if (!isAlreadyGenerated) {
val tagFileName = "DefinitionTags-${hashCode()}"
val tagFileStream = writeDefinitionTagFile(tagFileName)
allDefinitions.forEach { def ->
writeDefinitionTag(tagFileStream, def, alreadyDeclaredTags)
}
} else {
allDefinitions.forEach { def ->
def.parameters
.filterIsInstance<KoinMetaData.DefinitionParameter.Dependency>()
.forEach { param ->
if (!param.hasDefault && !param.isNullable && !param.alreadyProvided) {
checkDependencyIsDefined(param, resolver, def)
}
} else {
writeDefinitionTag(def, alreadyDeclaredTags)
}
//TODO Check Cycle
}
}
}
}

private fun writeDefinitionTagFile(tagFileName: String): OutputStream {
val fileStream = codeGenerator.getNewFile(fileName = tagFileName)
fileStream.appendText("package $generationPackage")
return fileStream
}

private fun writeDefinitionTag(
fileStream: OutputStream,
def: KoinMetaData.Definition,
alreadyDeclared: ArrayList<String>
) {
val label = def.label
val className = label.capitalize()
val fileName = def.packageCamelCase() + className
fileStream.appendText("\n")

val fileStream = codeGenerator.getFile(fileName = fileName)
fileStream.appendText("package $generationPackage")

// tag for Class & Functions
writeClassTag(def, alreadyDeclared, fileStream)

def.bindings.forEach { writeDefinitionBindingTag(it, alreadyDeclared, fileStream) }
}

Expand Down

0 comments on commit 2c9af5d

Please sign in to comment.