initi.doc/templates/initi/search_inheritance2.js
2018-12-10 11:04:43 +03:00

194 lines
4.7 KiB
JavaScript

/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 12/6/18.
*/
var fs = require("fs");
var path = require('jsdoc/path');
var rx_class_declarator_std = /var {1,}?(.*?) {1,}?= {1,}?(.*?)\.inherit\(\{/i;
var rx_requires = /var {1,}?(.*?) {1,}?= {1,}?require\("(.*?)"\)/i;
var __tree_list = Object.create(null);
var __root;
var map = {};
var destination;
var find = function (_conf) {
__root = new Node();
// console.log("start find");
destination = path.normalize(_conf.source.include_3[0]);
// console.log("=================");
// console.log("===== START =====");
// console.log("=================");
// console.log("=================");
// console.log("====== END ======");
// console.log("=================");
recursive(destination);
// console.log("=================================================");
// console.log("var map = JSON.parse('%s');", JSON.stringify(map));
// console.log("=================================================");
generate_tree();
// console.log("=================================================");
// console.log("JSON.parse('%s');", JSON.stringify(__tree_list));
// console.log("=================================================");
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];
recursive(_path + "/" + name);
}
} else {
// console.log("FILE: ", _path);
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 requires = exec_all(rx_requires, file);
if(requires.length > 0) {
for(var a = 0; a < requires.length; a++){
info.requires[requires[a].subs[0]] = update_link_file(requires[a].subs[1])
}
}
var classes = exec_all(rx_class_declarator_std, file);
if(classes.length > 0) {
for(var a = 0; a < classes.length; a++){
info.classes[classes[a].subs[0]] = classes[a].subs[1]
}
}
map[_path] = info;
};
var exec_all = function(_rx, _string){
var out = [];
var result;
while(result = _string.match(_rx)){
result = convert_result(result);
// console.log("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 update_link_file = function (_path) {
var arr = destination.split("/");
// arr.pop();
return arr.join("/") + "/" + _path + ".js"
};
var generate_tree = function() {
__root.name = "BaseClass";
for (var path in map) {
var file_info = map[path];
recursive_tree(path, file_info);
}
for(var name in __tree_list){
var n = __tree_list[name];
if(n.inherit === "base_class"){
__root.children.push(n);
}
}
};
var recursive_tree = function (_file_name, _file_info, _child) {
if(!_file_info){
// console.log("ERROR: ", _file_name);
return;
}
var classes = _file_info.classes;
var requires = _file_info.requires;
for(var name in classes){
var path = _file_name + ":" + name;
var node = __tree_list[path];
if(!node){
node = new Node();
node.name = name;
__tree_list[path] = node;
}
var inheritance = classes[name];
var req = requires[inheritance];
// console.log("CLASS: %s, REQUIRE: %s", name, req);
if(req){
if(_child && node.children.indexOf(_child) === -1){
_child.inherit = name;
node.children.push(_child);
}
var fi = map[req];
recursive_tree(req, fi, node);
}
}
};
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;