-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
customTasks.gradle
111 lines (90 loc) · 4.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// custom tasks for gradle
// run with: ./gradlew checkLanguages
tasks.register('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) -> {
return file.name.toLowerCase().startsWith("values")
&& file.name.toLowerCase() != "values-v26"
}
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 listDirSize = readFromFolders().size()
var listXMLSize = readFromXML().size()
if (listDirSize != listXMLSize) {
String errMsg = "Outdated translation: XML element has " + listXMLSize
errMsg += " locales, but the values-* folders are " + listDirSize
throw new GradleException(errMsg)
}
}
}
// run with: ./gradlew checkFastlane
tasks.register('checkFastlane') {
// in fastlane/metadata/android there are folders for each language supported
// by F-Droid and the Play Store. Each of these folders MUST have (at least)
// 3 files: full_description.txt, short_description.txt, title.txt
// Otherwise, fastlane will crash and we can't publish the update to the app
// stores. This function checks if the 'fastlane/' sub-directories are
// configured correctly
group 'NoNonsenseNotes custom'
description 'Ensure all languages in fastlane/metadata/android folders are configured correctly'
doLast {
// folder containing all languages sub-directories
File flDir = new File(projectDir, "fastlane/metadata/android")
for (File langSubDir : flDir.listFiles()) {
// check that each sub-folder has the 3 mandatory files
var onlyTxtFilesFilter = new FilenameFilter() {
@Override
boolean accept(File dir, String fname) {
return fname.endsWithIgnoreCase('.txt')
}
}
String[] requiredFileNames = [ 'full_description.txt', 'short_description.txt', 'title.txt' ]
String[] presentFileName = langSubDir.list(onlyTxtFilesFilter)
for (String fn : requiredFileNames) {
if (!presentFileName.contains(fn)) {
// 1 of the 3 required files is missing => fail the task
String errMsg = "Missing file " + fn + " in folder " + langSubDir.name + " for fastlane"
throw new GradleException(errMsg)
}
}
}
// finished: we have confirmed that all the folders have the 3 required .txt files,
// and now fastlane can publish without issues
}
}