Skip to content

Commit

Permalink
refs #89 - added task to check messageBundle for duplicate entries an…
Browse files Browse the repository at this point in the history
…d that all entries are used
  • Loading branch information
johnscancella committed Apr 27, 2017
1 parent e2e2a70 commit 63479fa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
56 changes: 55 additions & 1 deletion code-quality.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,58 @@ dependencyCheck {
// xml.enabled = false
// html.enabled = true
// }
//}
//}

import java.util.Map.Entry;
task checkMessageBundle(){
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!")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static void checkNormalization(final String path, final Path rootDir, fi
final String normalizedFile = normalizePathToNFD(file);

if(!file.equals(fileToCheck) && normalizedFileToCheck.equals(normalizedFile)){
logger.warn(messages.getString("different_normalization_warning"), fileToCheck);
logger.warn(messages.getString("different_normalization_in_manifest_warning"), fileToCheck);
warnings.add(BagitWarning.DIFFERENT_NORMALIZATION);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void run() {

if(!fileExists){
if(existsNormalized){
logger.warn(messages.getString("different_normalization_warning"), file);
logger.warn(messages.getString("different_normalization_on_filesystem_warning"), file);
}
else{
missingFiles.add(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public static void writeBagitFile(final Version version, final Charset encoding,
final Path bagitPath = outputDir.resolve("bagit.txt");
logger.debug(messages.getString("write_bagit_file_to_path"), outputDir);


final String firstLine = "BagIt-Version: " + version + System.lineSeparator();
logger.debug(messages.getString("writing_line_to_file"), firstLine, bagitPath);
Files.write(bagitPath, firstLine.getBytes(StandardCharsets.UTF_8),
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/MessageBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ different_case_warning=In manifest [{}], path [{}] is the same as another path e
manifest_line_violated_spec_error=Manifest contains line [{}] which does not follow the specified form of <CHECKSUM> <PATH>
md5sum_generated_line_warning=Path [{}] starts with a *, which means it was generated with a non-bagit tool. It is recommended to remove the * in order to conform to the bagit specification.
cannot_access_parent_path_error=Could not access parent folder of [{}].
different_normalization_warning=File [{}] has a different normalization then what is specified in the manifest.
different_normalization_in_manifest_warning=File [{}] has a different normalization then what is specified in the manifest.
bag_within_bag_warning=We stronger recommend not storing a bag within a bag as it is known to cause problems.
leading_dot_slash_warning=In manifest [{}] line [{}] is a non-normalized path.
os_specific_files_warning=In manifest [{}] line [{}] contains a OS specific file.
Expand Down Expand Up @@ -145,7 +145,7 @@ checksums_not_matching_error=[{}] errors occured. At least one of the errors is
checking_bag_is_complete=Checking if the bag with root directory [{}] is complete.

#for CheckIfFileExistsTask.java
different_normalization_warning=File name [{}] has a different normalization than what is contained on the filesystem!
different_normalization_on_filesystem_warning=File name [{}] has a different normalization than what is contained on the filesystem!
error_reading_normalized_file=Error while trying to read [{}] to see if any files in that directory match the normalized filename of [{}]!

#for CheckManifestHashesTask.java
Expand Down

0 comments on commit 63479fa

Please sign in to comment.