|
3 | 3 |
|
4 | 4 | 'use strict';
|
5 | 5 |
|
6 |
| -import { inject, injectable } from 'inversify'; |
| 6 | +import * as vscode from 'vscode'; |
7 | 7 | import * as path from 'path';
|
8 |
| -import { Uri, WorkspaceFolder } from 'vscode'; |
9 |
| -import { IWorkspaceService } from '../../../../common/application/types'; |
10 |
| -import { IFileSystem } from '../../../../common/platform/types'; |
11 |
| -import { IPathUtils } from '../../../../common/types'; |
| 8 | +import * as fs from 'fs-extra'; |
12 | 9 | import { DebugConfigStrings } from '../../../../common/utils/localize';
|
13 | 10 | import { MultiStepInput } from '../../../../common/utils/multiStepInput';
|
14 |
| -import { SystemVariables } from '../../../../common/variables/systemVariables'; |
15 | 11 | import { sendTelemetryEvent } from '../../../../telemetry';
|
16 | 12 | import { EventName } from '../../../../telemetry/constants';
|
17 | 13 | import { DebuggerTypeName } from '../../../constants';
|
18 | 14 | import { LaunchRequestArguments } from '../../../types';
|
19 |
| -import { DebugConfigurationState, DebugConfigurationType, IDebugConfigurationProvider } from '../../types'; |
| 15 | +import { DebugConfigurationState, DebugConfigurationType } from '../../types'; |
| 16 | +import { resolveVariables } from '../utils/common'; |
20 | 17 |
|
21 | 18 | const workspaceFolderToken = '${workspaceFolder}';
|
22 | 19 |
|
23 |
| -@injectable() |
24 |
| -export class DjangoLaunchDebugConfigurationProvider implements IDebugConfigurationProvider { |
25 |
| - constructor( |
26 |
| - @inject(IFileSystem) private fs: IFileSystem, |
27 |
| - @inject(IWorkspaceService) private readonly workspace: IWorkspaceService, |
28 |
| - @inject(IPathUtils) private pathUtils: IPathUtils, |
29 |
| - ) {} |
30 |
| - public async buildConfiguration(input: MultiStepInput<DebugConfigurationState>, state: DebugConfigurationState) { |
31 |
| - const program = await this.getManagePyPath(state.folder); |
32 |
| - let manuallyEnteredAValue: boolean | undefined; |
33 |
| - const defaultProgram = `${workspaceFolderToken}${this.pathUtils.separator}manage.py`; |
34 |
| - const config: Partial<LaunchRequestArguments> = { |
35 |
| - name: DebugConfigStrings.django.snippet.name, |
36 |
| - type: DebuggerTypeName, |
37 |
| - request: 'launch', |
38 |
| - program: program || defaultProgram, |
39 |
| - args: ['runserver'], |
40 |
| - django: true, |
41 |
| - justMyCode: true, |
42 |
| - }; |
43 |
| - if (!program) { |
44 |
| - const selectedProgram = await input.showInputBox({ |
45 |
| - title: DebugConfigStrings.django.enterManagePyPath.title, |
46 |
| - value: defaultProgram, |
47 |
| - prompt: DebugConfigStrings.django.enterManagePyPath.prompt, |
48 |
| - validate: (value) => this.validateManagePy(state.folder, defaultProgram, value), |
49 |
| - }); |
50 |
| - if (selectedProgram) { |
51 |
| - manuallyEnteredAValue = true; |
52 |
| - config.program = selectedProgram; |
53 |
| - } |
| 20 | +export async function buildDjangoLaunchDebugConfiguration( |
| 21 | + input: MultiStepInput<DebugConfigurationState>, |
| 22 | + state: DebugConfigurationState, |
| 23 | +) { |
| 24 | + const program = await getManagePyPath(state.folder); |
| 25 | + let manuallyEnteredAValue: boolean | undefined; |
| 26 | + const defaultProgram = `${workspaceFolderToken}${path.sep}manage.py`; |
| 27 | + const config: Partial<LaunchRequestArguments> = { |
| 28 | + name: DebugConfigStrings.django.snippet.name, |
| 29 | + type: DebuggerTypeName, |
| 30 | + request: 'launch', |
| 31 | + program: program || defaultProgram, |
| 32 | + args: ['runserver'], |
| 33 | + django: true, |
| 34 | + justMyCode: true, |
| 35 | + }; |
| 36 | + if (!program) { |
| 37 | + const selectedProgram = await input.showInputBox({ |
| 38 | + title: DebugConfigStrings.django.enterManagePyPath.title, |
| 39 | + value: defaultProgram, |
| 40 | + prompt: DebugConfigStrings.django.enterManagePyPath.prompt, |
| 41 | + validate: (value) => validateManagePy(state.folder, defaultProgram, value), |
| 42 | + }); |
| 43 | + if (selectedProgram) { |
| 44 | + manuallyEnteredAValue = true; |
| 45 | + config.program = selectedProgram; |
54 | 46 | }
|
| 47 | + } |
55 | 48 |
|
56 |
| - sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, { |
57 |
| - configurationType: DebugConfigurationType.launchDjango, |
58 |
| - autoDetectedDjangoManagePyPath: !!program, |
59 |
| - manuallyEnteredAValue, |
60 |
| - }); |
61 |
| - Object.assign(state.config, config); |
| 49 | + sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, { |
| 50 | + configurationType: DebugConfigurationType.launchDjango, |
| 51 | + autoDetectedDjangoManagePyPath: !!program, |
| 52 | + manuallyEnteredAValue, |
| 53 | + }); |
| 54 | + |
| 55 | + Object.assign(state.config, config); |
| 56 | +} |
| 57 | + |
| 58 | +export async function validateManagePy( |
| 59 | + folder: vscode.WorkspaceFolder | undefined, |
| 60 | + defaultValue: string, |
| 61 | + selected?: string, |
| 62 | +): Promise<string | undefined> { |
| 63 | + const error = DebugConfigStrings.django.enterManagePyPath.invalid; |
| 64 | + if (!selected || selected.trim().length === 0) { |
| 65 | + return error; |
62 | 66 | }
|
63 |
| - public async validateManagePy( |
64 |
| - folder: WorkspaceFolder | undefined, |
65 |
| - defaultValue: string, |
66 |
| - selected?: string, |
67 |
| - ): Promise<string | undefined> { |
68 |
| - const error = DebugConfigStrings.django.enterManagePyPath.invalid; |
69 |
| - if (!selected || selected.trim().length === 0) { |
70 |
| - return error; |
71 |
| - } |
72 |
| - const resolvedPath = this.resolveVariables(selected, folder ? folder.uri : undefined); |
73 |
| - if (selected !== defaultValue && !(await this.fs.fileExists(resolvedPath))) { |
74 |
| - return error; |
75 |
| - } |
76 |
| - if (!resolvedPath.trim().toLowerCase().endsWith('.py')) { |
77 |
| - return error; |
78 |
| - } |
79 |
| - return; |
| 67 | + const resolvedPath = resolveVariables(selected, undefined, folder); |
| 68 | + |
| 69 | + if (selected !== defaultValue && !(await fs.pathExists(resolvedPath))) { |
| 70 | + return error; |
80 | 71 | }
|
81 |
| - protected resolveVariables(pythonPath: string, resource: Uri | undefined): string { |
82 |
| - const systemVariables = new SystemVariables(resource, undefined, this.workspace); |
83 |
| - return systemVariables.resolveAny(pythonPath); |
| 72 | + if (!resolvedPath.trim().toLowerCase().endsWith('.py')) { |
| 73 | + return error; |
84 | 74 | }
|
| 75 | + return; |
| 76 | +} |
85 | 77 |
|
86 |
| - protected async getManagePyPath(folder: WorkspaceFolder | undefined): Promise<string | undefined> { |
87 |
| - if (!folder) { |
88 |
| - return; |
89 |
| - } |
90 |
| - const defaultLocationOfManagePy = path.join(folder.uri.fsPath, 'manage.py'); |
91 |
| - if (await this.fs.fileExists(defaultLocationOfManagePy)) { |
92 |
| - return `${workspaceFolderToken}${this.pathUtils.separator}manage.py`; |
93 |
| - } |
| 78 | +export async function getManagePyPath(folder: vscode.WorkspaceFolder | undefined): Promise<string | undefined> { |
| 79 | + if (!folder) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const defaultLocationOfManagePy = path.join(folder.uri.fsPath, 'manage.py'); |
| 83 | + if (await fs.pathExists(defaultLocationOfManagePy)) { |
| 84 | + return `${workspaceFolderToken}${path.sep}manage.py`; |
94 | 85 | }
|
95 | 86 | }
|
0 commit comments