forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsourceMapSupport.ts
More file actions
76 lines (71 loc) · 2.98 KB
/
sourceMapSupport.ts
File metadata and controls
76 lines (71 loc) · 2.98 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { WorkspaceConfiguration } from 'vscode';
import './common/extensions';
import { EXTENSION_ROOT_DIR } from './constants';
type VSCode = typeof import('vscode');
// tslint:disable:no-require-imports
const setting = 'sourceMapsEnabled';
export class SourceMapSupport {
private readonly config: WorkspaceConfiguration;
constructor(private readonly vscode: VSCode) {
this.config = this.vscode.workspace.getConfiguration('python.diagnostics', null);
}
public async initialize(): Promise<void> {
if (!this.enabled) {
return;
}
await this.enableSourceMaps(true);
const localize = require('./common/utils/localize') as typeof import('./common/utils/localize');
const disable = localize.Diagnostics.disableSourceMaps();
this.vscode.window.showWarningMessage(localize.Diagnostics.warnSourceMaps(), disable).then(selection => {
if (selection === disable) {
this.disable().ignoreErrors();
}
});
}
public get enabled(): boolean {
return this.config.get<boolean>(setting, false);
}
public async disable(): Promise<void> {
if (this.enabled) {
await this.config.update(setting, false, this.vscode.ConfigurationTarget.Global);
}
await this.enableSourceMaps(false);
}
protected async enableSourceMaps(enable: boolean) {
const extensionSourceFile = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'extension.js');
const debuggerSourceFile = path.join(EXTENSION_ROOT_DIR, 'out', 'client', 'debugger', 'debugAdapter', 'main.js');
await Promise.all([this.enableSourceMap(enable, extensionSourceFile), this.enableSourceMap(enable, debuggerSourceFile)]);
}
protected async enableSourceMap(enable: boolean, sourceFile: string) {
const sourceMapFile = `${sourceFile}.map`;
const disabledSourceMapFile = `${sourceFile}.map.disabled`;
if (enable) {
await this.rename(disabledSourceMapFile, sourceMapFile);
} else {
await this.rename(sourceMapFile, disabledSourceMapFile);
}
}
protected async rename(sourceFile: string, targetFile: string) {
const fsExists = promisify(fs.exists);
const fsRename = promisify(fs.rename);
if (await fsExists(targetFile)) {
return;
}
await fsRename(sourceFile, targetFile);
}
}
export function initialize(vscode: VSCode = require('vscode')) {
if (!vscode.workspace.getConfiguration('python.diagnostics', null).get('sourceMapsEnabled', false)) {
new SourceMapSupport(vscode).disable().ignoreErrors();
return;
}
new SourceMapSupport(vscode).initialize().catch(_ex => {
console.error('Failed to initialize source map support in extension');
});
}