diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2021-03-20 14:44:07 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2021-03-20 14:58:18 +0530 |
commit | a87060140f3a6661d898b302f9ef7e136edb6847 (patch) | |
tree | fff35058081a4389624978c8d37c625fb3bd1fc0 /buildSrc | |
parent | a8216feadcf0251ff7c9c8ba40c82a98a06e3a73 (diff) |
buildSrc: add removeIncompleteStrings step to Crowdin plugin
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'buildSrc')
-rw-r--r-- | buildSrc/src/main/java/CrowdinDownloadPlugin.kt | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/buildSrc/src/main/java/CrowdinDownloadPlugin.kt b/buildSrc/src/main/java/CrowdinDownloadPlugin.kt index 6ec6e33a..7a0f9d95 100644 --- a/buildSrc/src/main/java/CrowdinDownloadPlugin.kt +++ b/buildSrc/src/main/java/CrowdinDownloadPlugin.kt @@ -5,12 +5,14 @@ import de.undercouch.gradle.tasks.download.Download import java.io.File +import javax.xml.parsers.DocumentBuilderFactory import org.gradle.api.GradleException import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.Copy import org.gradle.kotlin.dsl.create import org.gradle.kotlin.dsl.register +import org.w3c.dom.Document private const val EXCEPTION_MESSAGE = """Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension.""" @@ -40,6 +42,30 @@ class CrowdinDownloadPlugin : Plugin<Project> { setDependsOn(setOf("extractCrowdin")) from("$buildDir/translations/") into("${projectDir}/src/") + setFinalizedBy(setOf("removeIncompleteStrings")) + } + tasks.register("removeIncompleteStrings") { + doLast { + val sourceSets = arrayOf("main", "nonFree") + for (sourceSet in sourceSets) { + val stringFiles = File("${projectDir}/src/$sourceSet").walkTopDown().filter { it.name == "strings.xml" } + val sourceFile = + stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") } + ?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet") + val sourceDoc = parseDocument(sourceFile) + val baselineStringCount = countStrings(sourceDoc) + val threshold = 0.80 * baselineStringCount + stringFiles.forEach { file -> + if (file != sourceFile) { + val doc = parseDocument(file) + val stringCount = countStrings(doc) + if (stringCount < threshold) { + file.delete() + } + } + } + } + } } tasks.register("crowdin") { setDependsOn(setOf("extractStrings")) @@ -54,4 +80,17 @@ class CrowdinDownloadPlugin : Plugin<Project> { } } } + + private fun parseDocument(file: File): Document { + val dbFactory = DocumentBuilderFactory.newInstance() + val documentBuilder = dbFactory.newDocumentBuilder() + return documentBuilder.parse(file) + } + + private fun countStrings(document: Document): Int { + // Normalization is beneficial for us + // https://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work + document.documentElement.normalize() + return document.getElementsByTagName("string").length + } } |