diff --git a/examples/gradle/libs.versions.toml b/examples/gradle/libs.versions.toml index 2964147..d9bd908 100644 --- a/examples/gradle/libs.versions.toml +++ b/examples/gradle/libs.versions.toml @@ -3,10 +3,10 @@ # /!\ Koin in gradle.properties /!\ # Core -kotlin = "1.9.23" +kotlin = "1.9.24" koin = "3.5.6" -koinAnnotations = "1.4.0-Alpha1" -ksp = "1.9.23-1.0.20" +koinAnnotations = "1.4.0-RC2" +ksp = "1.9.24-1.0.20" junit = "4.13.2" # Android agp = "7.4.2" diff --git a/examples/other-ksp/src/main/kotlin/org/koin/example/newmodule/ComponentWithDefaultValues.kt b/examples/other-ksp/src/main/kotlin/org/koin/example/newmodule/ComponentWithDefaultValues.kt index 6fe6f81..eee3a51 100644 --- a/examples/other-ksp/src/main/kotlin/org/koin/example/newmodule/ComponentWithDefaultValues.kt +++ b/examples/other-ksp/src/main/kotlin/org/koin/example/newmodule/ComponentWithDefaultValues.kt @@ -4,7 +4,6 @@ import org.koin.core.annotation.Factory import org.koin.core.annotation.Property import org.koin.core.annotation.PropertyValue import org.koin.core.annotation.Single -import org.koin.example.newmodule.ComponentWithProps.Companion.DEFAULT_ID public interface ComponentInterface { diff --git a/examples/other-ksp/src/test/kotlin/org.koin.example/TestModule.kt b/examples/other-ksp/src/test/kotlin/org.koin.example/TestModule.kt index 01619a7..3bcce1a 100644 --- a/examples/other-ksp/src/test/kotlin/org.koin.example/TestModule.kt +++ b/examples/other-ksp/src/test/kotlin/org.koin.example/TestModule.kt @@ -44,7 +44,6 @@ class TestModule { koin.get() koin.get() - //TODO Handle default prop koin.get().let { assertTrue { it.id == DEFAULT_ID } } diff --git a/projects/gradle.properties b/projects/gradle.properties index 5d5a5c0..b25fe1d 100644 --- a/projects/gradle.properties +++ b/projects/gradle.properties @@ -7,7 +7,7 @@ org.gradle.parallel=true #Kotlin kotlin.code.style=official #Koin -koinAnnotationsVersion=1.4.0-RC1 +koinAnnotationsVersion=1.4.0-RC2 #Android android.useAndroidX=true androidMinSDK=14 diff --git a/projects/gradle/libs.versions.toml b/projects/gradle/libs.versions.toml index 897496b..5803b7b 100644 --- a/projects/gradle/libs.versions.toml +++ b/projects/gradle/libs.versions.toml @@ -3,9 +3,9 @@ # /!\ Koin in gradle.properties /!\ # Core -kotlin = "1.9.23" +kotlin = "1.9.24" koin = "3.5.6" -ksp = "1.9.23-1.0.20" +ksp = "1.9.24-1.0.20" publish = "2.0.0" dokka = "1.9.10" diff --git a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/BuilderProcessor.kt b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/BuilderProcessor.kt index 0d3304c..2e49d83 100644 --- a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/BuilderProcessor.kt +++ b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/BuilderProcessor.kt @@ -24,7 +24,7 @@ import org.koin.compiler.scanner.KoinMetaDataScanner import org.koin.compiler.verify.KoinConfigVerification class BuilderProcessor( - codeGenerator: CodeGenerator, + private val codeGenerator: CodeGenerator, private val logger: KSPLogger, private val options: Map ) : SymbolProcessor { @@ -34,8 +34,7 @@ class BuilderProcessor( private val koinConfigVerification = KoinConfigVerification(codeGenerator, logger) override fun process(resolver: Resolver): List { - logger.logging("Scanning symbols ...") - + logger.logging("Scan symbols ...") val invalidSymbols = koinMetaDataScanner.scanSymbols(resolver) if (invalidSymbols.isNotEmpty()) { diff --git a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/generator/DefinitionGenerationExt.kt b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/generator/DefinitionGenerationExt.kt index 73a9b4a..223aef2 100644 --- a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/generator/DefinitionGenerationExt.kt +++ b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/generator/DefinitionGenerationExt.kt @@ -198,7 +198,7 @@ private fun generateConstructor(constructorParameters: List if (!isNullable) "params.get()" else "params.getOrNull()" is KoinMetaData.DefinitionParameter.Property -> { - val defaultValue = if (ctorParam.defaultField != null) ",${ctorParam.defaultField}" else "" + val defaultValue = ctorParam.defaultValue?.let { ",${it.field}" } ?: "" if (!isNullable) "getProperty(\"${ctorParam.value}\"$defaultValue)" else "getPropertyOrNull(\"${ctorParam.value}\",$defaultValue)" } } diff --git a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/metadata/KoinMetaData.kt b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/metadata/KoinMetaData.kt index 88b5f5b..1f6421b 100644 --- a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/metadata/KoinMetaData.kt +++ b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/metadata/KoinMetaData.kt @@ -209,7 +209,7 @@ sealed class KoinMetaData { override val name: String?, val value: String? = null, val isNullable: Boolean = false, - var defaultField: String? = null, + var defaultValue: PropertyValue? = null, override val hasDefault: Boolean ) : DefinitionParameter(isNullable) } diff --git a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinMetaDataScanner.kt b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinMetaDataScanner.kt index 8f6a6ba..c8d21a2 100644 --- a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinMetaDataScanner.kt +++ b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/scanner/KoinMetaDataScanner.kt @@ -38,9 +38,9 @@ class KoinMetaDataScanner( private val componentMetadataScanner = ClassComponentScanner(logger) private val functionMetadataScanner = FunctionComponentScanner(logger) - private var validModuleSymbols = listOf() - private var validDefinitionSymbols = listOf() - private var defaultProperties = listOf() + private var validModuleSymbols = mutableListOf() + private var validDefinitionSymbols = mutableListOf() + private var defaultProperties = mutableListOf() private var externalDefinitions = listOf() private val definitionAnnotationName = Definition::class.simpleName @@ -48,10 +48,12 @@ class KoinMetaDataScanner( @OptIn(KspExperimental::class) fun scanSymbols(resolver: Resolver): List { val moduleSymbols = resolver.getSymbolsWithAnnotation(Module::class.qualifiedName!!).toList() - val definitionSymbols = DEFINITION_ANNOTATION_LIST_TYPES.flatMap { annotation -> resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!) } + val definitionSymbols = DEFINITION_ANNOTATION_LIST_TYPES.flatMap { annotation -> + resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!) + } - validModuleSymbols = moduleSymbols.filter { it.validate() } - validDefinitionSymbols = definitionSymbols.filter { it.validate() } + validModuleSymbols.addAll(moduleSymbols.filter { it.validate() }) + validDefinitionSymbols.addAll(definitionSymbols.filter { it.validate() }) val invalidModuleSymbols = moduleSymbols.filter { !it.validate() } val invalidDefinitionSymbols = definitionSymbols.filter { !it.validate() } @@ -62,9 +64,8 @@ class KoinMetaDataScanner( return invalidSymbols } - defaultProperties = resolver.getSymbolsWithAnnotation(PropertyValue::class.qualifiedName!!).toList().filter { it.validate() } - - logger.logging("All symbols are valid") + val propertyValueSymbols = resolver.getSymbolsWithAnnotation(PropertyValue::class.qualifiedName!!).toList() + defaultProperties.addAll(propertyValueSymbols.filter { it.validate() }) externalDefinitions = resolver.getDeclarationsFromPackage("org.koin.ksp.generated") .filter { it.annotations.any { it.shortName.asString() == definitionAnnotationName } } @@ -96,11 +97,7 @@ class KoinMetaDataScanner( .let { a -> val id = a.arguments.first().value?.toString() val field = (a.parent as? KSDeclaration) - id?.let { - field?.qualifiedName?.asString()?.let { - KoinMetaData.PropertyValue(id = id , it) - } - } + id?.let { field?.qualifiedName?.asString()?.let { KoinMetaData.PropertyValue(id = id , it) } } } } @@ -109,11 +106,13 @@ class KoinMetaDataScanner( .flatMap { it.parameters } .filterIsInstance() - //attribute default values - propertyValues.forEach { pv -> allProperties - .filter { it.value == pv.id } - .forEach { it.defaultField = pv.field } - } + //associate default values + propertyValues + .forEach { propertyValue -> + allProperties.filter { it.value == propertyValue.id }.forEach { + it.defaultValue = propertyValue + } + } } private fun scanExternalDefinitions(index: List) { @@ -198,7 +197,7 @@ class KoinMetaDataScanner( val alreadyExists = foundModule.definitions.contains(definition) if (!alreadyExists) { if (foundModule == defaultModule) { - logger.warn("No module found for '$definitionPackage.${definition.label}'. Definition is added to 'defaultModule'") + logger.info("No module found for '$definitionPackage.${definition.label}'. Definition is added to 'defaultModule'") } foundModule.definitions.add(definition) } else { diff --git a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/verify/KoinConfigVerification.kt b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/verify/KoinConfigVerification.kt index f9baab8..a1176bb 100644 --- a/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/verify/KoinConfigVerification.kt +++ b/projects/koin-ksp-compiler/src/jvmMain/kotlin/org/koin/compiler/verify/KoinConfigVerification.kt @@ -36,13 +36,13 @@ class KoinConfigVerification(val codeGenerator: CodeGenerator, val logger: KSPLo moduleList: List, resolver: Resolver ) { - val noGenFile = codeGenerator.generatedFile.isEmpty() - val alreadyDeclared = arrayListOf() + val isAlreadyGenerated = codeGenerator.generatedFile.isEmpty() + val alreadyDeclaredTags = arrayListOf() moduleList .flatMap { it.definitions } .forEach { def -> - if (noGenFile) { + if (isAlreadyGenerated) { def.parameters .filterIsInstance() .forEach { param -> @@ -52,7 +52,7 @@ class KoinConfigVerification(val codeGenerator: CodeGenerator, val logger: KSPLo //TODO Check Cycle } } else { - writeDefinitionTag(def, alreadyDeclared) + writeDefinitionTag(def, alreadyDeclaredTags) } } }