Skip to content

Commit

Permalink
moved message bundle tasks to separate gradle build file
Browse files Browse the repository at this point in the history
  • Loading branch information
johnscancella committed May 16, 2017
1 parent e5e1abc commit 2a401dd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 58 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
apply from: 'eclipse.gradle'
apply from: 'maven-central.gradle'
apply from: 'code-quality.gradle'
apply from: 'message-bundle.gradle'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
59 changes: 1 addition & 58 deletions code-quality.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,61 +97,4 @@ dependencyCheck {
// xml.enabled = false
// html.enabled = true
// }
//}

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{
Set<String> messageKeys = new HashSet<>()

inputs.getFiles().each{File file ->
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){
messageKeys.each{String key ->
throw new GradleException("[${key}] is listed in the internationalization message bundle but never actually used!")
}
}
}
//}
58 changes: 58 additions & 0 deletions message-bundle.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//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{
Set<String> messageKeys = new HashSet<>()

inputs.getFiles().each{File file ->
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){
messageKeys.each{String key ->
throw new GradleException("[${key}] is listed in the internationalization message bundle but never actually used!")
}
}
}

0 comments on commit 2a401dd

Please sign in to comment.