-
Notifications
You must be signed in to change notification settings - Fork 7
/
message-bundle.gradle
54 lines (46 loc) · 1.87 KB
/
message-bundle.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
//this build file is responsible for all the tasks related to internationalization messages
import java.util.Map.Entry;
task checkMessageBundle(){
description "Checks the message bundles(which are used for language translation) that all entries are used, " +
"and that there are no duplicates."
group "Verification"
inputs.files(fileTree(dir: "src/main/resources", include: "**/MessageBundle*.properties"))
outputs.dir("$buildDir/checkMessageBundle") //hack: define a output dir so gradle will check if up-to-date
doLast{
inputs.getFiles().each{File file ->
Set<String> messageKeys = new HashSet<>()
file.eachLine {String line ->
if(line && !line.trim().startsWith('#')){
String[] keyValue = checkMessageBundleLineIsCorrectlyFormatted(line, file)
if(messageKeys.contains(keyValue[0])){
throw new GradleException("Internationalization message bundle contains duplicate key [${keyValue[0]}]!")
}
messageKeys.add(keyValue[0])
}
}
checkAllMessageKeysAreUsed(messageKeys)
}
}
}
check.dependsOn checkMessageBundle
String[] checkMessageBundleLineIsCorrectlyFormatted(String line, File file){
String[] keyValue = line.split("=", 2)
if(keyValue.size() != 2 || keyValue[1].isEmpty()){
throw new GradleException("Line [${line}] in file [${file}] is not a valid entry for the internationalization message bundle!")
}
return keyValue
}
void checkAllMessageKeysAreUsed(Set<String> messageKeys){
sourceSets.main.allJava.each { File file ->
file.eachLine{ String line ->
for(String key : messageKeys.clone()){
if(line.contains(key)){
messageKeys.remove(key)
}
}
}
}
if(messageKeys.size() > 0){
throw new GradleException("[${messageKeys}] is listed in the internationalization message bundle but never actually used!")
}
}