Add better class extraction

main
4n0n 2 years ago
parent 95f2b6b499
commit f8582af362
  1. 97
      index.js
  2. 0
      output/.gitkeep

@ -3,16 +3,61 @@ const path = require("path");
// Extract emmy lua classes from a file
// "---@class Bastion" -> "Bastion"
/*
"---@class Bastion" -> "Bastion"
Bastion = {
Test = function(self, name, name2)
return 5
end,
isTest = true,
}
-> ["Bastion", variables: [{key: 'Test', value: 'function(self, name, name2)'} ,
{key: 'isTest', value: 'true'}]
*/
function extractClasses(file) {
const classes = [];
const lines = file.split("\n");
let className = "";
let variables = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith("---@class ")) {
const className = line.replace("---@class ", "");
classes.push(className);
// We have reached the start of a class documentation
className = line.replace("---@class ", "");
} else if (line.startsWith("}")) {
if (className != "") {
// console.log("end of class", className, "variables", variables);
classes.push({
name: className,
variables,
});
className = "";
variables = [];
}
// if the line contains both { and } then the table is empty and we can just add it to the classes array
} else if (className != "" && line.includes("{") && line.includes("}")) {
classes.push({
name: className,
variables: [],
});
className = "";
variables = [];
} else if (className != "" && !line.includes("{")) {
// if the line contains a = then it is a variable declaration
if (line.includes("=")) {
const [key, value] = line.split("=");
variables.push({
key: key.trim(),
value: value.trim(),
});
}
}
}
// console.log(classes);
return classes;
}
@ -109,6 +154,7 @@ function extractFunctions(file) {
}
let globalFunctions = [];
let globalClasses = [];
// dump a file to markdown
function Dump(filePath) {
@ -118,13 +164,18 @@ function Dump(filePath) {
globalFunctions = [...globalFunctions, ...functions];
const classes = extractClasses(fs.readFileSync(filePath, "utf8")).map(
(className) => {
(classData) => {
const classFunctions = functions.filter(
(func) => func.className === classData.name
);
return {
name: className,
functions: functions.filter((func) => func.className === className),
...classData,
functions: classFunctions,
};
}
);
globalClasses = [...globalClasses, ...classes];
// console.log(classes);
@ -133,6 +184,12 @@ function Dump(filePath) {
.map((classData) => {
const classMarkdown = `# ${classData.name}
${classData.variables ? "## Variables" : ""}
${classData.variables
.map((variable) => {
return `\`${variable.key} = ${variable.value}\``;
})
.join("\n")}
${classData.functions
.map((func) => {
@ -195,13 +252,33 @@ function DumpDirectory(directory) {
});
}
function DumpAPIFile(_funcs) {
function DumpAPIFile(_classes, _funcs) {
// console.log(_classes);
const luaClasses = _classes.map((classData) => {
let str = "";
if (classData.description && classData.description !== "") {
str += `--- ${classData.description}\n`;
}
str += `---@class ${classData.name}\n`;
if (classData.variables.length > 0) {
classData.variables.map((variable) => {
str += `---@field ${variable.key} ${variable.value}\n`;
});
}
return str;
});
// console.log(_funcs);
const lua = _funcs
.map((func) => {
let str = "";
if (func.description !== "") {
if (func.description && func.description !== "") {
str += `--- ${func.description}\n`;
}
@ -219,7 +296,9 @@ function DumpAPIFile(_funcs) {
})
.join("\n\n");
fs.writeFileSync(path.join(__dirname, "output", "API.lua"), lua);
let output = [...luaClasses, lua].join("\n\n");
fs.writeFileSync(path.join(__dirname, "output", "API.lua"), output);
}
// Wipe the output directory
@ -227,4 +306,4 @@ fs.rmdirSync(path.join(__dirname, "output"), { recursive: true });
fs.mkdirSync(path.join(__dirname, "output"));
DumpDirectory(path.join(__dirname, "input"));
DumpAPIFile(globalFunctions);
DumpAPIFile(globalClasses, globalFunctions);

Loading…
Cancel
Save