Skip to content

Commit

Permalink
Improvements on debug mode ksp runs
Browse files Browse the repository at this point in the history
  • Loading branch information
raamcosta committed Jul 20, 2024
1 parent d70d005 commit b137b95
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.ramcosta.composedestinations.codegen

import com.ramcosta.composedestinations.codegen.commons.firstTypeInfoArg
import com.ramcosta.composedestinations.codegen.commons.isCustomArrayOrArrayListTypeNavArg
import com.ramcosta.composedestinations.codegen.commons.sanitizePackageName
import com.ramcosta.composedestinations.codegen.facades.CodeOutputStreamMaker
import com.ramcosta.composedestinations.codegen.facades.Logger
import com.ramcosta.composedestinations.codegen.facades.debug
Expand All @@ -18,8 +17,6 @@ import com.ramcosta.composedestinations.codegen.servicelocator.defaultKtxSeriali
import com.ramcosta.composedestinations.codegen.servicelocator.destinationsWriter
import com.ramcosta.composedestinations.codegen.servicelocator.initialValidator
import com.ramcosta.composedestinations.codegen.servicelocator.moduleOutputWriter
import java.util.Locale
import java.util.UUID

internal const val DEFAULT_GEN_PACKAGE_NAME = "com.ramcosta.composedestinations.generated"
internal lateinit var codeGenBasePackageName: String
Expand All @@ -28,22 +25,15 @@ internal lateinit var registryId: String

class CodeGenerator(
override val codeGenerator: CodeOutputStreamMaker,
override val isBottomSheetDependencyPresent: Boolean,
override val codeGenConfig: CodeGenConfig
) : ServiceLocator {

init {
initConfigurationValues()

Logger.instance.debug {
"""
|CodeGenerator init:
|---------------------------------------
|codeGenConfig= ${pprint(codeGenConfig)}
|isBottomSheetDependencyPresent= $isBottomSheetDependencyPresent
|moduleName= '$moduleName'
|codeGenBasePackageName= '$codeGenBasePackageName'
|registryId= '$registryId'
|CodeGenerator init:
|---------------------------------------
|${pprint(codeGenConfig)}
""".trimMargin()
}
}
Expand Down Expand Up @@ -106,17 +96,6 @@ class CodeGenerator(
}
}

private fun initConfigurationValues() {
moduleName = codeGenConfig.moduleName?.replaceFirstChar { it.uppercase(Locale.US) } ?: ""
val defaultPackageName = if (moduleName.isEmpty()) {
DEFAULT_GEN_PACKAGE_NAME
} else {
"$DEFAULT_GEN_PACKAGE_NAME.${moduleName.lowercase()}".sanitizePackageName()
}
codeGenBasePackageName = codeGenConfig.packageName?.sanitizePackageName() ?: defaultPackageName
registryId = moduleName.ifEmpty { UUID.randomUUID().toString().replace("-", "_") }
}

private fun shouldWriteKtxSerializableNavTypeSerializer(
destinations: List<CodeGenProcessedDestination>,
) = destinations.any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface PPrinter {
fun pprint(any: Any): String
}

inline fun Logger.debug(message: PPrinter.() -> String) {
inline fun Logger.debug(message: PPrinter.() -> String) = synchronized(this) {
debugModeOutputPath?.let {
File(it)
.run {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
package com.ramcosta.composedestinations.codegen.model

import com.ramcosta.composedestinations.codegen.DEFAULT_GEN_PACKAGE_NAME
import com.ramcosta.composedestinations.codegen.commons.sanitizePackageName
import java.util.Locale
import java.util.UUID

data class CodeGenConfig(
val packageName: String?,
val moduleName: String?,
val packageName: String,
val moduleName: String,
val registryId: String,
val generateNavGraphs: Boolean,
val htmlMermaidGraph: String?,
val mermaidGraph: String?,
val debugModeOutputDir: String?,
)
val isBottomSheetDependencyPresent: Boolean
) {

init {
com.ramcosta.composedestinations.codegen.codeGenBasePackageName = packageName
com.ramcosta.composedestinations.codegen.moduleName = moduleName
com.ramcosta.composedestinations.codegen.registryId = registryId
}

companion object {
operator fun invoke(
packageName: String?,
moduleName: String?,
generateNavGraphs: Boolean,
htmlMermaidGraph: String?,
mermaidGraph: String?,
debugModeOutputDir: String?,
isBottomSheetDependencyPresent: Boolean
): CodeGenConfig {
val finalModuleName = moduleName?.replaceFirstChar { it.uppercase(Locale.US) } ?: ""
val defaultPackageName = if (finalModuleName.isEmpty()) {
DEFAULT_GEN_PACKAGE_NAME
} else {
"$DEFAULT_GEN_PACKAGE_NAME.${finalModuleName.lowercase()}".sanitizePackageName()
}
val codeGenBasePackageName = packageName?.sanitizePackageName() ?: defaultPackageName
val registryId = finalModuleName.ifEmpty { UUID.randomUUID().toString().replace("-", "_") }

return CodeGenConfig(
packageName = codeGenBasePackageName,
moduleName = finalModuleName,
registryId = registryId,
generateNavGraphs = generateNavGraphs,
htmlMermaidGraph = htmlMermaidGraph,
mermaidGraph = mermaidGraph,
debugModeOutputDir = debugModeOutputDir,
isBottomSheetDependencyPresent = isBottomSheetDependencyPresent
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.ramcosta.composedestinations.codegen.writers.sub.SingleNavGraphWriter

internal interface ServiceLocator {
val codeGenerator: CodeOutputStreamMaker
val isBottomSheetDependencyPresent: Boolean
val codeGenConfig: CodeGenConfig
}

Expand Down Expand Up @@ -60,7 +59,6 @@ internal fun ServiceLocator.destinationsWriter(
) = DestinationsWriter(
codeGenConfig,
codeGenerator,
isBottomSheetDependencyPresent,
customNavTypeByType,
submodules,
)
Expand All @@ -78,8 +76,7 @@ internal fun ServiceLocator.navGraphsSingleObjectWriter(
)

internal val ServiceLocator.initialValidator get() = InitialValidator(
codeGenConfig,
isBottomSheetDependencyPresent
codeGenConfig
)

internal val ServiceLocator.defaultKtxSerializableNavTypeSerializerWriter get() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.ramcosta.composedestinations.codegen.model.Visibility

internal class InitialValidator(
private val codeGenConfig: CodeGenConfig,
private val isBottomSheetDependencyPresent: Boolean
) {

fun validate(
Expand Down Expand Up @@ -162,7 +161,7 @@ internal class InitialValidator(

private fun DestinationGeneratingParams.validateReceiverColumnScope() {
if (composableReceiverSimpleName == COLUMN_SCOPE_SIMPLE_NAME) {
if (!isBottomSheetDependencyPresent) {
if (!codeGenConfig.isBottomSheetDependencyPresent) {
throw MissingRequiredDependency(
"'${annotatedName}' composable: " +
"You need to include $BOTTOM_SHEET_DEPENDENCY dependency to use a $COLUMN_SCOPE_SIMPLE_NAME receiver!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.ramcosta.composedestinations.codegen.writers.helpers.NavArgResolver
internal class DestinationsWriter(
private val codeGenConfig: CodeGenConfig,
private val codeGenerator: CodeOutputStreamMaker,
private val isBottomSheetDependencyPresent: Boolean,
private val customNavTypeByType: Map<Type, CustomNavType>,
private val submodules: List<SubModuleInfo>,
) {
Expand All @@ -26,7 +25,6 @@ internal class DestinationsWriter(
SingleDestinationWriter(
codeGenConfig,
codeGenerator,
isBottomSheetDependencyPresent,
NavArgResolver(customNavTypeByType, importableHelper),
destination,
importableHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.ramcosta.composedestinations.codegen.model.Type
import com.ramcosta.composedestinations.codegen.model.TypeArgument
import com.ramcosta.composedestinations.codegen.model.TypeInfo
import com.ramcosta.composedestinations.codegen.model.Visibility
import com.ramcosta.composedestinations.codegen.moduleName
import com.ramcosta.composedestinations.codegen.registryId
import com.ramcosta.composedestinations.codegen.templates.core.setOfImportable
import com.ramcosta.composedestinations.codegen.writers.helpers.ImportableHelper
Expand Down Expand Up @@ -75,7 +76,7 @@ internal class ModuleRegistryWriter(
)
@_Info_$registryId(
moduleName = "${codeGenConfig.moduleName ?: ""}",
moduleName = "$moduleName",
packageName = "$codeGenBasePackageName",
hasNavArgsPackage = ${"$codeGenBasePackageName.navargs" in codeGenerator.packageNamesWrittenTo},
typeResults = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ val destinationsPackageName = "$codeGenBasePackageName.destinations"
internal class SingleDestinationWriter(
private val codeGenConfig: CodeGenConfig,
private val codeGenerator: CodeOutputStreamMaker,
private val isBottomSheetDependencyPresent: Boolean,
private val navArgResolver: NavArgResolver,
private val destination: CodeGenProcessedDestination,
private val importableHelper: ImportableHelper,
Expand Down Expand Up @@ -275,7 +274,7 @@ internal class SingleDestinationWriter(
}

private fun destinationStyleBottomSheet(): String {
if (!isBottomSheetDependencyPresent) {
if (!codeGenConfig.isBottomSheetDependencyPresent) {
throw MissingRequiredDependency("You need to include '$BOTTOM_SHEET_DEPENDENCY' to use $CORE_BOTTOM_SHEET_DESTINATION_STYLE!")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.ramcosta.composedestinations.codegen.facades.Logger
import com.ramcosta.composedestinations.codegen.facades.PPrinter
import com.ramcosta.composedestinations.codegen.model.CodeGenConfig
import io.exoquery.fansi.Attrs
import java.util.Locale

class KspLogger(
private val codeGenConfig: CodeGenConfig,
Expand All @@ -13,7 +14,7 @@ class KspLogger(

override val debugMode: Boolean = codeGenConfig.debugModeOutputDir != null
override val debugModeOutputPath: String? = codeGenConfig.debugModeOutputDir?.let {
"$it/compose-destinations-debug.txt"
"$it/composeDestinationsDebug/${codeGenConfig.registryId.lowercase(Locale.ROOT)}.txt"
}

private val _prettyPrinter: PPrinter = object: PPrinter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package com.ramcosta.composedestinations.ksp.processors

import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.Resolver
import com.ramcosta.composedestinations.codegen.commons.CORE_BOTTOM_SHEET_DESTINATION_STYLE
import com.ramcosta.composedestinations.codegen.commons.CORE_PACKAGE_NAME
import com.ramcosta.composedestinations.codegen.model.CodeGenConfig

class ConfigParser(
Expand All @@ -21,7 +25,7 @@ class ConfigParser(
private const val HTML_MERMAID_GRAPH = "$PREFIX.htmlMermaidGraph"
}

fun parse(): CodeGenConfig {
fun parse(resolver: Resolver): CodeGenConfig {
val packageName = options[GEN_PACKAGE_NAME]?.trim()?.removeSuffix(".")
val moduleName = options[MODULE_NAME]?.trim()?.filter { it.isLetter() }
val htmlMermaidGraph = options[HTML_MERMAID_GRAPH]?.trim()
Expand All @@ -34,6 +38,7 @@ class ConfigParser(
packageName = packageName,
mermaidGraph = mermaidGraph,
htmlMermaidGraph = htmlMermaidGraph,
isBottomSheetDependencyPresent = resolver.isBottomSheetDepPresent()
)
}

Expand All @@ -44,6 +49,10 @@ class ConfigParser(
throw WrongConfigurationSetup("$key must be a boolean value!", cause = it)
}
}

private fun Resolver.isBottomSheetDepPresent(): Boolean {
return getClassDeclarationByName("$CORE_PACKAGE_NAME.bottomsheet.spec.$CORE_BOTTOM_SHEET_DESTINATION_STYLE") != null
}
}

class WrongConfigurationSetup(message: String, cause: Throwable? = null) :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ramcosta.composedestinations.ksp.processors

import com.google.devtools.ksp.KspExperimental
import com.google.devtools.ksp.getClassDeclarationByName
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
Expand All @@ -13,7 +12,6 @@ import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.Modifier
import com.ramcosta.composedestinations.codegen.CodeGenerator
import com.ramcosta.composedestinations.codegen.commons.ACTIVITY_DESTINATION_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.CORE_BOTTOM_SHEET_DESTINATION_STYLE
import com.ramcosta.composedestinations.codegen.commons.CORE_PACKAGE_NAME
import com.ramcosta.composedestinations.codegen.commons.DESTINATION_ANNOTATION_QUALIFIED
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
Expand Down Expand Up @@ -41,7 +39,7 @@ class Processor(
) : SymbolProcessor {

override fun process(resolver: Resolver): List<KSAnnotated> {
val codeGenConfig = ConfigParser(options).parse()
val codeGenConfig = ConfigParser(options).parse(resolver)
Logger.instance = KspLogger(codeGenConfig, logger)

val composableDestinations = resolver.getComposableDestinationPaths()
Expand All @@ -59,11 +57,6 @@ class Processor(

val mutableKSFileSourceMapper = MutableKSFileSourceMapper()
val kspCodeOutputStreamMaker = KspCodeOutputStreamMaker(codeGenerator, mutableKSFileSourceMapper)
val codeGenerator = CodeGenerator(
codeGenerator = kspCodeOutputStreamMaker,
isBottomSheetDependencyPresent = resolver.isBottomSheetDepPresent(),
codeGenConfig = codeGenConfig
)

val navTypeSerializers = resolver.getNavTypeSerializers()
val destinationMappingUtils = DestinationMappingUtils(resolver)
Expand All @@ -83,7 +76,10 @@ class Processor(
)
val destinations = functionsToDestinationsMapper.map(composableDestinations.map { it.immutable() }, activityDestinations)

codeGenerator.generate(
CodeGenerator(
codeGenerator = kspCodeOutputStreamMaker,
codeGenConfig = codeGenConfig
).generate(
destinations,
navGraphs,
navTypeSerializers,
Expand Down Expand Up @@ -245,10 +241,6 @@ class Processor(
)
}.toList()
}

private fun Resolver.isBottomSheetDepPresent(): Boolean {
return getClassDeclarationByName("$CORE_PACKAGE_NAME.bottomsheet.spec.$CORE_BOTTOM_SHEET_DESTINATION_STYLE") != null
}
}

typealias KSPClassKind = com.google.devtools.ksp.symbol.ClassKind
Expand Down
2 changes: 1 addition & 1 deletion playground/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ android {

// Possible Compose Destinations configs:
ksp {
//arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.debugMode", "$rootDir")
// // Module name.
// // It will be used as the generated sealed Destinations prefix
// arg("compose-destinations.moduleName", "featureX")
Expand Down
2 changes: 1 addition & 1 deletion playground/featurex/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
}

ksp {
//arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.moduleName", "featureX")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/playground/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/playground/docs")
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion playground/featurey/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
}

ksp {
//arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.moduleName", "featureY")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/playground/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/playground/docs")
Expand Down
2 changes: 1 addition & 1 deletion playground/featurey/sub/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
}

ksp {
//arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.debugMode", "$rootDir")
arg("compose-destinations.moduleName", "subFeatureY")
arg("compose-destinations.htmlMermaidGraph", "$rootDir/playground/docs")
arg("compose-destinations.mermaidGraph", "$rootDir/playground/docs")
Expand Down
Loading

0 comments on commit b137b95

Please sign in to comment.