forked from spacecowboy/NotePad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomTasks.gradle
64 lines (52 loc) · 2.48 KB
/
customTasks.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// custom tasks for gradle
// run with: ./gradlew checkLanguages
task checkLanguages {
// TODO Feeder has a better version of this, but you need to change the locales code. See:
// https://gitlab.com/spacecowboy/Feeder/-/blob/master/app/build.gradle.kts#L344
group 'NoNonsenseNotes custom'
description 'Ensure all languages in res/values-*/strings.xml are selectable in the spinner'
var readFromFolders = () -> {
// Read res/values/ folders, add their codes to localesInFolders
File resDir = new File(projectDir, "app/src/main/res")
FileFilter theFilter = (file) -> file.name.toLowerCase().startsWith("values")
File[] dirs = resDir.listFiles(theFilter)
var localesInFolders = new ArrayList<String>()
for (File valueFolder : dirs) {
// exclude folders like 'values-600dp' which don't represent languages
if (valueFolder.name.contains("0dp")) continue
if (valueFolder.name.contains("values-land")) continue
String localeCode = valueFolder.name.replace("values-", "")
localesInFolders.add(localeCode)
}
return localesInFolders
}
var readFromXML = () -> {
// read the language list in arrays.xml
File arrFile = new File(projectDir, "app/src/main/res/values/arrays.xml")
var reader = new FileReader(arrFile.absolutePath)
String xmlText = reader.text
reader.close()
// locate where the "translated_langs" array is
int posBegin = xmlText.indexOf("<string-array name=\"translated_langs\" tools:ignore=\"MissingTranslation\">")
int posEnd = xmlText.indexOf("</string-array>", posBegin)
String onlyLangArray = xmlText.substring(posBegin, posEnd)
// add locale codes to localesInXML
var localesInXML = new ArrayList<String>()
for (String line : onlyLangArray.split('\n')) {
if (!line.contains("<item>")) continue
String localeCode = line.replace("<item>", "").replace("</item>", "")
localesInXML.add(localeCode)
}
return localesInXML
}
doLast {
// actually run the task
var listDir = readFromFolders()
var listXML = readFromXML()
if (listDir.size != listXML.size) {
String errMsg = "Outdated translation: XML element has " + listXML.size
errMsg += " locales, but the values-* folders are " + listDir.size
throw new GradleException(errMsg)
}
}
}