Skip to content

Commit 276cc34

Browse files
LuckyFBBliuxy0551
authored andcommitted
fix(ci): change ci and add hash judge
1 parent 2b30e78 commit 276cc34

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: pnpm check-types
4343

4444
- name: Run antlr4 all sql
45-
run: pnpm antlr4 --lang all
45+
run: pnpm antlr4 --all --check
4646

4747
test:
4848
runs-on: ubuntu-latest

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"license": "MIT",
4141
"dependencies": {
4242
"antlr4-c3": "3.3.7",
43-
"antlr4ng": "2.0.11"
43+
"antlr4ng": "2.0.11",
44+
"crypto": "^1.0.1"
4445
},
4546
"devDependencies": {
4647
"@commitlint/cli": "^17.7.2",
@@ -70,7 +71,7 @@
7071
},
7172
"repository": {
7273
"type": "git",
73-
"url": "https://github.com/DTStack/dt-sql-parser.git"
74+
"url": "https://github.com/DTStack/dt-sql-parser.git"
7475
},
7576
"publishConfig": {
7677
"registry": "https://registry.npmjs.org/"
@@ -89,6 +90,8 @@
8990
"*": [
9091
"prettier --write --ignore-unknown"
9192
],
92-
"*.g4": ["antlr-format -c ./antlr.format.json -v"]
93+
"*.g4": [
94+
"antlr-format -c ./antlr.format.json -v"
95+
]
9396
}
9497
}

pnpm-lock.yaml

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/antlr4.js

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs');
44
const argv = require('yargs-parser')(process.argv.slice(2));
55
const inquirer = require('inquirer');
66
const chalk = require('chalk');
7+
const crypto = require('crypto');
78
const { cleanComment } = require('./cleanComment');
89

910
const grammarsPath = path.resolve(__dirname, '../src/grammar');
@@ -15,26 +16,57 @@ const languageEntries = fs.readdirSync(grammarsPath).filter((language) => {
1516

1617
const baseCmd = 'antlr4ng -Dlanguage=TypeScript -visitor -listener -Xexact-output-dir -o';
1718

18-
function compile(language) {
19-
const cmd = `${baseCmd} ${outputPath}/${language} ${grammarsPath}/${language}/*.g4`;
19+
function getFileHash(filePath) {
20+
if (!fs.existsSync(filePath)) return null;
2021

21-
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
22-
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
23-
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
24-
}
22+
const fileBuffer = fs.readFileSync(filePath);
23+
const hashSum = crypto.createHash('sha256');
24+
hashSum.update(fileBuffer);
2525

26-
console.info(chalk.green('Executing:'), chalk.gray(cmd));
27-
exec(cmd, (err) => {
28-
if (err) {
29-
console.error(
30-
chalk.redBright(`\n[Antlr4 compile error]:`),
31-
chalk.cyan(language),
32-
chalk.gray(err)
33-
);
34-
} else {
35-
cleanComment(language);
36-
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
26+
return hashSum.digest('hex');
27+
}
28+
29+
function compile(language) {
30+
return new Promise((resolve, reject) => {
31+
const outputDir = `${outputPath}/${language}`;
32+
const grammarFiles = fs
33+
.readdirSync(`${grammarsPath}/${language}`)
34+
.filter((file) => file.endsWith('.g4'));
35+
const previousHashes = grammarFiles.map((file) => ({
36+
file,
37+
hash: getFileHash(path.join(outputDir, file.replace('.g4', '.ts'))),
38+
}));
39+
40+
if (language !== 'plsql' && fs.existsSync(`${outputPath}/${language}`)) {
41+
console.info(chalk.green(`\nRemoving:`, chalk.gray(`${outputPath}/${language}/*`)));
42+
fs.rmSync(`${outputPath}/${language}`, { recursive: true });
3743
}
44+
45+
const cmd = `${baseCmd} ${outputDir} ${grammarsPath}/${language}/*.g4`;
46+
console.info(chalk.green('Executing:'), chalk.gray(cmd));
47+
exec(cmd, (err) => {
48+
if (err) {
49+
console.error(
50+
chalk.redBright(`\n[Antlr4 compile error]:`),
51+
chalk.cyan(language),
52+
chalk.gray(err)
53+
);
54+
} else {
55+
cleanComment(language);
56+
console.info(chalk.greenBright(`Compile ${language} succeeded!`));
57+
58+
const changedFiles = grammarFiles.filter((file) => {
59+
const newHash = getFileHash(path.join(outputDir, file.replace('.g4', '.ts')));
60+
const prevHash = previousHashes.find((h) => h.file === file)?.hash;
61+
return newHash !== prevHash;
62+
});
63+
64+
if (changedFiles.length > 0) {
65+
return reject(`${language} not run antlr4`);
66+
}
67+
resolve();
68+
}
69+
});
3870
});
3971
}
4072

@@ -61,22 +93,32 @@ function prompt() {
6193
});
6294
}
6395

96+
async function antlr4AllSql() {
97+
const errors = [];
98+
99+
const tasks = languageEntries.map((language) =>
100+
compile(language).catch((err) => errors.push(err))
101+
);
102+
103+
await Promise.all(tasks);
104+
105+
if (errors.length > 0 && argv.check) {
106+
errors.forEach((error) => console.error(chalk.red(`- ${error}`)));
107+
process.exit(1); // 非零退出表示错误
108+
}
109+
}
110+
64111
function main() {
65112
if (argv.all) {
66-
// compile all: yarn antlr4 --all
67-
languageEntries.forEach((language) => {
68-
compile(language);
69-
});
113+
antlr4AllSql();
70114
} else if (argv.lang && typeof argv.lang === 'string') {
71115
// compile single: yarn antlr4 --lang=mysql
72116
const supportedLanguage = languageEntries.find((language) =>
73117
language.startsWith(argv.lang)
74118
);
75119

76120
if (argv.lang === 'all') {
77-
languageEntries.forEach((language) => {
78-
compile(language);
79-
});
121+
antlr4AllSql();
80122
} else if (supportedLanguage) {
81123
compile(supportedLanguage);
82124
} else {

0 commit comments

Comments
 (0)