|
|
@ -3,16 +3,61 @@ const path = require("path"); |
|
|
|
|
|
|
|
|
|
|
|
// Extract emmy lua classes from a file
|
|
|
|
// Extract emmy lua classes from a file
|
|
|
|
// "---@class Bastion" -> "Bastion"
|
|
|
|
// "---@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) { |
|
|
|
function extractClasses(file) { |
|
|
|
const classes = []; |
|
|
|
const classes = []; |
|
|
|
const lines = file.split("\n"); |
|
|
|
const lines = file.split("\n"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let className = ""; |
|
|
|
|
|
|
|
let variables = []; |
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < lines.length; i++) { |
|
|
|
for (let i = 0; i < lines.length; i++) { |
|
|
|
const line = lines[i]; |
|
|
|
const line = lines[i]; |
|
|
|
|
|
|
|
|
|
|
|
if (line.startsWith("---@class ")) { |
|
|
|
if (line.startsWith("---@class ")) { |
|
|
|
const className = line.replace("---@class ", ""); |
|
|
|
// We have reached the start of a class documentation
|
|
|
|
classes.push(className); |
|
|
|
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; |
|
|
|
return classes; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -109,6 +154,7 @@ function extractFunctions(file) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let globalFunctions = []; |
|
|
|
let globalFunctions = []; |
|
|
|
|
|
|
|
let globalClasses = []; |
|
|
|
|
|
|
|
|
|
|
|
// dump a file to markdown
|
|
|
|
// dump a file to markdown
|
|
|
|
function Dump(filePath) { |
|
|
|
function Dump(filePath) { |
|
|
@ -118,21 +164,32 @@ function Dump(filePath) { |
|
|
|
globalFunctions = [...globalFunctions, ...functions]; |
|
|
|
globalFunctions = [...globalFunctions, ...functions]; |
|
|
|
|
|
|
|
|
|
|
|
const classes = extractClasses(fs.readFileSync(filePath, "utf8")).map( |
|
|
|
const classes = extractClasses(fs.readFileSync(filePath, "utf8")).map( |
|
|
|
(className) => { |
|
|
|
(classData) => { |
|
|
|
|
|
|
|
const classFunctions = functions.filter( |
|
|
|
|
|
|
|
(func) => func.className === classData.name |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
name: className, |
|
|
|
...classData, |
|
|
|
functions: functions.filter((func) => func.className === className), |
|
|
|
functions: classFunctions, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
globalClasses = [...globalClasses, ...classes]; |
|
|
|
|
|
|
|
|
|
|
|
// console.log(classes);
|
|
|
|
// console.log(classes);
|
|
|
|
|
|
|
|
|
|
|
|
// write the data to disk as a markdown file
|
|
|
|
// write the data to disk as a markdown file
|
|
|
|
const markdown = classes |
|
|
|
const markdown = classes |
|
|
|
.map((classData) => { |
|
|
|
.map((classData) => { |
|
|
|
const classMarkdown = `# ${classData.name} |
|
|
|
const classMarkdown = `# ${classData.name} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
${classData.variables ? "## Variables" : ""} |
|
|
|
|
|
|
|
${classData.variables |
|
|
|
|
|
|
|
.map((variable) => { |
|
|
|
|
|
|
|
return `\`${variable.key} = ${variable.value}\``; |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.join("\n")} |
|
|
|
|
|
|
|
|
|
|
|
${classData.functions |
|
|
|
${classData.functions |
|
|
|
.map((func) => { |
|
|
|
.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);
|
|
|
|
// console.log(_funcs);
|
|
|
|
const lua = _funcs |
|
|
|
const lua = _funcs |
|
|
|
.map((func) => { |
|
|
|
.map((func) => { |
|
|
|
let str = ""; |
|
|
|
let str = ""; |
|
|
|
|
|
|
|
|
|
|
|
if (func.description !== "") { |
|
|
|
if (func.description && func.description !== "") { |
|
|
|
str += `--- ${func.description}\n`; |
|
|
|
str += `--- ${func.description}\n`; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -219,7 +296,9 @@ function DumpAPIFile(_funcs) { |
|
|
|
}) |
|
|
|
}) |
|
|
|
.join("\n\n"); |
|
|
|
.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
|
|
|
|
// Wipe the output directory
|
|
|
@ -227,4 +306,4 @@ fs.rmdirSync(path.join(__dirname, "output"), { recursive: true }); |
|
|
|
fs.mkdirSync(path.join(__dirname, "output")); |
|
|
|
fs.mkdirSync(path.join(__dirname, "output")); |
|
|
|
|
|
|
|
|
|
|
|
DumpDirectory(path.join(__dirname, "input")); |
|
|
|
DumpDirectory(path.join(__dirname, "input")); |
|
|
|
DumpAPIFile(globalFunctions); |
|
|
|
DumpAPIFile(globalClasses, globalFunctions); |
|
|
|