-
Notifications
You must be signed in to change notification settings - Fork 531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #3972: Added TextViewStyleCheck script #5599
base: develop
Are you sure you want to change the base?
Changes from 16 commits
e243e20
00f7367
25e31c0
0e7240c
8afffd9
0cda3ff
ecb097e
b3247dd
284759b
50155ab
781d682
d66f9aa
8b1f90f
b5155bd
0631ec7
f104b96
8bd90bf
9e530aa
051ab79
0510d7d
f24b7f6
ec4c12d
67ab290
afcb708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,173 @@ | ||||||||||||
package org.oppia.android.scripts.xml | ||||||||||||
|
||||||||||||
import org.w3c.dom.Element | ||||||||||||
import java.io.File | ||||||||||||
import javax.xml.parsers.DocumentBuilderFactory | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Script to ensure all TextView elements in layout XML files use centrally managed styles. | ||||||||||||
* | ||||||||||||
* Usage: | ||||||||||||
* bazel run //scripts:check_textview_styles -- <path_to_repository_root> | ||||||||||||
* | ||||||||||||
* Arguments: | ||||||||||||
* - path_to_repository_root: The root path of the repository. | ||||||||||||
* | ||||||||||||
* Example: | ||||||||||||
* bazel run //scripts:check_textview_styles -- $(pwd) | ||||||||||||
*/ | ||||||||||||
fun main(vararg args: String) { | ||||||||||||
require(args.isNotEmpty()) { | ||||||||||||
"Usage: bazel run //scripts:check_textview_styles -- <path_to_repository_root>" | ||||||||||||
} | ||||||||||||
|
||||||||||||
val repoRoot = File(args[0]) | ||||||||||||
require(repoRoot.exists()) { "Repository root path does not exist: ${args[0]}" } | ||||||||||||
|
||||||||||||
val resDir = File(repoRoot, "app/src/main/res") | ||||||||||||
require(resDir.exists()) { "Resource directory does not exist: ${resDir.path}" } | ||||||||||||
|
||||||||||||
val xmlFiles = resDir.listFiles { file -> file.isDirectory && file.name.startsWith("layout") } | ||||||||||||
?.flatMap { dir -> dir.walkTopDown().filter { it.extension == "xml" } } | ||||||||||||
?: emptyList() | ||||||||||||
|
||||||||||||
val styleChecker = TextViewStyleCheck(repoRoot) | ||||||||||||
styleChecker.checkFiles(xmlFiles) | ||||||||||||
} | ||||||||||||
|
||||||||||||
private class TextViewStyleCheck(private val repoRoot: File) { | ||||||||||||
private val errors = mutableListOf<String>() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - val |
||||||||||||
private val legacyDirectionalityWarnings = mutableListOf<String>() | ||||||||||||
private val builderFactory = DocumentBuilderFactory.newInstance() | ||||||||||||
private val styles: Map<String, Element> by lazy { loadStyles() } | ||||||||||||
|
||||||||||||
private fun loadStyles(): Map<String, Element> { | ||||||||||||
val stylesFile = File(repoRoot, "app/src/main/res/values/styles.xml") | ||||||||||||
require(stylesFile.exists()) { "Styles file does not exist: ${stylesFile.path}" } | ||||||||||||
|
||||||||||||
val document = builderFactory.newDocumentBuilder().parse(stylesFile) | ||||||||||||
val styleNodes = document.getElementsByTagName("style") | ||||||||||||
return (0 until styleNodes.length).associate { i -> | ||||||||||||
val element = styleNodes.item(i) as Element | ||||||||||||
element.getAttribute("name") to element | ||||||||||||
} | ||||||||||||
} | ||||||||||||
/** Checks XML files for TextView elements to ensure compliance with style requirements. */ | ||||||||||||
fun checkFiles(xmlFiles: List<File>) { | ||||||||||||
xmlFiles.forEach { file -> processXmlFile(file) } | ||||||||||||
printResults() | ||||||||||||
} | ||||||||||||
|
||||||||||||
private fun processXmlFile(file: File) { | ||||||||||||
val document = builderFactory.newDocumentBuilder().parse(file) | ||||||||||||
val textViewNodes = document.getElementsByTagName("TextView") | ||||||||||||
|
||||||||||||
for (i in 0 until textViewNodes.length) { | ||||||||||||
val element = textViewNodes.item(i) as Element | ||||||||||||
validateTextViewElement(element, file.path) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
private fun validateTextViewElement(element: Element, filePath: String) { | ||||||||||||
val styleAttribute = element.attributes.getNamedItem("style")?.nodeValue | ||||||||||||
val idAttribute = element.attributes.getNamedItem("android:id")?.nodeValue ?: "No ID" | ||||||||||||
|
||||||||||||
if (!isExemptFromStyleRequirement(element)) { | ||||||||||||
if (styleAttribute?.startsWith("@style/") == true) { | ||||||||||||
validateStyle(styleAttribute, idAttribute, filePath) | ||||||||||||
} else { | ||||||||||||
errors.add("$filePath: TextView ($idAttribute) requires central style.") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think including line numbers in error and warning logs could be helpful, especially when views are missing IDs. Something like:
Ref: #4128 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing this out! I’ve addressed this in the recent changes by using the |
||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
checkForLegacyDirectionality(element, filePath) | ||||||||||||
} | ||||||||||||
// Validate if the referenced style exists and contains necessary RTL/LTR properties. | ||||||||||||
private fun validateStyle(styleAttribute: String, idAttribute: String, filePath: String) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While the check for the existence of the referenced style is helpful, I wonder if it's possible to avoid redundant RTL/LTR settings checks, as they seem to repeat the same element validations multiple times. |
||||||||||||
val styleName = styleAttribute.removePrefix("@style/") | ||||||||||||
val styleElement = styles[styleName] ?: run { | ||||||||||||
errors.add("$filePath: TextView ($idAttribute) references non-existent style: $styleName") | ||||||||||||
return | ||||||||||||
} | ||||||||||||
|
||||||||||||
val items = styleElement.getElementsByTagName("item") | ||||||||||||
val hasRtlProperties = (0 until items.length).any { i -> | ||||||||||||
val item = items.item(i) as Element | ||||||||||||
when (item.getAttribute("name")) { | ||||||||||||
"android:textAlignment", | ||||||||||||
"android:gravity", | ||||||||||||
"android:layoutDirection", | ||||||||||||
"android:textDirection", | ||||||||||||
"android:textSize" -> true | ||||||||||||
else -> false | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (!hasRtlProperties) { | ||||||||||||
errors.add( | ||||||||||||
"$filePath: TextView ($idAttribute) style '$styleName' lacks RTL/LTR properties" | ||||||||||||
) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
// Determines if a TextView is exempt from requiring a centrally managed style. | ||||||||||||
private fun isExemptFromStyleRequirement(element: Element): Boolean { | ||||||||||||
if (element.getAttribute("android:gravity")?.contains("center") == true) return true | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows TextViews with @adhiamboperes, can you clarify if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the clarification, @Rd4dev and @adhiamboperes. Here’s the finalized implementation and explanation, as I understand it, for handling the 1. Direction-Neutrality
2. Considerations for
|
||||||||||||
if (hasDynamicVisibility(element)) return true | ||||||||||||
if (element.hasAttribute("android:textSize")) return true | ||||||||||||
Comment on lines
+192
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not entirely clear on the criteria for the exemptions. I saw that |
||||||||||||
|
||||||||||||
return !hasDirectionalAttributes(element) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? The elements with the |
||||||||||||
} | ||||||||||||
|
||||||||||||
private fun hasDynamicVisibility(element: Element) = | ||||||||||||
element.getAttribute("android:visibility").let { it.contains("{") && it.contains("}") } | ||||||||||||
|
||||||||||||
private fun hasDirectionalAttributes(element: Element): Boolean { | ||||||||||||
val directionAttributes = listOf( | ||||||||||||
"android:layout_alignParentStart", | ||||||||||||
"android:layout_alignParentEnd", | ||||||||||||
"android:layout_toStartOf", | ||||||||||||
"android:layout_toEndOf", | ||||||||||||
"android:paddingStart", | ||||||||||||
"android:paddingEnd", | ||||||||||||
"android:layout_marginStart", | ||||||||||||
"android:layout_marginEnd" | ||||||||||||
) | ||||||||||||
return directionAttributes.any { element.hasAttribute(it) } | ||||||||||||
} | ||||||||||||
|
||||||||||||
private fun checkForLegacyDirectionality(element: Element, filePath: String) { | ||||||||||||
val legacyAttributes = listOf( | ||||||||||||
"android:paddingLeft", | ||||||||||||
"android:paddingRight", | ||||||||||||
"android:layout_marginLeft", | ||||||||||||
"android:layout_marginRight", | ||||||||||||
"android:layout_alignParentLeft", | ||||||||||||
"android:layout_alignParentRight", | ||||||||||||
"android:layout_toLeftOf", | ||||||||||||
"android:layout_toRightOf" | ||||||||||||
) | ||||||||||||
|
||||||||||||
val foundLegacyAttributes = legacyAttributes.filter { element.hasAttribute(it) } | ||||||||||||
if (foundLegacyAttributes.isNotEmpty()) { | ||||||||||||
legacyDirectionalityWarnings.add( | ||||||||||||
"$filePath: TextView uses legacy" + | ||||||||||||
" directional attributes: ${foundLegacyAttributes.joinToString(", ")}" | ||||||||||||
) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
private fun printResults() { | ||||||||||||
if (legacyDirectionalityWarnings.isNotEmpty()) { | ||||||||||||
println("\nWarnings - Legacy directionality attributes found:") | ||||||||||||
legacyDirectionalityWarnings.forEach { println(it) } | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (errors.isNotEmpty()) { | ||||||||||||
println("\nTextView Style Check FAILED:") | ||||||||||||
errors.forEach { println(it) } | ||||||||||||
throw Exception("Some TextView elements do not have centrally managed styles.") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - For consistency in formatting and proper signaling of failures, capitalize the exception message and use ref: oppia-android/scripts/src/java/org/oppia/android/scripts/xml/StringResourceValidationCheck.kt Lines 49 to 53 in 17f2ef3
|
||||||||||||
} else { | ||||||||||||
println("\nTextView Style Check PASSED.") | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this script should be included as part of the CI checks.