1
0
Fork 0

YAML language definitions (#292)

This commit is contained in:
Dimo Karaivanov 2023-06-26 15:14:22 +03:00 committed by GitHub
parent 241a4125b0
commit 6756de4466
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
89 changed files with 689 additions and 522 deletions

View file

@ -2,17 +2,26 @@ buildscript {
repositories {
mavenCentral()
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:8.0.2'
classpath 'gradle.plugin.at.zierler:yaml-validator-plugin:1.5.0'
}
}
apply plugin: 'com.android.application'
apply plugin: 'at.zierler.yamlvalidator'
configurations.all {
// fixes 'duplicate class error', when using these combine: androidx.core:1.10.1, androidx.preference:1.2.0 and androidx.room:2.5.1
// see: https://stackoverflow.com/questions/75274720/a-failure-occurred-while-executing-appcheckdebugduplicateclasses/75315276#75315276
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
yamlValidator {
searchPaths = ['assets/languages/definitions']
}
}
dependencies {
@ -20,11 +29,15 @@ dependencies {
implementation 'androidx.preference:preference:1.2.0'
implementation 'androidx.room:room-runtime:2.5.1'
annotationProcessor 'androidx.room:room-compiler:2.5.1'
implementation 'org.yaml:snakeyaml:2.0'
}
repositories {
mavenCentral()
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
def execThing ( String cmdStr ) {
@ -86,9 +99,13 @@ def getReleaseVersion = { ->
return "${getVersionName()} (${getCurrentGitHash()})"
}
task validateDictionaries {
inputs.dir fileTree(dir:'assets', excludes:['dict.properties'])
outputs.file "${project.buildDir}/dict.validation.txt"
task validateLanguages {
final baseDir = "${project.rootDir}/assets/languages"
final definitionsDir = "${baseDir}/definitions"
final dictionariesDir = "${baseDir}/dictionaries"
inputs.dir fileTree(dir:baseDir, excludes:['dict.properties'])
outputs.file "${project.buildDir}/lang.validation.txt"
doLast {
final String CSV_DELIMITER = ' ' // TAB
@ -100,19 +117,86 @@ task validateDictionaries {
outputs.files.singleFile.text = ""
inputs.getFiles().each {File file ->
fileTree(definitionsDir).getFiles().each { File languageFile ->
if (errorCount >= MAX_ERRORS) {
return
}
println "Validating dictionary: " + file.name
println "Validating language: ${languageFile.name}"
def uniqueWords = [:]
int lineNumber = 0
boolean isFileValid = true
file.eachLine {line ->
boolean hasLayout = false
boolean isLocaleValid = false
def localeString = ''
def dictionaryFileName = ''
languageFile.eachLine { line ->
if (
line.matches("^[a-zA-Z].*")
&& !line.startsWith("abcString")
&& !line.startsWith("dictionaryFile")
&& !line.startsWith("hasUpperCase")
&& !line.startsWith("layout")
&& !line.startsWith("locale")
&& !line.startsWith("name")
) {
isFileValid = false
def parts = line.split(":")
def property = parts.length > 0 ? parts[0] : line
errorCount++
errors += "Language '${languageFile.name}' is invalid. Found unknown property: '${property}'.\n"
}
if (line.startsWith("hasUpperCase") && !line.endsWith("yes") && !line.endsWith("no")) {
def invalidVal = line.replace("hasUpperCase:", "").trim()
isFileValid = false
errorCount++
errors += "Language '${languageFile.name}' is invalid. Unrecognized 'hasUpperCase' value: '${invalidVal}'. Only 'yes' and 'no' are allowed.\n"
}
if (line.startsWith("layout")) {
hasLayout = true
}
if (line.startsWith("locale")) {
localeString = line.replace("locale:", "").trim()
isLocaleValid = line.matches("^locale:\\s*[a-z]{2}(?:-[A-Z]{2})?")
}
if (line.startsWith("dictionaryFile")) {
dictionaryFileName = line.replace("dictionaryFile:", "").trim()
}
}
if (!hasLayout) {
isFileValid = false
errorCount++
errors += "Language '${languageFile.name}' is invalid. Missing 'layout' property.\n"
}
if (!isLocaleValid) {
isFileValid = false
errorCount++
def msg = localeString.isEmpty() ? "Missing 'locale' property." : "Unrecognized locale format: '${localeString}'"
errors += "Language '${languageFile.name}' is invalid. ${msg}\n"
}
def dictionaryFile = new File("$dictionariesDir/${dictionaryFileName}")
if (dictionaryFileName.isEmpty() || !dictionaryFile.exists()) {
errorCount++
errors += "Could not find dictionary file: '${dictionaryFileName}' in: '${dictionariesDir}'. Make sure 'dictionaryFile' is set correctly in: '${languageFile.name}'.\n"
outputs.files.singleFile.text += "${languageFile.name} INVALID \n"
return
}
def uniqueWords = [:]
int lineNumber = 0
dictionaryFile.eachLine {line ->
if (errorCount >= MAX_ERRORS) {
return
}
@ -122,14 +206,14 @@ task validateDictionaries {
if (line == "") {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. There is no word on line " + lineNumber + ". Remove all empty lines.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. There is no word on line ${lineNumber}. Remove all empty lines.\n"
return
}
if (line.contains(" ")) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found space on line " + lineNumber + ". Make sure each word is on a new line. Phrases are not allowed.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found space on line ${lineNumber}. Make sure each word is on a new line. Phrases are not allowed.\n"
return
}
@ -140,32 +224,32 @@ task validateDictionaries {
if (frequency.length() > 0 && !frequency.matches("^\\d+\$")) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found out-of-range word frequency: '" + frequency + "' on line " + lineNumber + ". Frequency must be a non-negative integer.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found out-of-range word frequency: '${frequency}' on line ${lineNumber}. Frequency must be a non-negative integer.\n"
}
if (word.matches("(\\d.+?|.+?\\d|\\d)")) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found numbers on line " + lineNumber + ". Remove all numbers.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found numbers on line ${lineNumber}. Remove all numbers.\n"
}
if (word.matches("^\\P{L}+\$")) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found a garbage word: '" + word + "' on line " + lineNumber + ".\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found a garbage word: '${word}' on line ${lineNumber}.\n"
}
if (word.matches("^.\$") && !Character.isUpperCase(word.charAt(0))) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found a single letter: '" + word + "' on line " + lineNumber + ". Only uppercase single letters are allowed. The rest of the alphabet will be added automatically.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found a single letter: '${word}' on line ${lineNumber}. Only uppercase single letters are allowed. The rest of the alphabet will be added automatically.\n"
}
String uniqueWordKey = word ==~ GEOGRAPHICAL_NAME ? word : word.toLowerCase()
if (uniqueWords[uniqueWordKey] != null && uniqueWords[uniqueWordKey] == true) {
isFileValid = false
errorCount++
errors += "Dictionary '" + file.name + "' is invalid. Found a repeating word: '" + word + "' on line " + lineNumber + ". Ensure all words appear only once.\n"
errors += "Dictionary '${dictionaryFile.name}' is invalid. Found a repeating word: '${word}' on line ${lineNumber}. Ensure all words appear only once.\n"
} else {
uniqueWords[uniqueWordKey] = true
}
@ -175,7 +259,7 @@ task validateDictionaries {
}
}
outputs.files.singleFile.text += file.name + " " + (isFileValid ? "OK" : "INVALID") + "\n"
outputs.files.singleFile.text += "${languageFile.name} ${isFileValid ? 'OK' : 'INVALID'}\n"
}
if (errors != "") {
@ -239,7 +323,10 @@ android {
aidl true
}
applicationVariants.all { variant ->
tasks["merge${variant.name.capitalize()}Assets"].dependsOn(validateDictionaries)
tasks["merge${variant.name.capitalize()}Assets"]
.dependsOn(validateYaml)
.dependsOn(validateLanguages)
}
}