All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.voize.reaktnativetoolkit.ksp.processor.ToolkitSymbolProcessor.kt Maven / Gradle / Ivy

The newest version!
package de.voize.reaktnativetoolkit.ksp.processor

import com.google.auto.service.AutoService
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.*


class ToolkitSymbolProcessor(
    codeGenerator: CodeGenerator,
    private val platforms: List,
    options: Map,
    logger: KSPLogger,
) : SymbolProcessor {
    private var typescriptModelsGenerationInvoked = false

    private val reactNativeModuleGenerator = ReactNativeModuleGenerator(
        codeGenerator,
        platforms,
        options,
        logger,
    )

    private val reactNativeViewManagerGenerator = ReactNativeViewManagerGenerator(
        codeGenerator,
        platforms,
        options,
        logger,
    )

    private val typescriptModelsGenerator = TypescriptModelsGenerator(
        codeGenerator,
        TypescriptConfig.fromOptions(options),
        logger,
    )

    data class ProcessResult(
        val deferredSymbols: List,
        val types: List,
        val originatingFiles: List,
    )

    override fun process(resolver: Resolver): List {
        val rnModulesProcessResult = reactNativeModuleGenerator.process(resolver)
        val rnViewManagersProcessResult = reactNativeViewManagerGenerator.process(resolver)

        val exportTypescriptTypes = resolver.getSymbolsWithAnnotation(
            "$toolkitPackageName.annotation.ExportTypescriptType"
        ).map {
            when (it) {
                is KSClassDeclaration -> {
                    it.asStarProjectedType()
                }

                is KSTypeAlias -> {
                    // TODO get type of alias declaration, not the type referenced by the alias
                    error("Currently unsupported, because of missing api in KSP")
                }

                else -> throw IllegalArgumentException("ExportTypescriptType annotation can only be used on class declarations or type aliases")
            }
        }.toList()

        if (!typescriptModelsGenerationInvoked && platforms.isCommon()) {
            val (rootNamespace, typesOriginatingFiles) = TypescriptModelsNamespaceTree.build(
                rnModulesProcessResult.types + rnViewManagersProcessResult.types + exportTypescriptTypes,
            )
            typescriptModelsGenerator.createTypescriptModelsFile(
                rootNamespace,
                rnModulesProcessResult.originatingFiles + rnViewManagersProcessResult.originatingFiles + typesOriginatingFiles,
            )
            typescriptModelsGenerationInvoked = true
        }

        return rnModulesProcessResult.deferredSymbols + rnViewManagersProcessResult.deferredSymbols
    }
}

@AutoService(SymbolProcessorProvider::class)
class ToolkitSymbolProcessorProvider : SymbolProcessorProvider {
    override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
        return ToolkitSymbolProcessor(
            environment.codeGenerator,
            environment.platforms,
            environment.options,
            environment.logger
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy