Skip to content

Commit

Permalink
Fix registration file hierarchy setting retention from library settings
Browse files Browse the repository at this point in the history
  • Loading branch information
chippmann committed Nov 3, 2024
1 parent ac2a1bd commit 94d80b3
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ properties = [
]
functions = [
provide_greeting
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object EntryGenerator {
classRegistrarFromDependencyCount: Int,
logger: Logger,
sourceFiles: List<SourceFile>,
isRegistrationFileHierarchyEnabled: Boolean,
jvmTypeFqNamesProvider: (JvmType) -> Set<String>,
compilationTimeRelativeRegistrationFilePathProvider: (RegisteredClass) -> String,
classRegistrarAppendableProvider: (RegisteredClass) -> BufferedWriter,
Expand Down Expand Up @@ -67,7 +68,8 @@ object EntryGenerator {
projectName = projectName,
registeredClass = registeredClass,
registrarAppendableProvider = classRegistrarAppendableProvider,
compilationTimeRelativeRegistrationFilePath = compilationTimeRelativeRegistrationFilePathProvider(registeredClass)
compilationTimeRelativeRegistrationFilePath = compilationTimeRelativeRegistrationFilePathProvider(registeredClass),
isRegistrationFileHierarchyEnabled = isRegistrationFileHierarchyEnabled,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClassRegistrarFileBuilder(
private val registeredClass: RegisteredClass,
private val compilationTimeRelativeRegistrationFilePath: String,
private val registrarAppendableProvider: (RegisteredClass) -> BufferedWriter,
isRegistrationFileHierarchyEnabled: Boolean,
) {
private val classRegistrarBuilder = TypeSpec
.classBuilder("${registeredClass.registeredName}Registrar")
Expand All @@ -41,6 +42,7 @@ class ClassRegistrarFileBuilder(
.addMember("%S", registeredClass.signals.joinToString(",") { it.fqName })
.addMember("%S", registeredClass.properties.joinToString(",") { it.fqName })
.addMember("%S", registeredClass.functions.filter { it.name != GodotFunctions.notification }.joinToString(",") { it.fqName })
.addMember("%L", isRegistrationFileHierarchyEnabled)
.build()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ data class RegisteredClassMetadataContainer(
val signals: String,
val properties: String,
val functions: String,
val isRegistrationFileHierarchyEnabled: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class GodotKotlinSymbolProcessor(
?: throw IllegalStateException("No isRegistrationFileHierarchyEnabled option provided or not a boolean"),
isRegistrationFileGenerationEnabled = options["isRegistrationFileGenerationEnabled"]?.toBooleanStrictOrNull()
?: throw IllegalStateException("No isRegistrationFileGenerationEnabled option provided or not a boolean"),
registeredClassMetadataContainers = emptyList(),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package godot.annotation.processor

import godot.entrygenerator.model.RegisteredClassMetadataContainer
import java.io.File

internal data class Settings(
Expand All @@ -10,4 +11,5 @@ internal data class Settings(
val isFqNameRegistrationEnabled: Boolean,
val isRegistrationFileHierarchyEnabled: Boolean,
val isRegistrationFileGenerationEnabled: Boolean,
var registeredClassMetadataContainers: List<RegisteredClassMetadataContainer>,
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package godot.annotation.processor.ext

import godot.annotation.processor.Settings
import godot.tools.common.constants.FileExtensions
import org.jetbrains.kotlin.konan.properties.suffix

internal fun provideRegistrationFilePathForInitialGenerationWithoutExtension(
isRegistrationFileHierarchyEnabled: Boolean,
settings: Settings,
fqName: String,
registeredName: String,
compilationProjectName: String,
classProjectName: String,
registrationFileOutDir: String
): String {
val registrationMetadata = settings.registeredClassMetadataContainers.firstOrNull { it.fqName == fqName }
val isRegistrationFileHierarchyEnabled = if (registrationMetadata != null) {
registrationMetadata.isRegistrationFileHierarchyEnabled
} else {
settings.isRegistrationFileHierarchyEnabled
}
val registrationFileRelativePath = if (isRegistrationFileHierarchyEnabled && fqName.contains(".")) {
fqName.substringBeforeLast(".").replace(".", "/")
} else ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,48 @@ internal fun KSType.provideRegisteredClassName(
internal fun KSDeclaration.provideRegisteredClassName(
settings: Settings,
): String? {
val registerClassAnnotation = annotations
.firstOrNull { it.fqNameUnsafe == RegisterClass::class.qualifiedName }
return settings
.registeredClassMetadataContainers
.firstOrNull { it.fqName == this.qualifiedName?.asString() }
?.registeredName
?: run {
val registerClassAnnotation = annotations
.firstOrNull { it.fqNameUnsafe == RegisterClass::class.qualifiedName }

val customName = registerClassAnnotation
?.arguments
?.first()
?.value as? String
val customName = registerClassAnnotation
?.arguments
?.first()
?.value as? String

val fqName = this.qualifiedName?.asString() ?: return null
val fqName = this.qualifiedName?.asString() ?: return null

val registeredName = if (customName.isNullOrEmpty()) {
if (settings.isFqNameRegistrationEnabled) {
fqName.replace(".", "_")
} else {
if (fqName.contains(".")) {
fqName.substringAfterLast(".")
val registeredName = if (customName.isNullOrEmpty()) {
if (settings.isFqNameRegistrationEnabled) {
fqName.replace(".", "_")
} else {
if (fqName.contains(".")) {
fqName.substringAfterLast(".")
} else {
fqName
}
}
} else {
fqName
customName
}
}
} else {
customName
}

return if (settings.classPrefix != null) {
if (registeredName.contains("_")) {
val packageName = registeredName.substringBeforeLast("_")
val classNameWithPrefix = registeredName
.substringAfterLast("_")
.let { className -> "${settings.classPrefix.uppercase()}$className" }
if (settings.classPrefix != null) {
if (registeredName.contains("_")) {
val packageName = registeredName.substringBeforeLast("_")
val classNameWithPrefix = registeredName
.substringAfterLast("_")
.let { className -> "${settings.classPrefix.uppercase()}$className" }

"${packageName}_$classNameWithPrefix"
} else {
"${settings.classPrefix.uppercase()}$registeredName"
"${packageName}_$classNameWithPrefix"
} else {
"${settings.classPrefix.uppercase()}$registeredName"
}
} else {
registeredName
}
}
} else {
registeredName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ internal class RoundGenerateRegistrarsForCurrentProjectAndDependencyRegistration
private val settings: Settings,
) : BaseRound() {
override fun executeInternal(): List<KSAnnotated> {
val metadataAnnotationVisitor = MetadataAnnotationVisitor()
resolver.getDeclarationsFromPackage(godotEntryBasePackage).forEach { declaration ->
declaration.accept(metadataAnnotationVisitor, Unit)
}
settings.registeredClassMetadataContainers = metadataAnnotationVisitor.registeredClassMetadataContainers

val registerAnnotationVisitor = RegistrationAnnotationVisitor(
settings = settings,
)
Expand All @@ -37,11 +43,6 @@ internal class RoundGenerateRegistrarsForCurrentProjectAndDependencyRegistration
it.accept(registerAnnotationVisitor, Unit)
}

val metadataAnnotationVisitor = MetadataAnnotationVisitor()
resolver.getDeclarationsFromPackage(godotEntryBasePackage).forEach { declaration ->
declaration.accept(metadataAnnotationVisitor, Unit)
}


// generate entry files for this compilation
EntryGenerator.generateEntryFiles(
Expand All @@ -51,14 +52,15 @@ internal class RoundGenerateRegistrarsForCurrentProjectAndDependencyRegistration
classRegistrarFromDependencyCount = metadataAnnotationVisitor.registeredClassMetadataContainers.size,
logger = LoggerWrapper(logger),
sourceFiles = registerAnnotationVisitor.sourceFilesContainingRegisteredClasses,
isRegistrationFileHierarchyEnabled = settings.isRegistrationFileHierarchyEnabled,
jvmTypeFqNamesProvider = JvmTypeProvider(),
compilationTimeRelativeRegistrationFilePathProvider = { registeredClass ->
val registrationFile = blackboard
.existingRegistrationFilesMap["${registeredClass.registeredName}.${FileExtensions.GodotKotlinJvm.registrationFile}"]
?.relativeTo(settings.projectBaseDir)
?: File(
provideRegistrationFilePathForInitialGenerationWithoutExtension(
isRegistrationFileHierarchyEnabled = settings.isRegistrationFileHierarchyEnabled,
settings = settings,
fqName = registeredClass.fqName,
registeredName = registeredClass.registeredName,
compilationProjectName = settings.projectName,
Expand Down Expand Up @@ -100,7 +102,7 @@ internal class RoundGenerateRegistrarsForCurrentProjectAndDependencyRegistration
blackboard.alreadyGeneratedRegistrationFiles.add(metadata.fqName)

val registrationFile = provideRegistrationFilePathForInitialGenerationWithoutExtension(
isRegistrationFileHierarchyEnabled = settings.isRegistrationFileHierarchyEnabled,
settings = settings,
fqName = metadata.fqName,
registeredName = metadata.registeredName,
compilationProjectName = settings.projectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class RoundGenerateRegistrationFilesForCurrentCompilation(
blackboard.alreadyGeneratedRegistrationFiles.add(metadata.fqName)

val resourcePathFromProjectRoot = provideRegistrationFilePathForInitialGenerationWithoutExtension(
isRegistrationFileHierarchyEnabled = settings.isRegistrationFileHierarchyEnabled,
settings = settings,
fqName = metadata.fqName,
registeredName = metadata.registeredName,
compilationProjectName = settings.projectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal class MetadataAnnotationVisitor: KSVisitorVoid() {
signals = annotation.signals,
properties = annotation.properties,
functions = annotation.functions,
isRegistrationFileHierarchyEnabled = annotation.isRegistrationFileHierarchyEnabled
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ annotation class RegisteredClassMetadata(
val signals: String,
val properties: String,
val functions: String,
val isRegistrationFileHierarchyEnabled: Boolean
)

0 comments on commit 94d80b3

Please sign in to comment.