forked from microsoft/vscode-cmake-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompdb.ts
58 lines (48 loc) · 1.76 KB
/
compdb.ts
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
import * as shlex from '@cmt/shlex';
import {createLogger} from './logging';
import {fs} from './pr';
import * as util from './util';
import * as nls from 'vscode-nls';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
const log = createLogger('compdb');
interface BaseCompileCommand {
directory: string;
file: string;
output?: string;
}
interface StringCompileCommand extends BaseCompileCommand {
command: string;
}
interface ArgsCompileCommand extends BaseCompileCommand {
arguments: string[];
}
export type CompileCommand = StringCompileCommand|ArgsCompileCommand;
export class CompilationDatabase {
private readonly _infoByFilePath: Map<string, ArgsCompileCommand>;
constructor(infos: CompileCommand[]) {
this._infoByFilePath = infos.reduce(
(acc, cur) => acc.set(util.platformNormalizePath(cur.file), {
directory: cur.directory,
file: cur.file,
output: cur.output,
arguments: 'arguments' in cur ? cur.arguments : [...shlex.split(cur.command)],
}),
new Map<string, ArgsCompileCommand>(),
);
}
get(fspath: string) { return this._infoByFilePath.get(util.platformNormalizePath(fspath)); }
public static async fromFilePath(dbpath: string): Promise<CompilationDatabase|null> {
if (!await fs.exists(dbpath)) {
return null;
}
const data = await fs.readFile(dbpath);
try {
const content = JSON.parse(data.toString()) as CompileCommand[];
return new CompilationDatabase(content);
} catch (e) {
log.warning(localize('error.parsing.compilation.database', 'Error parsing compilation database "{0}": {1}', dbpath, e));
return null;
}
}
}