Created
August 25, 2014 11:10
-
-
Save alivedise/6c00b7da0c725ec74953 to your computer and use it in GitHub Desktop.
parse.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var utils = require('utils'); | |
| exports.execute = function(options) { | |
| var dir = utils.getFile(options.GAIA_DIR, 'ast'); | |
| utils.ensureFolderExists(dir); | |
| var files = utils.ls(utils.getFile(options.GAIA_DIR, 'apps', 'system', 'js'), true); | |
| var moduleList = []; | |
| var output = dir.clone(); | |
| output.append('system-dep.json'); | |
| var result = 'digraph G {\n'; | |
| function file2Module(strings) { | |
| var i = 0; | |
| var ch = ''; | |
| var pre = ''; | |
| while (i <= strings.length) { | |
| var character = strings.charAt(i); | |
| if (i === 0) { | |
| ch += character.toUpperCase(); | |
| } else if (character === '_') { | |
| } else if (pre === '_') { | |
| ch += character.toUpperCase(); | |
| } else { | |
| ch += character; | |
| } | |
| i++; | |
| pre = character; | |
| } | |
| return ch; | |
| }; | |
| function module2File(strings) { | |
| var i = 0; | |
| var ch = ''; | |
| while (i <= strings.length) { | |
| var character = strings.charAt(i); | |
| if (character !== character.toLowerCase()) { | |
| if (ch === '') { | |
| ch += character.toLowerCase(); | |
| } else { | |
| ch += '_' + character.toLowerCase(); | |
| } | |
| } else { | |
| ch += character; | |
| } | |
| i++; | |
| } | |
| return ch; | |
| }; | |
| // First round: get module list | |
| files.forEach(function(file) { | |
| if (!file.leafName.contains('.js')) { | |
| return; | |
| } | |
| var fileName = file.leafName.substr(0, file.leafName.lastIndexOf('.')).toLowerCase(); | |
| moduleList.push(file2Module(fileName)); | |
| }); | |
| dump(moduleList); | |
| // Second round: get file content | |
| files.forEach(function(file) { | |
| if (!file.leafName.contains('.js')) { | |
| return; | |
| } | |
| var fileName = file.leafName.substr(0, file.leafName.lastIndexOf('.')).toLowerCase(); | |
| dump('parsing: ' + file.path + '\n'); | |
| var content = utils.getFileContent(file); | |
| var ast = utils.scriptParser(content); | |
| var jstring = JSON.stringify(ast, null, 2); | |
| moduleList.forEach(function(module) { | |
| if (jstring.search('\"' + module + '\"') < 0) { | |
| return; | |
| } | |
| if (module2File(module) === fileName) { | |
| return; | |
| } | |
| dump(file2Module(fileName) + ' -> ' + module + ';\n'); | |
| result = result + '\t' + file2Module(fileName) + ' -> ' + module + ';\n'; | |
| }); | |
| }); | |
| result = result + '}'; | |
| utils.writeContent(output, result); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment