initi.doc/templates/initi/search_inheritance_core.js

172 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-12-10 08:04:43 +00:00
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 12/6/18.
*/
var fs = require("fs");
var path = require('jsdoc/path');
var rx_match_modelType = /ModelType {1,}?([A-Za-z]{1,}?)\(@(.*)?\)/i;
var __tree_list = Object.create(null);
var __root;
var map = {
"mObject": ""
};
var destination;
var ignore = {
".git": true
};
var avail_format = {
"xxc": true
};
var find = function (_conf) {
__root = new Node();
destination = path.normalize(_conf.source.core[0]);
recursive(destination);
generate_tree();
return __root.get_struct();
};
var recursive = function(_path){
if(fs.lstatSync(_path).isDirectory()) {
// console.log("DIRECTORY: ",_path);
var arr = fs.readdirSync(_path);
for (var a = 0; a < arr.length; a++) {
var name = arr[a];
if (!ignore[name]) {
recursive(_path + "/" + name);
}
}
} else {
console.log("FILE: ", _path);
var extenstion = _path.split(".").pop();
if (avail_format[extenstion]) {
parse_file(_path);
}
}
};
var parse_file = function(_path){
let info = Object.create(null);
info.requires = Object.create(null);
info.classes = Object.create(null);
var file = fs.readFileSync(_path, "utf8");
var classes = exec_all(rx_match_modelType, file);
if(classes.length > 0) {
for(var a = 0; a < classes.length; a++){
map[classes[a].subs[0]] = classes[a].subs[1];
}
}
};
var exec_all = function(_rx, _string){
var out = [];
var result;
while(result = _string.match(_rx)){
result = convert_result(result);
_string = _string.substring(result.index + result.match.length, _string.length);
out.push(result);
}
return out
};
var convert_result = function(_exec_result) {
var info = Object.create(null);
info.index = _exec_result.index;
info.match = _exec_result[0];
info.subs = [];
for (var a = 1; a < _exec_result.length; a++) {
info.subs.push(_exec_result[a]);
}
return info;
};
var generate_tree = function() {
__root.name = "BaseClass";
for (var type in map) {
var inherit = map[type];
recursive_tree(type, inherit);
}
};
var recursive_tree = function (_type_name, _inherit, _child) {
if(_type_name === "mObject"){
console.log("create mObject");
var n = new Node();
n.name = _type_name;
n.inherit = __root.name;
__root.children.push(n);
__tree_list[_type_name] = n;
return;
}
if(_type_name !== "mObject" && !_type_name){
console.log("ERROR: TYPE %s, INHERIT: %s, ChildName: %s", _type_name, _inherit, _child.name);
process.exit(1);
} else {
console.log("CLASS: %s, INHERIT: %s", _type_name, _inherit);
}
var node = __tree_list[_type_name];
if(!node){
node = new Node();
node.name = _type_name;
__tree_list[_type_name] = node;
if(_child && node.children.indexOf(_child) === -1){
_child.inherit = _type_name;
node.children.push(_child);
}
}
var parent = __tree_list[_inherit];
if(parent){
if(parent.children.indexOf(node) === -1) {
node.inherit = _inherit;
parent.children.push(node);
}
} else {
var inh = map[_inherit];
if(inh) {
recursive_tree(_inherit, inh, node);
} else {
if(__root.children.indexOf(node) === -1) {
__root.children.push(node);
node.inherit = __root.name;
}
}
}
};
var Node = function(){
this.children = [];
this.name = "";
this.inherit = "base_class";
};
Node.prototype = {
get_struct: function () {
var children = [];
for(var a = 0; a < this.children.length; a++){
children.push(this.children[a].get_struct());
}
return {
name: this.name,
children: children
}
}
};
module.exports.find = find;