2024-04-19 04:31:09 +00:00
|
|
|
/**
|
|
|
|
|
* This script checks if the field constants in the Java files are named and defined correctly.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-14 01:00:40 +00:00
|
|
|
import {promises as fs} from "fs";
|
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
|
|
async function findInnerClassDefinitions(directory) {
|
2024-04-19 04:31:09 +00:00
|
|
|
let isPass = true;
|
|
|
|
|
|
2024-03-14 01:00:40 +00:00
|
|
|
try {
|
|
|
|
|
const files = await fs.readdir(directory);
|
|
|
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
|
const filePath = path.join(directory, file);
|
|
|
|
|
const stats = await fs.stat(filePath);
|
|
|
|
|
|
|
|
|
|
if (stats.isDirectory()) {
|
2024-04-19 04:31:09 +00:00
|
|
|
if (!await findInnerClassDefinitions(filePath)) {
|
|
|
|
|
isPass = false;
|
|
|
|
|
}
|
|
|
|
|
} else if (path.extname(filePath) === ".java") {
|
|
|
|
|
if (!await processJavaFile(filePath)) {
|
|
|
|
|
isPass = false;
|
|
|
|
|
}
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
2024-04-19 04:31:09 +00:00
|
|
|
isPass = false;
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
2024-04-19 04:31:09 +00:00
|
|
|
|
|
|
|
|
return isPass;
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function processJavaFile(filePath) {
|
2024-04-19 04:31:09 +00:00
|
|
|
let isPass = true;
|
|
|
|
|
|
2024-03-14 01:00:40 +00:00
|
|
|
try {
|
2024-04-19 04:31:09 +00:00
|
|
|
const contents = await fs.readFile(filePath, "utf8");
|
2024-03-14 01:00:40 +00:00
|
|
|
|
|
|
|
|
const innerClassRegex = /^ {4}([\w ]+?)\s+class\s+Fields\s+(extends (\w+)\.Fields)?\s*{(.+?\n {4})?}$/gsm;
|
|
|
|
|
|
|
|
|
|
for (const innerClassMatch of contents.matchAll(innerClassRegex)) {
|
|
|
|
|
const classQualifiers = innerClassMatch[1]; // we don't care much about this
|
|
|
|
|
const expectedParentClass = innerClassMatch[3];
|
|
|
|
|
|
|
|
|
|
for (const match of innerClassMatch[0].matchAll(/\bpublic\s+static\s+final\s+String\s+(\w+)\s+=\s+(.+?);/gs)) {
|
2024-04-19 04:31:09 +00:00
|
|
|
const key = match[1];
|
2024-03-14 01:00:40 +00:00
|
|
|
const valMatcherParts = [`^dotted\\(`];
|
|
|
|
|
for (const [i, field] of key.split("_").entries()) {
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
valMatcherParts.push(`\\s*,\\s+`);
|
|
|
|
|
}
|
|
|
|
|
valMatcherParts.push(`(\\w+\\.\\w+\\.)?${field}`);
|
|
|
|
|
}
|
|
|
|
|
valMatcherParts.push(`\\s*\\)$`);
|
2024-04-19 04:31:09 +00:00
|
|
|
const valMatcher = new RegExp(valMatcherParts.join(""));
|
2024-03-14 01:00:40 +00:00
|
|
|
if (!valMatcher.test(match[2])) {
|
2024-04-19 04:31:09 +00:00
|
|
|
console.log(filePath, classQualifiers, expectedParentClass);
|
|
|
|
|
console.log("\tkey is", key);
|
|
|
|
|
console.log("\tval is", match[2]);
|
|
|
|
|
console.log("\tpattern", valMatcher);
|
|
|
|
|
console.error(`\tField ${key} in ${filePath} is not looking right.`);
|
|
|
|
|
isPass = false;
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (finds.length > 1) {
|
|
|
|
|
// console.error(`Found multiple inner class definitions in file: ${filePath}`);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
2024-04-19 04:31:09 +00:00
|
|
|
isPass = false;
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
2024-04-19 04:31:09 +00:00
|
|
|
|
|
|
|
|
return isPass;
|
2024-03-14 01:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-04-19 04:31:09 +00:00
|
|
|
// Can't use `import.meta.dirname` because it's not available in Node.js 18.
|
|
|
|
|
// And v18 is what is included in GitHub Actions today.
|
|
|
|
|
// See <https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2204-Readme.md#language-and-runtime>.
|
|
|
|
|
const directoryPath = import.meta.resolve("..").replace("file://", "");
|
|
|
|
|
|
|
|
|
|
findInnerClassDefinitions(directoryPath)
|
|
|
|
|
.then(isPass => {
|
|
|
|
|
if (isPass) {
|
|
|
|
|
console.log("All okay.");
|
|
|
|
|
} else {
|
|
|
|
|
console.error("Some field constants are not looking good.");
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
}
|
|
|
|
|
});
|