build: optimize antlr script (#171)

This commit is contained in:
Hayden 2023-10-09 17:43:41 +08:00 committed by GitHub
parent 7bbaba6398
commit fc97bba879
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 60 deletions

View File

@ -1,51 +0,0 @@
const path = require('path');
const exec = require('child_process').exec;
const argv = require('yargs-parser')(process.argv.slice(2))
const antlr4 = path.resolve(__dirname, './antlr-4.12.0-complete.jar');
const grammars = path.resolve(__dirname, '../src/grammar');
const output = path.resolve(__dirname, '../src/lib');
const entry = [
'generic',
'hive',
'pgsql',
'plsql',
'spark',
'flinksql',
'trinosql',
];
function compile(language) {
const cmd = `
antlr4ts
-visitor
-listener
-Xexact-output-dir -o ${output}/${language}
${grammars}/${language}/*.g4
`.replace(/\n/g, '');
console.info('Executing:', cmd);
exec(cmd, (err) => {
if (err) {
console.error('Antlr4 build error: ' + language, err);
} else {
console.log(`Build ${language} success.`);
}
});
}
if (argv.all) { // build all: yarn antlr4 --all
entry.forEach((language) => {
compile(language);
});
} else if (argv.lang) {// build single: yarn antlr4 --lang=generic
const supportedLanguage = entry.find((language) => language === argv.lang);
if (supportedLanguage) {
compile(argv.lang);
} else {
console.error('Invalid language: ' + argv.lang + ', supported languages:\n' + entry.join(', '));
}
} else {
console.error('Please to specify the language, just like: yarn antlr4 --lang flinksql');
}

View File

@ -18,7 +18,7 @@
"dist" "dist"
], ],
"scripts": { "scripts": {
"antlr4": "node build/antlr4.js", "antlr4": "node ./scripts/antlr4.js",
"build": "rm -rf dist && tsc", "build": "rm -rf dist && tsc",
"eslint": "eslint ./src/**/*.ts", "eslint": "eslint ./src/**/*.ts",
"check-types": "tsc --skipLibCheck", "check-types": "tsc --skipLibCheck",
@ -35,6 +35,7 @@
"@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1", "@typescript-eslint/parser": "^3.10.1",
"antlr4ts-cli": "^0.5.0-alpha.4", "antlr4ts-cli": "^0.5.0-alpha.4",
"chalk": "4.1.2",
"eslint": "^7.32.0", "eslint": "^7.32.0",
"eslint-config-google": "^0.14.0", "eslint-config-google": "^0.14.0",
"inquirer": "^8.2.2", "inquirer": "^8.2.2",

View File

@ -10,6 +10,7 @@ specifiers:
antlr4-c3: ^3.0.1 antlr4-c3: ^3.0.1
antlr4ts: ^0.5.0-alpha.4 antlr4ts: ^0.5.0-alpha.4
antlr4ts-cli: ^0.5.0-alpha.4 antlr4ts-cli: ^0.5.0-alpha.4
chalk: 4.1.2
eslint: ^7.32.0 eslint: ^7.32.0
eslint-config-google: ^0.14.0 eslint-config-google: ^0.14.0
inquirer: ^8.2.2 inquirer: ^8.2.2
@ -30,6 +31,7 @@ devDependencies:
'@typescript-eslint/eslint-plugin': 3.10.1_a18e814019c959d52a44881c5533ce12 '@typescript-eslint/eslint-plugin': 3.10.1_a18e814019c959d52a44881c5533ce12
'@typescript-eslint/parser': 3.10.1_eslint@7.32.0+typescript@5.0.4 '@typescript-eslint/parser': 3.10.1_eslint@7.32.0+typescript@5.0.4
antlr4ts-cli: 0.5.0-alpha.4 antlr4ts-cli: 0.5.0-alpha.4
chalk: 4.1.2
eslint: 7.32.0 eslint: 7.32.0
eslint-config-google: 0.14.0_eslint@7.32.0 eslint-config-google: 0.14.0_eslint@7.32.0
inquirer: 8.2.6 inquirer: 8.2.6

85
scripts/antlr4.js Normal file
View File

@ -0,0 +1,85 @@
const path = require("path");
const exec = require("child_process").exec;
const fs = require("fs");
const argv = require("yargs-parser")(process.argv.slice(2));
const inquirer = require("inquirer");
const chalk = require("chalk");
const grammarsPath = path.resolve(__dirname, "../src/grammar");
const outputPath = path.resolve(__dirname, "../src/lib");
const languageEntries = fs
.readdirSync(grammarsPath)
.filter((item) => item !== "impala"); // impala is not support yet.
const baseCmd = 'antlr4ts -visitor -listener -Xexact-output-dir -o';
function compile(language) {
const cmd = `${baseCmd} ${outputPath}/${language} ${grammarsPath}/${language}/*.g4`;
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
fs.rmdirSync(`${outputPath}/${language}`, { recursive: true })
console.info(chalk.green("Executing:"), chalk.gray(cmd));
exec(cmd, (err) => {
if (err) {
console.error(
chalk.redBright(`\n[Antlr4 compile error]:`),
chalk.cyan(language),
chalk.gray(err)
);
} else {
console.log(chalk.greenBright(`\nCompile ${language} succeeded!`));
}
});
}
function prompt() {
inquirer
.prompt([
{
type: "list",
name: "language",
message: "Which language you want compile (or all languages)",
choices: ["All Languages", ...languageEntries],
loop: true,
},
])
.then((result) => {
const language = result.language;
if(language === 'All Languages') {
languageEntries.forEach((language) => {
compile(language);
});
} else {
compile(result.language);
}
});
}
function main() {
if (argv.all) {
// compile all: yarn antlr4 --all
languageEntries.forEach((language) => {
compile(language);
});
} else if (argv.lang) {
// compile single: yarn antlr4 --lang=generic
const supportedLanguage = languageEntries.some(
(language) => language === argv.lang
);
if (supportedLanguage) {
compile(argv.lang);
} else {
console.error(
chalk.bold.red("\n[Invalid language]:"),
chalk.white.underline(`${argv.lang}\n`)
);
prompt();
}
} else {
prompt();
}
}
main();

View File

@ -1161,6 +1161,14 @@ caniuse-lite@^1.0.30001489:
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001491.tgz#eab0e0f392de6f7411751d148de9b5bd6b203e46" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001491.tgz#eab0e0f392de6f7411751d148de9b5bd6b203e46"
integrity sha512-17EYIi4TLnPiTzVKMveIxU5ETlxbSO3B6iPvMbprqnKh4qJsQGk5Nh1Lp4jIMAE0XfrujsJuWZAM3oJdMHaKBA== integrity sha512-17EYIi4TLnPiTzVKMveIxU5ETlxbSO3B6iPvMbprqnKh4qJsQGk5Nh1Lp4jIMAE0XfrujsJuWZAM3oJdMHaKBA==
chalk@4.1.2, chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.1:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"
chalk@^2.0.0, chalk@^2.4.2: chalk@^2.0.0, chalk@^2.4.2:
version "2.4.2" version "2.4.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
@ -1170,14 +1178,6 @@ chalk@^2.0.0, chalk@^2.4.2:
escape-string-regexp "^1.0.5" escape-string-regexp "^1.0.5"
supports-color "^5.3.0" supports-color "^5.3.0"
chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.1:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"
char-regex@^1.0.2: char-regex@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf" resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"