forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsedEnvironmentFile.ts
More file actions
89 lines (72 loc) · 3.25 KB
/
Copy pathParsedEnvironmentFile.ts
File metadata and controls
89 lines (72 loc) · 3.25 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as fs from 'fs';
import * as nls from 'vscode-nls';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
export interface Environment {
name: string;
value: string;
}
export class ParsedEnvironmentFile {
public Env: Environment[];
public Warning?: string;
private constructor(env: Environment[], warning?: string) {
this.Env = env;
this.Warning = warning;
}
public static CreateFromFile(envFile: string, initialEnv?: Environment[]): ParsedEnvironmentFile {
const content: string = fs.readFileSync(envFile, "utf8");
return this.CreateFromContent(content, envFile, initialEnv);
}
public static CreateFromContent(content: string, envFile: string, initialEnv?: Environment[]): ParsedEnvironmentFile {
// Remove UTF-8 BOM if present
if (content.charAt(0) === '\uFEFF') {
content = content.substring(1);
}
const parseErrors: string[] = [];
const env: Map<string, any> = new Map();
if (initialEnv) {
// Convert array to map to prevent duplicate keys being created.
// If a duplicate key is found, replace it.
initialEnv.forEach((e) => {
env.set(e.name, e.value);
});
}
content.split("\n").forEach(line => {
// Split the line between key and value
const r: RegExpMatchArray | null = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r) {
const key: string = r[1];
let value: string = r[2] ?? "";
if ((value.length > 0) && (value.charAt(0) === '"') && (value.charAt(value.length - 1) === '"')) {
value = value.replace(/\\n/gm, "\n");
}
value = value.replace(/(^['"]|['"]$)/g, "");
env.set(key, value);
} else {
// Blank lines and lines starting with # are no parse errors
const comments: RegExp = new RegExp(/^\s*(#|$)/);
if (!comments.test(line)) {
parseErrors.push(line);
}
}
});
// show error message if single lines cannot get parsed
let warning: string | undefined;
if (parseErrors.length !== 0) {
warning = localize("ignoring.lines.in.envfile", "Ignoring non-parsable lines in {0} {1}: ", "envFile", envFile);
parseErrors.forEach(function (value, idx, array): void {
warning += "\"" + value + "\"" + ((idx !== array.length - 1) ? ", " : ".");
});
}
// Convert env map back to array.
const arrayEnv: Environment[] = [];
for (const key of env.keys()) {
arrayEnv.push({name: key, value: env.get(key)});
}
return new ParsedEnvironmentFile(arrayEnv, warning);
}
}