-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcommon.ts
1075 lines (961 loc) · 41.1 KB
/
common.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as child_process from 'child_process';
import * as vscode from 'vscode';
import * as Telemetry from './telemetry';
import HttpsProxyAgent = require('https-proxy-agent');
import * as url from 'url';
import { PlatformInformation } from './platform';
import { getOutputChannelLogger, showOutputChannel } from './logger';
import * as assert from 'assert';
import * as https from 'https';
import { ClientRequest, OutgoingHttpHeaders } from 'http';
import { getBuildTasks } from './LanguageServer/extension';
import { OtherSettings } from './LanguageServer/settings';
import { lookupString } from './nativeStrings';
import * as nls from 'vscode-nls';
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
export type Mutable<T> = {
-readonly [P in keyof T]: T[P] extends ReadonlyArray<infer U> ? Mutable<U>[] : Mutable<T[P]>
};
export let extensionPath: string;
export let extensionContext: vscode.ExtensionContext;
export function setExtensionContext(context: vscode.ExtensionContext): void {
extensionContext = context;
extensionPath = extensionContext.extensionPath;
}
export function setExtensionPath(path: string): void {
extensionPath = path;
}
export const failedToParseTasksJson: string = localize("failed.to.parse.tasks", "Failed to parse tasks.json, possibly due to comments or trailing commas.");
// Use this package.json to read values
export const packageJson: any = vscode.extensions.getExtension("ms-vscode.cpptools").packageJSON;
// Use getRawPackageJson to read and write back to package.json
// This prevents obtaining any of VSCode's expanded variables.
let rawPackageJson: any = null;
export function getRawPackageJson(): any {
if (rawPackageJson === null) {
const fileContents: Buffer = fs.readFileSync(getPackageJsonPath());
rawPackageJson = JSON.parse(fileContents.toString());
}
return rawPackageJson;
}
export function getRawTasksJson(): Promise<any> {
const path: string = getTasksJsonPath();
if (!path) {
return undefined;
}
return new Promise<any>((resolve, reject) => {
fs.exists(path, exists => {
if (!exists) {
return resolve({});
}
let fileContents: string = fs.readFileSync(path).toString();
fileContents = fileContents.replace(/^\s*\/\/.*$/gm, ""); // Remove start of line // comments.
let rawTasks: any = {};
try {
rawTasks = JSON.parse(fileContents);
} catch (error) {
return reject(new Error(failedToParseTasksJson));
}
resolve(rawTasks);
});
});
}
export async function ensureBuildTaskExists(taskName: string): Promise<void> {
let rawTasksJson: any = await getRawTasksJson();
// Ensure that the task exists in the user's task.json. Task will not be found otherwise.
if (!rawTasksJson.tasks) {
rawTasksJson.tasks = new Array();
}
// Find or create the task which should be created based on the selected "debug configuration".
let selectedTask: vscode.Task = rawTasksJson.tasks.find(task => {
return task.label && task.label === task;
});
if (selectedTask) {
return;
}
const buildTasks: vscode.Task[] = await getBuildTasks(false);
selectedTask = buildTasks.find(task => task.name === taskName);
console.assert(selectedTask);
rawTasksJson.version = "2.0.0";
if (!rawTasksJson.tasks.find(task => task.label === selectedTask.definition.label)) {
rawTasksJson.tasks.push(selectedTask.definition);
}
// TODO: It's dangerous to overwrite this file. We could be wiping out comments.
let settings: OtherSettings = new OtherSettings();
await writeFileText(getTasksJsonPath(), JSON.stringify(rawTasksJson, null, settings.editorTabSize));
}
export function fileIsCOrCppSource(file: string): boolean {
const fileExtLower: string = path.extname(file).toLowerCase();
return [".C", ".c", ".cpp", ".cc", ".cxx", ".mm", ".ino", ".inl"].some(ext => fileExtLower === ext);
}
export function isEditorFileCpp(file: string): boolean {
let editor: vscode.TextEditor = vscode.window.visibleTextEditors.find(e => e.document.uri.toString() === file);
return editor && editor.document.languageId === "cpp";
}
// This function is used to stringify the rawPackageJson.
// Do not use with util.packageJson or else the expanded
// package.json will be written back.
export function stringifyPackageJson(packageJson: string): string {
return JSON.stringify(packageJson, null, 2);
}
export function getExtensionFilePath(extensionfile: string): string {
return path.resolve(extensionPath, extensionfile);
}
export function getPackageJsonPath(): string {
return getExtensionFilePath("package.json");
}
export function getTasksJsonPath(): string {
const editor: vscode.TextEditor = vscode.window.activeTextEditor;
const folder: vscode.WorkspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri);
if (!folder) {
return undefined;
}
return path.join(folder.uri.fsPath, ".vscode", "tasks.json");
}
export function getVcpkgPathDescriptorFile(): string {
if (process.platform === 'win32') {
return path.join(process.env.LOCALAPPDATA, "vcpkg/vcpkg.path.txt");
} else {
return path.join(process.env.HOME, ".vcpkg/vcpkg.path.txt");
}
}
let vcpkgRoot: string;
export function getVcpkgRoot(): string {
if (!vcpkgRoot && vcpkgRoot !== "") {
vcpkgRoot = "";
// Check for vcpkg instance.
if (fs.existsSync(getVcpkgPathDescriptorFile())) {
let vcpkgRootTemp: string = fs.readFileSync(getVcpkgPathDescriptorFile()).toString();
vcpkgRootTemp = vcpkgRootTemp.trim();
if (fs.existsSync(vcpkgRootTemp)) {
vcpkgRoot = path.join(vcpkgRootTemp, "/installed").replace(/\\/g, "/");
}
}
}
return vcpkgRoot;
}
/**
* This is a fuzzy determination of whether a uri represents a header file.
* For the purposes of this function, a header file has no extension, or an extension that begins with the letter 'h'.
* @param document The document to check.
*/
export function isHeader(uri: vscode.Uri): boolean {
let ext: string = path.extname(uri.fsPath);
return !ext || ext.startsWith(".h") || ext.startsWith(".H");
}
// Extension is ready if install.lock exists and debugAdapters folder exist.
export async function isExtensionReady(): Promise<boolean> {
const doesInstallLockFileExist: boolean = await checkInstallLockFile();
return doesInstallLockFileExist;
}
let isExtensionNotReadyPromptDisplayed: boolean = false;
export const extensionNotReadyString: string = localize("extension.not.ready", 'The C/C++ extension is still installing. See the output window for more information.');
export function displayExtensionNotReadyPrompt(): void {
if (!isExtensionNotReadyPromptDisplayed) {
isExtensionNotReadyPromptDisplayed = true;
showOutputChannel();
getOutputChannelLogger().showInformationMessage(extensionNotReadyString).then(
() => { isExtensionNotReadyPromptDisplayed = false; },
() => { isExtensionNotReadyPromptDisplayed = false; }
);
}
}
// This Progress global state tracks how far users are able to get before getting blocked.
// Users start with a progress of 0 and it increases as they get further along in using the tool.
// This eliminates noise/problems due to re-installs, terminated installs that don't send errors,
// errors followed by workarounds that lead to success, etc.
const progressInstallSuccess: number = 100;
const progressExecutableStarted: number = 150;
const progressExecutableSuccess: number = 200;
const progressParseRootSuccess: number = 300;
const progressIntelliSenseNoSquiggles: number = 1000;
// Might add more IntelliSense progress measurements later.
// IntelliSense progress is separate from the install progress, because parse root can occur afterwards.
let installProgressStr: string = "CPP." + packageJson.version + ".Progress";
let intelliSenseProgressStr: string = "CPP." + packageJson.version + ".IntelliSenseProgress";
export function getProgress(): number {
return extensionContext ? extensionContext.globalState.get<number>(installProgressStr, -1) : -1;
}
export function getIntelliSenseProgress(): number {
return extensionContext ? extensionContext.globalState.get<number>(intelliSenseProgressStr, -1) : -1;
}
export function setProgress(progress: number): void {
if (extensionContext && getProgress() < progress) {
extensionContext.globalState.update(installProgressStr, progress);
let telemetryProperties: { [key: string]: string } = {};
let progressName: string;
switch (progress) {
case 0: progressName = "install started"; break;
case progressInstallSuccess: progressName = "install succeeded"; break;
case progressExecutableStarted: progressName = "executable started"; break;
case progressExecutableSuccess: progressName = "executable succeeded"; break;
case progressParseRootSuccess: progressName = "parse root succeeded"; break;
}
telemetryProperties['progress'] = progressName;
Telemetry.logDebuggerEvent("progress", telemetryProperties);
}
}
export function setIntelliSenseProgress(progress: number): void {
if (extensionContext && getIntelliSenseProgress() < progress) {
extensionContext.globalState.update(intelliSenseProgressStr, progress);
let telemetryProperties: { [key: string]: string } = {};
let progressName: string;
switch (progress) {
case progressIntelliSenseNoSquiggles: progressName = "IntelliSense no squiggles"; break;
}
telemetryProperties['progress'] = progressName;
Telemetry.logDebuggerEvent("progress", telemetryProperties);
}
}
export function getProgressInstallSuccess(): number { return progressInstallSuccess; } // Download/install was successful (i.e. not blocked by component acquisition).
export function getProgressExecutableStarted(): number { return progressExecutableStarted; } // The extension was activated and starting the executable was attempted.
export function getProgressExecutableSuccess(): number { return progressExecutableSuccess; } // Starting the exe was successful (i.e. not blocked by 32-bit or glibc < 2.18 on Linux)
export function getProgressParseRootSuccess(): number { return progressParseRootSuccess; } // Parse root was successful (i.e. not blocked by processing taking too long).
export function getProgressIntelliSenseNoSquiggles(): number { return progressIntelliSenseNoSquiggles; } // IntelliSense was successful and the user got no squiggles.
export function isUri(input: any): input is vscode.Uri {
return input && input instanceof vscode.Uri;
}
export function isString(input: any): input is string {
return typeof(input) === "string";
}
export function isNumber(input: any): input is number {
return typeof(input) === "number";
}
export function isBoolean(input: any): input is boolean {
return typeof(input) === "boolean";
}
export function isArray(input: any): input is any[] {
return input instanceof Array;
}
export function isOptionalString(input: any): input is string | undefined {
return input === undefined || isString(input);
}
export function isArrayOfString(input: any): input is string[] {
return isArray(input) && input.every(isString);
}
export function isOptionalArrayOfString(input: any): input is string[] | undefined {
return input === undefined || isArrayOfString(input);
}
export function resolveCachePath(input: string, additionalEnvironment: {[key: string]: string | string[]}): string {
let resolvedPath: string = "";
if (!input) {
// If no path is set, return empty string to language service process, where it will set the default path as
// Windows: %LocalAppData%/Microsoft/vscode-cpptools/
// Linux and Mac: ~/.vscode-cpptools/
return resolvedPath;
}
resolvedPath = resolveVariables(input, additionalEnvironment);
return resolvedPath;
}
export function resolveVariables(input: string, additionalEnvironment: {[key: string]: string | string[]}): string {
if (!input) {
return "";
}
if (!additionalEnvironment) {
additionalEnvironment = {};
}
// Replace environment and configuration variables.
let regexp: () => RegExp = () => /\$\{((env|config|workspaceFolder)(\.|:))?(.*?)\}/g;
let ret: string = input;
let cycleCache: Set<string> = new Set();
while (!cycleCache.has(ret)) {
cycleCache.add(ret);
ret = ret.replace(regexp(), (match: string, ignored1: string, varType: string, ignored2: string, name: string) => {
// Historically, if the variable didn't have anything before the "." or ":"
// it was assumed to be an environment variable
if (varType === undefined) {
varType = "env";
}
let newValue: string;
switch (varType) {
case "env": {
let v: string | string[] = additionalEnvironment[name];
if (isString(v)) {
newValue = v;
} else if (input === match && isArrayOfString(v)) {
newValue = v.join(";");
}
if (!isString(newValue)) {
newValue = process.env[name];
}
break;
}
case "config": {
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration();
if (config) {
newValue = config.get<string>(name);
}
break;
}
case "workspaceFolder": {
// Only replace ${workspaceFolder:name} variables for now.
// We may consider doing replacement of ${workspaceFolder} here later, but we would have to update the language server and also
// intercept messages with paths in them and add the ${workspaceFolder} variable back in (e.g. for light bulb suggestions)
if (name && vscode.workspace && vscode.workspace.workspaceFolders) {
let folder: vscode.WorkspaceFolder = vscode.workspace.workspaceFolders.find(folder => folder.name.toLocaleLowerCase() === name.toLocaleLowerCase());
if (folder) {
newValue = folder.uri.fsPath;
}
}
break;
}
default: { assert.fail("unknown varType matched"); }
}
return (isString(newValue)) ? newValue : match;
});
}
// Resolve '~' at the start of the path.
regexp = () => /^\~/g;
ret = ret.replace(regexp(), (match: string, name: string) => {
let newValue: string = (process.platform === 'win32') ? process.env.USERPROFILE : process.env.HOME;
return (newValue) ? newValue : match;
});
return ret;
}
export function asFolder(uri: vscode.Uri): string {
let result: string = uri.toString();
if (result.charAt(result.length - 1) !== '/') {
result += '/';
}
return result;
}
/**
* get the default open command for the current platform
*/
export function getOpenCommand(): string {
if (os.platform() === 'win32') {
return 'explorer';
} else if (os.platform() === 'darwin') {
return '/usr/bin/open';
} else {
return '/usr/bin/xdg-open';
}
}
export function getDebugAdaptersPath(file: string): string {
return path.resolve(getExtensionFilePath("debugAdapters"), file);
}
export function getHttpsProxyAgent(): HttpsProxyAgent {
let proxy: string = vscode.workspace.getConfiguration().get<string>('http.proxy');
if (!proxy) {
proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy;
if (!proxy) {
return null; // No proxy
}
}
// Basic sanity checking on proxy url
let proxyUrl: any = url.parse(proxy);
if (proxyUrl.protocol !== "https:" && proxyUrl.protocol !== "http:") {
return null;
}
let strictProxy: any = vscode.workspace.getConfiguration().get("http.proxyStrictSSL", true);
let proxyOptions: any = {
host: proxyUrl.hostname,
port: parseInt(proxyUrl.port, 10),
auth: proxyUrl.auth,
rejectUnauthorized: strictProxy
};
return new HttpsProxyAgent(proxyOptions);
}
/** Creates a file if it doesn't exist */
function touchFile(file: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.writeFile(file, "", (err) => {
if (err) {
reject(err);
}
resolve();
});
});
}
export function touchInstallLockFile(): Promise<void> {
return touchFile(getInstallLockPath());
}
export function touchExtensionFolder(): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.utimes(path.resolve(extensionPath, ".."), new Date(Date.now()), new Date(Date.now()), (err) => {
if (err) {
reject(err);
}
resolve();
});
});
}
/** Test whether a file exists */
export function checkFileExists(filePath: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if (stats && stats.isFile()) {
resolve(true);
} else {
resolve(false);
}
});
});
}
/** Test whether a directory exists */
export function checkDirectoryExists(dirPath: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.stat(dirPath, (err, stats) => {
if (stats && stats.isDirectory()) {
resolve(true);
} else {
resolve(false);
}
});
});
}
export function checkFileExistsSync(filePath: string): boolean {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
}
return false;
}
/** Test whether a directory exists */
export function checkDirectoryExistsSync(dirPath: string): boolean {
try {
return fs.statSync(dirPath).isDirectory();
} catch (e) {
}
return false;
}
/** Read the files in a directory */
export function readDir(dirPath: string): Promise<string[]> {
return new Promise((resolve) => {
fs.readdir(dirPath, (err, list) => {
resolve(list);
});
});
}
/** Test whether the lock file exists.*/
export function checkInstallLockFile(): Promise<boolean> {
return checkFileExists(getInstallLockPath());
}
/** Reads the content of a text file */
export function readFileText(filePath: string, encoding: string = "utf8"): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.readFile(filePath, encoding, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
/** Writes content to a text file */
export function writeFileText(filePath: string, content: string, encoding: string = "utf8"): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.writeFile(filePath, content, { encoding }, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
export function deleteFile(filePath: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (fs.existsSync(filePath)) {
fs.unlink(filePath, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
resolve();
}
});
}
// Get the path of the lock file. This is used to indicate that the platform-specific dependencies have been downloaded.
export function getInstallLockPath(): string {
return getExtensionFilePath("install.lock");
}
export function getReadmeMessage(): string {
const readmePath: string = getExtensionFilePath("README.md");
const readmeMessage: string = localize("refer.read.me", "Please refer to {0} for troubleshooting information. Issues can be created at {1}", readmePath, "https://github.com/Microsoft/vscode-cpptools/issues");
return readmeMessage;
}
/** Used for diagnostics only */
export function logToFile(message: string): void {
const logFolder: string = getExtensionFilePath("extension.log");
fs.writeFileSync(logFolder, `${message}${os.EOL}`, { flag: 'a' });
}
export function execChildProcess(process: string, workingDirectory: string, channel?: vscode.OutputChannel): Promise<string> {
return new Promise<string>((resolve, reject) => {
child_process.exec(process, { cwd: workingDirectory, maxBuffer: 500 * 1024 }, (error: Error, stdout: string, stderr: string) => {
if (channel) {
let message: string = "";
let err: Boolean = false;
if (stdout && stdout.length > 0) {
message += stdout;
}
if (stderr && stderr.length > 0) {
message += stderr;
err = true;
}
if (error) {
message += error.message;
err = true;
}
if (err) {
channel.append(message);
channel.show();
}
}
if (error) {
reject(error);
return;
}
if (stderr && stderr.length > 0) {
reject(new Error(stderr));
return;
}
resolve(stdout);
});
});
}
export function spawnChildProcess(process: string, args: string[], workingDirectory: string,
dataCallback: (stdout: string) => void, errorCallback: (stderr: string) => void): Promise<void> {
return new Promise<void>(function (resolve, reject): void {
const child: child_process.ChildProcess = child_process.spawn(process, args, { cwd: workingDirectory });
child.stdout.on('data', (data) => {
dataCallback(`${data}`);
});
child.stderr.on('data', (data) => {
errorCallback(`${data}`);
});
child.on('exit', (code: number) => {
if (code !== 0) {
reject(new Error(localize("process.exited.with.code", "{0} exited with error code {1}", process, code)));
} else {
resolve();
}
});
});
}
export function allowExecution(file: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
if (process.platform !== 'win32') {
checkFileExists(file).then((exists: boolean) => {
if (exists) {
fs.chmod(file, '755', (err: NodeJS.ErrnoException) => {
if (err) {
reject(err);
return;
}
resolve();
});
} else {
getOutputChannelLogger().appendLine("");
getOutputChannelLogger().appendLine(localize("warning.file.missing", "Warning: Expected file {0} is missing.", file));
resolve();
}
});
} else {
resolve();
}
});
}
export function removePotentialPII(str: string): string {
let words: string[] = str.split(" ");
let result: string = "";
for (let word of words) {
if (word.indexOf(".") === -1 && word.indexOf("/") === -1 && word.indexOf("\\") === -1 && word.indexOf(":") === -1) {
result += word + " ";
} else {
result += "? ";
}
}
return result;
}
export function checkDistro(platformInfo: PlatformInformation): void {
if (platformInfo.platform !== 'win32' && platformInfo.platform !== 'linux' && platformInfo.platform !== 'darwin') {
// this should never happen because VSCode doesn't run on FreeBSD
// or SunOS (the other platforms supported by node)
getOutputChannelLogger().appendLine(localize("warning.debugging.not.tested", "Warning: Debugging has not been tested for this platform.") + " " + getReadmeMessage());
}
}
export async function unlinkPromise(fileName: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.unlink(fileName, err => {
if (err) {
return reject(err);
}
return resolve();
});
});
}
export async function renamePromise(oldName: string, newName: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.rename(oldName, newName, err => {
if (err) {
return reject(err);
}
return resolve();
});
});
}
export function promptForReloadWindowDueToSettingsChange(): void {
promptReloadWindow(localize("reload.workspace.for.changes", "Reload the workspace for the settings change to take effect."));
}
export function promptReloadWindow(message: string): void {
let reload: string = localize("reload.string", "Reload");
vscode.window.showInformationMessage(message, reload).then((value: string) => {
if (value === reload) {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
});
}
export function downloadFileToDestination(urlStr: string, destinationPath: string, headers?: OutgoingHttpHeaders): Promise<void> {
return new Promise<void>((resolve, reject) => {
let parsedUrl: url.Url = url.parse(urlStr);
let request: ClientRequest = https.request({
host: parsedUrl.host,
path: parsedUrl.path,
agent: getHttpsProxyAgent(),
rejectUnauthorized: vscode.workspace.getConfiguration().get('http.proxyStrictSSL', true),
headers: headers
}, (response) => {
if (response.statusCode === 301 || response.statusCode === 302) { // If redirected
// Download from new location
let redirectUrl: string;
if (typeof response.headers.location === 'string') {
redirectUrl = response.headers.location;
} else {
redirectUrl = response.headers.location[0];
}
return resolve(downloadFileToDestination(redirectUrl, destinationPath, headers));
}
if (response.statusCode !== 200) { // If request is not successful
return reject();
}
// Write file using downloaded data
let createdFile: fs.WriteStream = fs.createWriteStream(destinationPath);
createdFile.on('finish', () => { resolve(); });
response.on('error', (error) => { reject(error); });
response.pipe(createdFile);
});
request.on('error', (error) => { reject(error); });
request.end();
});
}
export function downloadFileToStr(urlStr: string, headers?: OutgoingHttpHeaders): Promise<any> {
return new Promise<string>((resolve, reject) => {
let parsedUrl: url.Url = url.parse(urlStr);
let request: ClientRequest = https.request({
host: parsedUrl.host,
path: parsedUrl.path,
agent: getHttpsProxyAgent(),
rejectUnauthorized: vscode.workspace.getConfiguration().get('http.proxyStrictSSL', true),
headers: headers
}, (response) => {
if (response.statusCode === 301 || response.statusCode === 302) { // If redirected
// Download from new location
let redirectUrl: string;
if (typeof response.headers.location === 'string') {
redirectUrl = response.headers.location;
} else {
redirectUrl = response.headers.location[0];
}
return resolve(downloadFileToStr(redirectUrl, headers));
}
if (response.statusCode !== 200) { // If request is not successful
return reject();
}
let downloadedData: string = '';
response.on('data', (data) => { downloadedData += data; });
response.on('error', (error) => { reject(error); });
response.on('end', () => { resolve(downloadedData); });
});
request.on('error', (error) => { reject(error); });
request.end();
});
}
export interface CompilerPathAndArgs {
compilerPath: string;
compilerName: string;
additionalArgs: string[];
}
export function extractCompilerPathAndArgs(inputCompilerPath: string, inputCompilerArgs?: string[]): CompilerPathAndArgs {
let compilerPath: string = inputCompilerPath;
let compilerName: string = "";
let additionalArgs: string[] = [];
let isWindows: boolean = os.platform() === 'win32';
if (compilerPath) {
if (compilerPath === "cl.exe") {
// Input is only compiler name, this is only for cl.exe
compilerName = compilerPath;
} else if (compilerPath.startsWith("\"")) {
// Input has quotes around compiler path
let endQuote: number = compilerPath.substr(1).search("\"") + 1;
if (endQuote !== -1) {
additionalArgs = compilerPath.substr(endQuote + 1).split(" ");
additionalArgs = additionalArgs.filter((arg: string) => arg.trim().length !== 0); // Remove empty args.
compilerPath = compilerPath.substr(1, endQuote - 1);
compilerName = path.basename(compilerPath);
}
} else {
// Input has no quotes but can have a compiler path with spaces and args.
// Go from right to left checking if a valid path is to the left of a space.
let spaceStart: number = compilerPath.lastIndexOf(" ");
if (spaceStart !== -1 && (!isWindows || !compilerPath.endsWith("cl.exe")) && !checkFileExistsSync(compilerPath)) {
let potentialCompilerPath: string = compilerPath.substr(0, spaceStart);
while ((!isWindows || !potentialCompilerPath.endsWith("cl.exe")) && !checkFileExistsSync(potentialCompilerPath)) {
spaceStart = potentialCompilerPath.lastIndexOf(" ");
if (spaceStart === -1) {
// Reached the start without finding a valid path. Use the original value.
potentialCompilerPath = compilerPath;
break;
}
potentialCompilerPath = potentialCompilerPath.substr(0, spaceStart);
}
if (compilerPath !== potentialCompilerPath) {
// Found a valid compilerPath and args.
additionalArgs = compilerPath.substr(spaceStart + 1).split(" ");
additionalArgs = additionalArgs.filter((arg: string) => arg.trim().length !== 0); // Remove empty args.
compilerPath = potentialCompilerPath;
}
}
// Get compiler name if there are no args but path is valid or a valid path was found with args.
if (compilerPath === "cl.exe" || checkFileExistsSync(compilerPath)) {
compilerName = path.basename(compilerPath);
}
}
}
// Combine args from inputCompilerPath and inputCompilerArgs and remove duplicates
if (inputCompilerArgs && inputCompilerArgs.length) {
additionalArgs = inputCompilerArgs.concat(additionalArgs.filter(
function (item: string): boolean {
return inputCompilerArgs.indexOf(item) < 0;
}));
}
return { compilerPath, compilerName, additionalArgs };
}
export function escapeForSquiggles(s: string): string {
// Replace all \<escape character> with \\<character>, except for \"
// Otherwise, the JSON.parse result will have the \<escape character> missing.
let newResults: string = "";
let lastWasBackslash: Boolean = false;
let lastBackslashWasEscaped: Boolean = false;
for (let i: number = 0; i < s.length; i++) {
if (s[i] === '\\') {
if (lastWasBackslash) {
newResults += "\\";
lastBackslashWasEscaped = !lastBackslashWasEscaped;
} else {
lastBackslashWasEscaped = false;
}
newResults += "\\";
lastWasBackslash = true;
} else {
if (lastWasBackslash && (lastBackslashWasEscaped || (s[i] !== '"'))) {
newResults += "\\";
}
lastWasBackslash = false;
lastBackslashWasEscaped = false;
newResults += s[i];
}
}
if (lastWasBackslash) {
newResults += "\\";
}
return newResults;
}
export class BlockingTask<T> {
private dependency: BlockingTask<any>;
private done: boolean = false;
private promise: Thenable<T>;
constructor(task: () => Thenable<T>, dependency?: BlockingTask<any>) {
if (!dependency) {
this.promise = task();
} else {
this.dependency = dependency;
this.promise = new Promise<T>((resolve, reject) => {
let f1: () => void = () => {
task().then(resolve, reject);
};
let f2: (err: any) => void = (err) => {
console.log(err);
task().then(resolve, reject);
};
this.dependency.promise.then(f1, f2);
});
}
this.promise.then(() => this.done = true, () => this.done = true);
}
public get Done(): boolean {
return this.done;
}
public getPromise(): Thenable<T> {
return this.promise;
}
}
interface VSCodeNlsConfig {
locale: string;
availableLanguages: {
[pack: string]: string;
};
}
export function getLocaleId(): string {
// This replicates the language detection used by initializeSettings() in vscode-nls
if (isString(process.env.VSCODE_NLS_CONFIG)) {
let vscodeOptions: VSCodeNlsConfig = JSON.parse(process.env.VSCODE_NLS_CONFIG) as VSCodeNlsConfig;
if (vscodeOptions.availableLanguages) {
let value: any = vscodeOptions.availableLanguages['*'];
if (isString(value)) {
return value;
}
}
if (isString(vscodeOptions.locale)) {
return vscodeOptions.locale.toLowerCase();
}
}
return "en";
}
export function getLocalizedHtmlPath(originalPath: string): string {
let locale: string = getLocaleId();
let localizedFilePath: string = getExtensionFilePath(path.join("dist/html/", locale, originalPath));
if (!fs.existsSync(localizedFilePath)) {
return getExtensionFilePath(originalPath);
}
return localizedFilePath;
}
export interface LocalizeStringParams {
text: string;
stringId: number;
stringArgs: string[];
indentSpaces: number;
}
export function getLocalizedString(params: LocalizeStringParams): string {
let indent: string = "";
if (params.indentSpaces) {
indent = " ".repeat(params.indentSpaces);
}
let text: string = params.text;
if (params.stringId !== 0) {
text = lookupString(params.stringId, params.stringArgs);
}
return indent + text;
}
function decodeUCS16(input: string): number[] {
let output: number[] = [];
let counter: number = 0;
let length: number = input.length;
let value: number;
let extra: number;
while (counter < length) {
value = input.charCodeAt(counter++);
// tslint:disable-next-line: no-bitwise
if ((value & 0xF800) === 0xD800 && counter < length) {
// high surrogate, and there is a next character
extra = input.charCodeAt(counter++);
// tslint:disable-next-line: no-bitwise
if ((extra & 0xFC00) === 0xDC00) { // low surrogate
// tslint:disable-next-line: no-bitwise
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
} else {
output.push(value, extra);
}
} else {
output.push(value);
}
}
return output;
}
let allowedIdentifierUnicodeRanges: number[][] = [
[0x0030, 0x0039], // digits
[0x0041, 0x005A], // upper case letters
[0x005F, 0x005F], // underscore
[0x0061, 0x007A], // lower case letters
[0x00A8, 0x00A8], // DIARESIS
[0x00AA, 0x00AA], // FEMININE ORDINAL INDICATOR
[0x00AD, 0x00AD], // SOFT HYPHEN
[0x00AF, 0x00AF], // MACRON
[0x00B2, 0x00B5], // SUPERSCRIPT TWO - MICRO SIGN
[0x00B7, 0x00BA], // MIDDLE DOT - MASCULINE ORDINAL INDICATOR
[0x00BC, 0x00BE], // VULGAR FRACTION ONE QUARTER - VULGAR FRACTION THREE QUARTERS
[0x00C0, 0x00D6], // LATIN CAPITAL LETTER A WITH GRAVE - LATIN CAPITAL LETTER O WITH DIAERESIS
[0x00D8, 0x00F6], // LATIN CAPITAL LETTER O WITH STROKE - LATIN SMALL LETTER O WITH DIAERESIS