Files
MQL5Data/compileEA.js
T

128 lines
3.5 KiB
JavaScript

//
// Import Requirements ...
const execSync = require("child_process").execSync;
const XFileTools = require("./Documents/JsModules/x-file.tools");
const XShellTools = require("./Documents/JsModules/x-shell.tools");
const XColorTools = require("./Documents/JsModules/x-color.tools");
//
handleCompile();
async function handleCompile() {
//
var foldersPath = [
"Libraries", // Required Libraries
"Classes", // Required Classes
"Indicators", // Required Indicators
"Helpers", // Indicator's Helpers Classes
"Parsers", // Strategies Filters based on Conditions
"Strategies", // Strategies Classes which Provides Signals
"Experts", // Expert Advisers
"XCATBEA", // XCATBEA Projects ...
];
//
for (const path of foldersPath) {
//
const fSourcePath = XFileTools.joinPath(".", path);
const filesToCompile = await XFileTools.extractFiles(fSourcePath, [".mq5", ".mq4"]);
for (const f of filesToCompile) {
//
// Log ...
console.log(
XColorTools.applyForegroundColor("try to Compile: ", XColorTools.COLOR_NAMES.Yellow),
XColorTools.applyForegroundColor(f, XColorTools.COLOR_NAMES.Blue)
);
//
await recursiveTriesToCompileFile(f, 0);
}
}
}
/**
* try to compile a file ...
* @param {string} f
* @param {number} numberOfTries
*/
async function recursiveTriesToCompileFile(f = "", numberOfTries = 0) {
//
let result = await handleFileCompile(f);
if (result) {
//
const msg = "compiled successfully ...";
console.log(XColorTools.applyForegroundColor(msg, XColorTools.COLOR_NAMES.Green));
} else {
//
if (numberOfTries < 10) {
//
numberOfTries++;
//
const msg = "retry (" + (numberOfTries + 1) + ") ...";
console.log(XColorTools.applyForegroundColor(msg, XColorTools.COLOR_NAMES.LightMagenta));
await recursiveTriesToCompileFile(f, numberOfTries);
} else {
//
const msg = "failed ...";
console.log(XColorTools.applyForegroundColor(msg, XColorTools.COLOR_NAMES.Red));
throw msg;
}
}
}
/**
* execute compile task on Specific File ...
* @param {string} f file path to Execute Compile task ...
* @returns
*/
async function handleFileCompile(f = "") {
//
let result = false;
//
let exResult = await compileMQLFile(f);
if (exResult.includes("0 errors")) {
result = true;
} else {
result = false;
}
//
return result;
}
/**
* Compile Specific MQL File ...
*
* @param {string} path MQL file path ...
* @returns
*/
async function compileMQLFile(path = "") {
//
// Check File Exists ...
const isExists = XFileTools.isFileExists(path);
if (!isExists) {
//
const msg = "file not found ...";
console.log(XColorTools.applyForegroundColor(msg, XColorTools.COLOR_NAMES.Red));
throw msg;
}
//
const ext = XFileTools.getFileExtension(path);
if (ext !== ".mq5" && ext !== "mq4") {
//
const msg = "invalid file extension ...";
console.log(XColorTools.applyForegroundColor(msg, XColorTools.COLOR_NAMES.Red));
throw msg;
}
//
const cwd = __dirname;
const cmd = "compile.cmd " + path;
//
const result = await XShellTools.execute(cmd, cwd);
return result;
}