From fc97bba87906d730b9b31b7e96d400b2c1918bd1 Mon Sep 17 00:00:00 2001 From: Hayden Date: Mon, 9 Oct 2023 17:43:41 +0800 Subject: [PATCH] build: optimize antlr script (#171) --- build/antlr4.js | 51 ---------------------------- package.json | 3 +- pnpm-lock.yaml | 2 ++ scripts/antlr4.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 16 ++++----- 5 files changed, 97 insertions(+), 60 deletions(-) delete mode 100644 build/antlr4.js create mode 100644 scripts/antlr4.js diff --git a/build/antlr4.js b/build/antlr4.js deleted file mode 100644 index 55f0e7d..0000000 --- a/build/antlr4.js +++ /dev/null @@ -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'); -} - diff --git a/package.json b/package.json index 4aa86f2..35e223b 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "dist" ], "scripts": { - "antlr4": "node build/antlr4.js", + "antlr4": "node ./scripts/antlr4.js", "build": "rm -rf dist && tsc", "eslint": "eslint ./src/**/*.ts", "check-types": "tsc --skipLibCheck", @@ -35,6 +35,7 @@ "@typescript-eslint/eslint-plugin": "^3.10.1", "@typescript-eslint/parser": "^3.10.1", "antlr4ts-cli": "^0.5.0-alpha.4", + "chalk": "4.1.2", "eslint": "^7.32.0", "eslint-config-google": "^0.14.0", "inquirer": "^8.2.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a677b7b..bf99885 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10,6 +10,7 @@ specifiers: antlr4-c3: ^3.0.1 antlr4ts: ^0.5.0-alpha.4 antlr4ts-cli: ^0.5.0-alpha.4 + chalk: 4.1.2 eslint: ^7.32.0 eslint-config-google: ^0.14.0 inquirer: ^8.2.2 @@ -30,6 +31,7 @@ devDependencies: '@typescript-eslint/eslint-plugin': 3.10.1_a18e814019c959d52a44881c5533ce12 '@typescript-eslint/parser': 3.10.1_eslint@7.32.0+typescript@5.0.4 antlr4ts-cli: 0.5.0-alpha.4 + chalk: 4.1.2 eslint: 7.32.0 eslint-config-google: 0.14.0_eslint@7.32.0 inquirer: 8.2.6 diff --git a/scripts/antlr4.js b/scripts/antlr4.js new file mode 100644 index 0000000..0506e16 --- /dev/null +++ b/scripts/antlr4.js @@ -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(); diff --git a/yarn.lock b/yarn.lock index aaba381..532313d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1161,6 +1161,14 @@ caniuse-lite@^1.0.30001489: resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001491.tgz#eab0e0f392de6f7411751d148de9b5bd6b203e46" 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: version "2.4.2" 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" 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: version "1.0.2" resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"