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 #3772: Add ci check for style check for text views #4128

7 changes: 7 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ kt_jvm_binary(
runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:xml_syntax_check_lib"],
)

kt_jvm_binary(
name = "text_view_style_check",
testonly = True,
main_class = "org.oppia.android.scripts.xml.TextViewStyleCheckKt",
runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/xml:text_view_style_check_lib"],
)

TEST_FILE_EXEMPTION_ASSETS = generate_test_file_assets_list_from_text_protos(
name = "test_file_exemption_assets",
test_file_exemptions_name = ["test_file_exemptions"],
Expand Down
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 @@ -22,3 +22,14 @@ kt_jvm_library(
"//scripts/src/java/org/oppia/android/scripts/common:repository_file",
],
)

kt_jvm_library(
name = "text_view_style_check_lib",
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,46 @@
package org.oppia.android.scripts.xml

import org.oppia.android.scripts.common.RepositoryFile
import org.w3c.dom.Node
import javax.xml.parsers.DocumentBuilderFactory

fun main(vararg args: String) {
// Path of the repo to be analyzed.
val repoPath = "${args[0]}/"

// A list of all XML files in the repo to be analyzed.
val searchFiles = RepositoryFile.collectSearchFiles(
repoPath = repoPath, expectedExtension = ".xml"
)
val builderFactory = DocumentBuilderFactory.newInstance()
searchFiles.forEach { file ->
val builder = builderFactory.newDocumentBuilder()
val doc = builder.parse(file)
val textViewsElements = doc.getElementsByTagName("TextView")
for (i in 0 until textViewsElements.length) {
val node = textViewsElements.item(i)
textViewRTL(node as Node)
}
}
}

private fun textViewRTL(node: Node) {
val attributes = node.attributes
for (i in 0 until attributes.length) {
val attribute = attributes.item(i)
when (attribute.nodeName) {
"android:layout_marginLeft" -> {
throw Exception("TextView has layout_marginLeft")
}
"android:layout_marginRight" -> {
throw Exception("TextView has layout_marginRight")
}
"android:paddingLeft" -> {
throw Exception("TextView has paddingLeft")
}
"android:paddingRight" -> {
throw Exception("TextView has paddingRight")
}
}
}
}