Skip to content

Commit c0857eb

Browse files
committed
fix(ci): change ci and add hash judge
1 parent 3fcb005 commit c0857eb

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');
@@ -13,26 +14,57 @@ const languageEntries = fs.readdirSync(grammarsPath);
1314

1415
const baseCmd = 'antlr4ng -Dlanguage=TypeScript -visitor -listener -Xexact-output-dir -o';
1516

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

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

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

@@ -59,22 +91,32 @@ function prompt() {
5991
});
6092
}
6193

94+
async function antlr4AllSql() {
95+
const errors = [];
96+
97+
const tasks = languageEntries.map((language) =>
98+
compile(language).catch((err) => errors.push(err))
99+
);
100+
101+
await Promise.all(tasks);
102+
103+
if (errors.length > 0 && argv.check) {
104+
errors.forEach((error) => console.error(chalk.red(`- ${error}`)));
105+
process.exit(1); // 非零退出表示错误
106+
}
107+
}
108+
62109
function main() {
63110
if (argv.all) {
64-
// compile all: yarn antlr4 --all
65-
languageEntries.forEach((language) => {
66-
compile(language);
67-
});
111+
antlr4AllSql();
68112
} else if (argv.lang) {
69113
// compile single: yarn antlr4 --lang=mysql
70114
const supportedLanguage = languageEntries.find((language) =>
71115
language.startsWith(argv.lang)
72116
);
73117

74118
if (argv.lang === 'all') {
75-
languageEntries.forEach((language) => {
76-
compile(language);
77-
});
119+
antlr4AllSql();
78120
} else if (supportedLanguage) {
79121
compile(supportedLanguage);
80122
} else {

0 commit comments

Comments
 (0)