| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- // Licensed to Cloudera, Inc. under one
- // or more contributor license agreements. See the NOTICE file
- // distributed with this work for additional information
- // regarding copyright ownership. Cloudera, Inc. licenses this file
- // to you under the Apache License, Version 2.0 (the
- // "License"); you may not use this file except in compliance
- // with the License. You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- const fs = require('fs');
- const exec = require('child_process').exec;
- const LICENSE =
- '// Licensed to Cloudera, Inc. under one\n' +
- '// or more contributor license agreements. See the NOTICE file\n' +
- '// distributed with this work for additional information\n' +
- '// regarding copyright ownership. Cloudera, Inc. licenses this file\n' +
- '// to you under the Apache License, Version 2.0 (the\n' +
- '// "License"); you may not use this file except in compliance\n' +
- '// with the License. You may obtain a copy of the License at\n' +
- '//\n' +
- '// http://www.apache.org/licenses/LICENSE-2.0\n' +
- '//\n' +
- '// Unless required by applicable law or agreed to in writing, software\n' +
- '// distributed under the License is distributed on an "AS IS" BASIS,\n' +
- '// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' +
- '// See the License for the specific language governing permissions and\n' +
- '// limitations under the License.\n';
- const SQL_STATEMENTS_PARSER_JSDOC =
- '/**\n' +
- ' * @param {string} input\n' +
- ' *\n' +
- ' * @return {SqlStatementsParserResult}\n' +
- ' */\n';
- const JISON_FOLDER = 'desktop/core/src/desktop/js/parse/jison/';
- const TARGET_FOLDER = 'desktop/core/src/desktop/js/parse/';
- const parserDefinitions = {
- globalSearchParser: {
- sources: ['globalSearchParser.jison'],
- target: 'globalSearchParser.jison',
- afterParse: contents =>
- new Promise(resolve => {
- resolve(
- LICENSE +
- contents.replace(
- 'var globalSearchParser = ',
- "import SqlParseSupport from 'parse/sqlParseSupport';\n\nvar globalSearchParser = "
- ) +
- '\nexport default globalSearchParser;\n'
- );
- })
- },
- solrFormulaParser: {
- sources: ['solrFormulaParser.jison'],
- target: 'solrFormulaParser.jison',
- afterParse: contents =>
- new Promise(resolve => {
- resolve(LICENSE + contents + 'export default solrFormulaParser;\n');
- })
- },
- solrQueryParser: {
- sources: ['solrQueryParser.jison'],
- target: 'solrQueryParser.jison',
- afterParse: contents =>
- new Promise(resolve => {
- resolve(LICENSE + contents + 'export default solrQueryParser;\n');
- })
- },
- sqlStatementsParser: {
- sources: ['sqlStatementsParser.jison'],
- target: 'sqlStatementsParser.jison',
- afterParse: contents =>
- new Promise(resolve => {
- resolve(
- LICENSE +
- contents.replace(
- 'parse: function parse',
- SQL_STATEMENTS_PARSER_JSDOC + 'parse: function parse'
- ) +
- 'export default sqlStatementsParser;\n'
- );
- })
- }
- };
- const readFile = path =>
- new Promise((resolve, reject) => {
- fs.readFile(path, (err, buf) => {
- if (err) {
- reject();
- }
- resolve(buf.toString());
- });
- });
- const writeFile = (path, contents) =>
- new Promise((resolve, reject) => {
- fs.writeFile(path, contents, err => {
- if (err) {
- reject();
- }
- resolve();
- });
- });
- const deleteFile = path => {
- fs.unlinkSync(path);
- };
- const execCmd = cmd =>
- new Promise((resolve, reject) => {
- exec(cmd, (err, stdout, stderr) => {
- if (err) {
- reject('stderr:\n' + stderr + '\n\nstdout:\n' + stdout);
- }
- resolve();
- });
- });
- const generateParser = parserName =>
- new Promise((resolve, reject) => {
- const parserConfig = parserDefinitions[parserName];
- const concatPromise = new Promise((resolve, reject) => {
- if (parserConfig.sources.length > 1 && parserConfig.target) {
- console.log('Concatenating files...');
- const promises = parserConfig.sources.map(fileName => readFile(JISON_FOLDER + fileName));
- Promise.all(promises)
- .then(contents => {
- writeFile(JISON_FOLDER + parserConfig.target, contents.join('')).then(() => {
- resolve(JISON_FOLDER + parserConfig.target);
- });
- })
- .catch(reject);
- } else if (parserConfig.sources.length === 1) {
- resolve(JISON_FOLDER + parserConfig.sources[0]);
- } else {
- reject('No jison source specified');
- }
- });
- concatPromise
- .then(targetPath => {
- let jisonCommand = 'jison ' + targetPath;
- if (parserConfig.lexer) {
- jisonCommand += ' ' + JISON_FOLDER + parserConfig.lexer;
- }
- jisonCommand += ' -m js';
- console.log('Generating parser...');
- execCmd(jisonCommand)
- .then(() => {
- if (parserConfig.sources.length > 1) {
- deleteFile(targetPath); // Remove concatenated file
- }
- console.log('Adjusting JS...');
- const generatedJsFileName = parserConfig.target.replace('.jison', '.js');
- readFile(generatedJsFileName)
- .then(contents => {
- parserConfig
- .afterParse(contents)
- .then(finalContents => {
- writeFile(TARGET_FOLDER + generatedJsFileName, finalContents)
- .then(() => {
- deleteFile(generatedJsFileName);
- console.log('Done!\n');
- resolve();
- })
- .catch(reject);
- })
- .catch(reject);
- })
- .catch(reject);
- })
- .catch(reject);
- })
- .catch(reject);
- });
- let parsersToGenerate = [];
- const invalid = [];
- let all = false;
- let appFound = false;
- const listDir = folder =>
- new Promise(resolve => {
- fs.readdir(folder, (err, files) => {
- resolve(files);
- });
- });
- const findParser = (fileIndex, folder, sharedFiles, autocomplete) => {
- const prefix = autocomplete ? 'autocomplete' : 'syntax';
- if (fileIndex[prefix + '_header.jison'] && fileIndex[prefix + '_footer.jison']) {
- const parserName = folder + (autocomplete ? 'AutocompleteParser' : 'SyntaxParser');
- const parserDefinition = {
- sources: ['sql/' + folder + '/' + prefix + '_header.jison'].concat(sharedFiles),
- lexer: 'sql/' + folder + '/sql.jisonlex',
- target: 'sql/' + folder + '/' + parserName + '.jison',
- afterParse: contents =>
- new Promise(resolve => {
- resolve(
- LICENSE +
- contents
- .replace(
- 'var ' + parserName + ' = ',
- "import SqlParseSupport from 'parse/sqlParseSupport';\n\nvar " +
- parserName +
- ' = '
- )
- .replace(
- 'loc: yyloc,',
- "loc: lexer.yylloc, ruleId: stack.slice(stack.length - 2, stack.length).join(''),"
- ) +
- '\nexport default ' +
- parserName +
- ';\n'
- );
- })
- };
- parserDefinition.sources.push('sql/' + folder + '/' + prefix + '_footer.jison');
- parserDefinitions[parserName] = parserDefinition;
- } else {
- console.log(
- "Warn: Could not find '" +
- prefix +
- "_header.jison' or '" +
- prefix +
- "_footer.jison' in " +
- JISON_FOLDER +
- 'sql/' +
- folder +
- '/'
- );
- }
- };
- const identifySqlParsers = () =>
- new Promise(resolve => {
- listDir(JISON_FOLDER + 'sql').then(files => {
- const promises = [];
- files.forEach(folder => {
- promises.push(
- listDir(JISON_FOLDER + 'sql/' + folder).then(jisonFiles => {
- const fileIndex = {};
- jisonFiles.forEach(jisonFile => {
- fileIndex[jisonFile] = true;
- });
- const sharedFiles = jisonFiles
- .filter(jisonFile => jisonFile.indexOf('sql_') !== -1)
- .map(jisonFile => 'sql/' + folder + '/' + jisonFile);
- if (fileIndex['sql.jisonlex']) {
- findParser(fileIndex, folder, sharedFiles, true);
- findParser(fileIndex, folder, sharedFiles, false);
- } else {
- console.log(
- "Warn: Could not find 'sql.jisonlex' in " + JISON_FOLDER + 'sql/' + folder + '/'
- );
- }
- })
- );
- });
- Promise.all(promises).then(resolve);
- });
- });
- identifySqlParsers().then(() => {
- process.argv.forEach(arg => {
- if (appFound) {
- if (arg === 'all') {
- all = true;
- } else if (parserDefinitions[arg]) {
- parsersToGenerate.push(arg);
- } else {
- let prefixFound = false;
- Object.keys(parserDefinitions).forEach(key => {
- if (key.indexOf(arg) === 0) {
- prefixFound = true;
- parsersToGenerate.push(key);
- }
- });
- if (!prefixFound) {
- invalid.push(arg);
- }
- }
- } else if (arg.indexOf('generateParsers.js') !== -1) {
- appFound = true;
- }
- });
- if (all) {
- parsersToGenerate = Object.keys(parserDefinitions);
- }
- if (invalid.length) {
- console.log("No parser config found for: '" + invalid.join("', '") + "'");
- console.log(
- '\nPossible options are:\n ' +
- ['all'].concat(Object.keys(parserDefinitions)).join('\n ') +
- '\n'
- );
- return;
- }
- const parserCount = parsersToGenerate.length;
- let idx = 0;
- const generateRecursive = () => {
- idx++;
- if (parsersToGenerate.length) {
- const parserName = parsersToGenerate.pop();
- if (parserCount > 1) {
- console.log("Generating '" + parserName + "' (" + idx + '/' + parserCount + ')...');
- } else {
- console.log("Generating '" + parserName + "'...");
- }
- generateParser(parserName)
- .then(generateRecursive)
- .catch(error => {
- console.log(error);
- console.log('FAIL!');
- });
- }
- };
- generateRecursive();
- });
|