Skip to content

Commit

Permalink
Fixes #648
Browse files Browse the repository at this point in the history
Also:
* Removed module name prefix from NavGraphs object fields
  • Loading branch information
raamcosta committed May 31, 2024
1 parent 39f3150 commit 734a75a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,4 @@ internal fun sourceIds(
}

return sourceIds
}

internal fun navGraphFieldName(navGraphRoute: String): String {
val regex = "[^a-zA-Z]".toRegex()
val auxNavGraphRoute = navGraphRoute.toCharArray().toMutableList()
var weirdCharIndex = auxNavGraphRoute.indexOfFirst { it.toString().matches(regex) }

while (weirdCharIndex != -1) {
auxNavGraphRoute.removeAt(weirdCharIndex)
if (weirdCharIndex >= auxNavGraphRoute.size) {
break
}
auxNavGraphRoute[weirdCharIndex] = auxNavGraphRoute[weirdCharIndex].uppercaseChar()

weirdCharIndex = auxNavGraphRoute.indexOfFirst { it.toString().matches(regex) }
}

return String(auxNavGraphRoute.toCharArray())
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ data class RawNavGraphGenParams(
}

override val baseRoute: String by lazy(LazyThreadSafetyMode.NONE) {
routeOverride ?: nameWithModuleName()
.replace("(?i)navgraph".toRegex(), "")
.replace("(?i)graph".toRegex(), "")
.toSnakeCase()
routeOverride ?: nameWithModuleName().prepareForRouteFormat()
}

val baseRouteWithNoModulePrefix: String by lazy(LazyThreadSafetyMode.NONE) {
routeOverride ?: name.prepareForRouteFormat()
}

private fun String.prepareForRouteFormat() = this
.replace("(?i)navgraph".toRegex(), "")
.replace("(?i)graph".toRegex(), "")
.toSnakeCase()

private fun nameWithModuleName(): String {
val moduleNamePrefix = moduleName.takeIf { it.isNotBlank() }?.let { "${it.toSnakeCase()}/" } ?: ""
return "$moduleNamePrefix${name.replaceFirstChar { it.lowercase(Locale.US) }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.ramcosta.composedestinations.codegen.codeGenBasePackageName
import com.ramcosta.composedestinations.codegen.commons.GENERATED_NAV_GRAPHS_OBJECT
import com.ramcosta.composedestinations.codegen.commons.IllegalDestinationsSetup
import com.ramcosta.composedestinations.codegen.commons.RawNavGraphTree
import com.ramcosta.composedestinations.codegen.commons.navGraphFieldName
import com.ramcosta.composedestinations.codegen.commons.plusAssign
import com.ramcosta.composedestinations.codegen.commons.sourceIds
import com.ramcosta.composedestinations.codegen.facades.CodeOutputStreamMaker
Expand Down Expand Up @@ -105,18 +104,25 @@ internal class NavGraphsSingleObjectWriter(
}

private fun checkUniquenessOnNavGraphFieldNames(navGraphsParams: List<RawNavGraphTree>) {
val nonUniqueFieldNames = navGraphsParams.groupBy { navGraphFieldName(it.rawNavGraphGenParams.baseRoute) }
val nonUniqueRoutesToAnnotationName: List<Pair<String, String>> = navGraphsParams.groupBy { it.navGraphFieldName() }
.filter {
it.value.size > 1
}.flatMap {
it.value
}.map {
it.rawNavGraphGenParams.annotationType.simpleName
}.flatMap { mapEntry ->
mapEntry.value.map { mapEntry.key to it }
}.map { (route, graph) ->
route to graph.rawNavGraphGenParams.annotationType.simpleName
}

if (nonUniqueRoutesToAnnotationName.isNotEmpty()) {
if (nonUniqueRoutesToAnnotationName.any { it.first == "main" }) {
throw IllegalDestinationsSetup(
"NavGraphs ${nonUniqueRoutesToAnnotationName.map { it.second }} result in the same field for the NavGraphs " +
"final object. You cannot have a Graph annotation called `${nonUniqueRoutesToAnnotationName.map { it.second }.firstOrNull { it.startsWith("main", ignoreCase = true) } ?: "MainGraph"}` and also have one with the same name as the module name."
)
}

if (nonUniqueFieldNames.isNotEmpty()) {
throw IllegalDestinationsSetup(
"NavGraphs $nonUniqueFieldNames result in the same field for the NavGraphs " +
"NavGraphs ${nonUniqueRoutesToAnnotationName.map { it.second }} result in the same field for the NavGraphs " +
"final object. Use only letters in your NavGraph annotations!"
)
}
Expand All @@ -128,7 +134,7 @@ internal class NavGraphsSingleObjectWriter(
val requireOptInAnnotationsAnchor = "[REQUIRE_OPT_IN_ANNOTATIONS_ANCHOR]"

return """
| ${requireOptInAnnotationsAnchor}val ${navGraphFieldName(navGraph.rawNavGraphGenParams.baseRoute)} = ${navGraph.rawNavGraphGenParams.name}
| ${requireOptInAnnotationsAnchor}val ${navGraph.navGraphFieldName()} = ${navGraph.rawNavGraphGenParams.name}
""".trimMargin()
.replace(
requireOptInAnnotationsAnchor,
Expand All @@ -146,4 +152,29 @@ internal class NavGraphsSingleObjectWriter(

return code.toString()
}

private fun RawNavGraphTree.navGraphFieldName(): String {
val navGraphRoute = rawNavGraphGenParams.baseRouteWithNoModulePrefix
val regex = "[^a-zA-Z]".toRegex()
val auxNavGraphRoute = navGraphRoute.toCharArray().toMutableList()
var weirdCharIndex = auxNavGraphRoute.indexOfFirst { it.toString().matches(regex) }

while (weirdCharIndex != -1) {
auxNavGraphRoute.removeAt(weirdCharIndex)
if (weirdCharIndex >= auxNavGraphRoute.size) {
break
}
auxNavGraphRoute[weirdCharIndex] = auxNavGraphRoute[weirdCharIndex].uppercaseChar()

weirdCharIndex = auxNavGraphRoute.indexOfFirst { it.toString().matches(regex) }
}

val fieldName = String(auxNavGraphRoute.toCharArray())
return if (fieldName.equals(moduleName, ignoreCase = true)) {
// to avoid things like "LoginNavGraphs.login" and have instead "LoginNavGraphs.main"
"main"
} else {
fieldName
}
}
}

0 comments on commit 734a75a

Please sign in to comment.