|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import * as path from "path"; |
5 | | -import * as fs from "fs-extra"; |
6 | | -import { parse } from "jsonc-parser"; |
7 | | -import { DebugConfiguration, Uri, WorkspaceFolder } from "vscode"; |
8 | | - |
9 | | -import { traceLog } from "../../../common/log/logging"; |
10 | | -import { |
11 | | - getConfiguration, |
12 | | - getWorkspaceFolder, |
13 | | -} from "../../../common/vscodeapi"; |
14 | | - |
15 | | -export async function getConfigurationsForWorkspace( |
16 | | - workspace: WorkspaceFolder, |
17 | | -): Promise<DebugConfiguration[]> { |
18 | | - traceLog("Getting configurations for workspace"); |
19 | | - |
20 | | - const filename = path.join(workspace.uri.fsPath, ".vscode", "launch.json"); |
21 | | - |
22 | | - if (!(await fs.pathExists(filename))) { |
23 | | - // Check launch config in the workspace file |
24 | | - const codeWorkspaceConfig = getConfiguration("launch", workspace); |
25 | | - |
26 | | - if ( |
27 | | - !codeWorkspaceConfig.configurations || |
28 | | - !Array.isArray(codeWorkspaceConfig.configurations) |
29 | | - ) { |
30 | | - return []; |
31 | | - } |
32 | | - |
33 | | - traceLog("Using configuration in workspace"); |
34 | | - |
35 | | - return codeWorkspaceConfig.configurations; |
36 | | - } |
37 | | - |
38 | | - const text = await fs.readFile(filename, "utf-8"); |
39 | | - |
40 | | - const parsed = parse(text, [], { |
41 | | - allowTrailingComma: true, |
42 | | - disallowComments: false, |
43 | | - }); |
44 | | - |
45 | | - if (!parsed.configurations || !Array.isArray(parsed.configurations)) { |
46 | | - throw Error("Missing field in launch.json: configurations"); |
47 | | - } |
48 | | - |
49 | | - if (!parsed.version) { |
50 | | - throw Error("Missing field in launch.json: version"); |
51 | | - } |
52 | | - |
53 | | - traceLog("Using configuration in launch.json"); |
54 | | - |
55 | | - return parsed.configurations; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as fs from 'fs-extra'; |
| 6 | +import { parse } from 'jsonc-parser'; |
| 7 | +import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode'; |
| 8 | +import { getConfiguration, getWorkspaceFolder } from '../../../common/vscodeapi'; |
| 9 | +import { traceLog } from '../../../common/log/logging'; |
| 10 | + |
| 11 | +export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> { |
| 12 | + traceLog('Getting configurations for workspace'); |
| 13 | + const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json'); |
| 14 | + if (!(await fs.pathExists(filename))) { |
| 15 | + return getConfigurationsFromSettings(workspace); |
| 16 | + } |
| 17 | + const text = await fs.readFile(filename, 'utf-8'); |
| 18 | + const parsed = parse(text, [], { allowTrailingComma: true, disallowComments: false }); |
| 19 | + // no launch.json or no configurations found in launch.json, look in settings.json |
| 20 | + if (!parsed || !parsed.configurations) { |
| 21 | + traceLog('No configurations found in launch.json, looking in settings.json.'); |
| 22 | + return getConfigurationsFromSettings(workspace); |
| 23 | + } |
| 24 | + // configurations found in launch.json, verify them then return |
| 25 | + if (!Array.isArray(parsed.configurations) || parsed.configurations.length === 0) { |
| 26 | + throw Error('Invalid configurations in launch.json'); |
| 27 | + } |
| 28 | + if (!parsed.version) { |
| 29 | + throw Error('Missing field in launch.json: version'); |
| 30 | + } |
| 31 | + traceLog('Using configuration in launch.json'); |
| 32 | + return parsed.configurations; |
56 | 33 | } |
57 | 34 |
|
58 | | -export async function getConfigurationsByUri( |
59 | | - uri?: Uri, |
60 | | -): Promise<DebugConfiguration[]> { |
61 | | - if (uri) { |
62 | | - const workspace = getWorkspaceFolder(uri); |
63 | | - |
64 | | - if (workspace) { |
65 | | - return getConfigurationsForWorkspace(workspace); |
66 | | - } |
67 | | - } |
| 35 | +export async function getConfigurationsByUri(uri?: Uri): Promise<DebugConfiguration[]> { |
| 36 | + if (uri) { |
| 37 | + const workspace = getWorkspaceFolder(uri); |
| 38 | + if (workspace) { |
| 39 | + return getConfigurationsForWorkspace(workspace); |
| 40 | + } |
| 41 | + } |
| 42 | + return []; |
| 43 | +} |
68 | 44 |
|
69 | | - return []; |
| 45 | +export function getConfigurationsFromSettings(workspace: WorkspaceFolder): DebugConfiguration[] { |
| 46 | + // look in settings.json |
| 47 | + const codeWorkspaceConfig = getConfiguration('launch', workspace); |
| 48 | + // if this includes user configs, how do I make sure it selects the workspace ones first |
| 49 | + if ( |
| 50 | + !codeWorkspaceConfig.configurations || |
| 51 | + !Array.isArray(codeWorkspaceConfig.configurations) || |
| 52 | + codeWorkspaceConfig.configurations.length === 0 |
| 53 | + ) { |
| 54 | + throw Error('No configurations found in launch.json or settings.json'); |
| 55 | + } |
| 56 | + traceLog('Using configuration in workspace settings.json.'); |
| 57 | + return codeWorkspaceConfig.configurations; |
70 | 58 | } |
0 commit comments