forked from microsoft/vscode-python-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunchJsonReader.ts
More file actions
58 lines (54 loc) · 2.48 KB
/
launchJsonReader.ts
File metadata and controls
58 lines (54 loc) · 2.48 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import * as fs from 'fs-extra';
import { parse } from 'jsonc-parser';
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
import { getConfiguration, getWorkspaceFolder } from '../../../common/vscodeapi';
import { traceLog } from '../../../common/log/logging';
export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> {
traceLog('Getting configurations for workspace');
const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json');
if (!(await fs.pathExists(filename))) {
return getConfigurationsFromSettings(workspace);
}
const text = await fs.readFile(filename, 'utf-8');
const parsed = parse(text, [], { allowTrailingComma: true, disallowComments: false });
// no launch.json or no configurations found in launch.json, look in settings.json
if (!parsed || !parsed.configurations) {
traceLog('No configurations found in launch.json, looking in settings.json.');
return getConfigurationsFromSettings(workspace);
}
// configurations found in launch.json, verify them then return
if (!Array.isArray(parsed.configurations) || parsed.configurations.length === 0) {
throw Error('Invalid configurations in launch.json');
}
if (!parsed.version) {
throw Error('Missing field in launch.json: version');
}
traceLog('Using configuration in launch.json');
return parsed.configurations;
}
export async function getConfigurationsByUri(uri?: Uri): Promise<DebugConfiguration[]> {
if (uri) {
const workspace = getWorkspaceFolder(uri);
if (workspace) {
return getConfigurationsForWorkspace(workspace);
}
}
return [];
}
export function getConfigurationsFromSettings(workspace: WorkspaceFolder): DebugConfiguration[] {
// look in settings.json
const codeWorkspaceConfig = getConfiguration('launch', workspace);
// if this includes user configs, how do I make sure it selects the workspace ones first
if (
!codeWorkspaceConfig.configurations ||
!Array.isArray(codeWorkspaceConfig.configurations) ||
codeWorkspaceConfig.configurations.length === 0
) {
throw Error('No configurations found in launch.json or settings.json');
}
traceLog('Using configuration in workspace settings.json.');
return codeWorkspaceConfig.configurations;
}