-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix_compile_commands.js
63 lines (58 loc) · 1.75 KB
/
fix_compile_commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// For each file named "compile_commands.json" under the src/out/* directory
// fix the directory location to work from the root of the repo instead of src
// TODO: Maybe just make this also invoke Make, since that would add a lot of
// flexibility to the system
const path = require('path');
const fsp = require('fs').promises;
const rootLoc = path.join('src', 'out');
const ccname = 'compile_commands.jsn';
// File all the compile_commands.json files underneath src/out
async function findFiles() {
const files = await fsp.readdir(rootLoc);
const ccfiles = [];
for (let subdir of files) {
const fullDir = path.join(rootLoc, subdir);
const allFiles = await fsp.readdir(fullDir);
if (allFiles.some((fn) => fn === ccname)) {
ccfiles.push(path.join(fullDir, ccname));
}
}
return ccfiles;
}
async function fixFile(filename) {
const text = (await fsp.readFile(filename))
.toString()
.split('\n')
.map((v) => v.trim());
const contents = [];
for (let i = 0; i < text.length - 2; i += 3) {
const obj = {};
const ln1 = text[i];
let flag = 0;
if (ln1.startsWith('dir#')) {
const dir = ln1.substring(4);
obj.directory = dir === '.' ? 'src' : dir;
}
flag += 1;
const ln2 = text[i + 1];
if (ln2.startsWith('fil#')) {
obj.file = ln2.substring(4);
flag += 2;
}
const ln3 = text[i + 2];
if (ln3.startsWith('cmd#')) {
obj.command = ln3.substring(4);
flag += 4;
}
if (flag === 7) {
contents.push(obj);
}
}
const jsonCC = JSON.stringify(contents, null, ' ');
await fsp.writeFile(filename.replace('.jsn', '.json'), jsonCC);
}
async function doit() {
const files = await findFiles();
await Promise.all(files.map(fixFile));
}
doit().catch(console.error);