Skip to content
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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,10 @@ java_binary(
main_class = "org.oppia.android.scripts.telemetry.DecodeUserStudyEventStringKt",
runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/telemetry:decode_user_study_event_string_lib"],
)

kt_jvm_binary(
name = "check_textview_styles",
testonly = True,
main_class = "org.oppia.android.scripts.xml.TextViewStyleCheckKt",
runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:check_textview_styles"],
)
11 changes: 11 additions & 0 deletions scripts/src/java/org/oppia/android/scripts/xml/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ kt_jvm_library(
"//scripts/src/java/org/oppia/android/scripts/common:repository_file",
],
)

kt_jvm_library(
name = "check_textview_styles",
testonly = True,
srcs = ["TextViewStyleCheck.kt"],
visibility = ["//scripts:oppia_script_binary_visibility"],
deps = [
":xml_syntax_error_handler",
"//scripts/src/java/org/oppia/android/scripts/common:repository_file",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.oppia.android.scripts.xml

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 layoutDirs = resDir.listFiles { file -> file.isDirectory && file.name.startsWith("layout") }
?: emptyArray()
val xmlFiles = layoutDirs.flatMap { dir ->
dir.walkTopDown().filter { file -> file.extension == "xml" }.toList()
}

val builderFactory = DocumentBuilderFactory.newInstance()
val errors = mutableListOf<String>()

for (file in xmlFiles) {
val document = builderFactory.newDocumentBuilder().parse(file)
val textViewNodes = document.getElementsByTagName("TextView")

for (i in 0 until textViewNodes.length) {
val node = textViewNodes.item(i)
val attributes = node.attributes
val styleAttribute = attributes?.getNamedItem("style")?.nodeValue

if (styleAttribute == null || !styleAttribute.startsWith("@style/")) {
errors.add("${file.path}: TextView element is missing a centrally managed style.")
break
}
}
}

if (errors.isNotEmpty()) {
println("TextView Style Check FAILED:")
errors.forEach { println(it) }
throw Exception("Some TextView elements do not have centrally managed styles.")
} else {
println("TextView Style Check PASSED.")
}
}
Loading