Skip to content

Commit

Permalink
Check region qualifier stands after language
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakok committed Dec 19, 2023
1 parent 78fddc8 commit f526329
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,20 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo
val languageRegex = Regex("[a-z][a-z]")
val regionRegex = Regex("r[A-Z][A-Z]")

var alreadyHasLanguage: String? = null
var alreadyHasRegion: String? = null
var alreadyHasTheme: String? = null
var alreadyHasDensity: String? = null
val qualifiersMap = mutableMapOf<ClassName, String>()

fun repetitiveQualifiers(first: String, second: String) {
error("${resourceItem.path} contains repetitive qualifiers: $first and $second")
fun saveQualifier(className: ClassName, qualifier: String) {
qualifiersMap[className]?.let {
error("${resourceItem.path} contains repetitive qualifiers: $it and $qualifier")
}
qualifiersMap[className] = qualifier
}

resourceItem.qualifiers.sorted().forEach { q ->
resourceItem.qualifiers.forEach { q ->
when (q) {
"light",
"dark" -> {
alreadyHasTheme?.let { repetitiveQualifiers(it, q) }
alreadyHasTheme = q
add("%T.${q.uppercase()}, ", themeQualifier)
saveQualifier(themeQualifier, q)
}

"mdpi",
Expand All @@ -65,28 +63,37 @@ private fun CodeBlock.Builder.addQualifiers(resourceItem: ResourceItem): CodeBlo
"xxhdpi",
"xxxhdpi",
"ldpi" -> {
alreadyHasDensity?.let { repetitiveQualifiers(it, q) }
alreadyHasDensity = q
add("%T.${q.uppercase()}, ", densityQualifier)
saveQualifier(densityQualifier, q)
}

else -> when {
q.matches(languageRegex) -> {
alreadyHasLanguage?.let { repetitiveQualifiers(it, q) }
alreadyHasLanguage = q
add("%T(\"$q\"), ", languageQualifier)
saveQualifier(languageQualifier, q)
}

q.matches(regionRegex) -> {
alreadyHasRegion?.let { repetitiveQualifiers(it, q) }
alreadyHasRegion = q
add("%T(\"${q.takeLast(2)}\"), ", regionQualifier)
saveQualifier(regionQualifier, q)
}

else -> error("${resourceItem.path} contains unknown qualifier: $q")
}
}
}
qualifiersMap[themeQualifier]?.let { q -> add("%T.${q.uppercase()}, ", themeQualifier) }
qualifiersMap[densityQualifier]?.let { q -> add("%T.${q.uppercase()}, ", densityQualifier) }
qualifiersMap[languageQualifier]?.let { q -> add("%T(\"$q\"), ", languageQualifier) }
qualifiersMap[regionQualifier]?.let { q ->
val lang = qualifiersMap[languageQualifier]
if (lang == null) {
error("Region qualifier must be used only with language")
}
val langAndRegion = "$lang-$q"
if(!resourceItem.path.toString().contains(langAndRegion)) {
error("Region qualifier must be declared after language: '$langAndRegion'")
}
add("%T(\"${q.takeLast(2)}\"), ", regionQualifier)
}

return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ class ResourcesTest : GradlePluginTestBase() {
}

file("src/commonMain/resources/composeRes/drawable-ren").renameTo(
file("src/commonMain/resources/composeRes/drawable-rUS-en")
)

gradle("generateComposeResClass").checks {
check.logContains("""
Region qualifier must be declared after language: 'en-rUS'
""".trimIndent())
}

file("src/commonMain/resources/composeRes/drawable-rUS-en").renameTo(
file("src/commonMain/resources/composeRes/drawable-rUS")
)

gradle("generateComposeResClass").checks {
check.logContains("""
Region qualifier must be used only with language
""".trimIndent())
}

file("src/commonMain/resources/composeRes/drawable-rUS").renameTo(
file("src/commonMain/resources/composeRes/drawable-en-fr")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ internal object Res {
public val vector: DrawableResource = DrawableResource(
"drawable:vector",
setOf(
ResourceItem(
setOf(LanguageQualifier("au"), RegionQualifier("US"), ),
"composeRes/drawable-au-rUS/vector.xml"
),
ResourceItem(
setOf(ThemeQualifier.DARK, LanguageQualifier("ge"), ),
"composeRes/drawable-dark-ge/vector.xml"
Expand All @@ -31,10 +35,6 @@ internal object Res {
setOf(LanguageQualifier("en"), ),
"composeRes/drawable-en/vector.xml"
),
ResourceItem(
setOf(RegionQualifier("US"), ),
"composeRes/drawable-rUS/vector.xml"
),
ResourceItem(
setOf(),
"composeRes/drawable/vector.xml"
Expand Down

0 comments on commit f526329

Please sign in to comment.