|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable } from 'inversify'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { WorkspaceFolder } from 'vscode'; |
| 9 | +import { IFileSystem } from '../../../../common/platform/types'; |
| 10 | +import { DebugConfigStrings } from '../../../../common/utils/localize'; |
| 11 | +import { MultiStepInput } from '../../../../common/utils/multiStepInput'; |
| 12 | +import { sendTelemetryEvent } from '../../../../telemetry'; |
| 13 | +import { EventName } from '../../../../telemetry/constants'; |
| 14 | +import { DebuggerTypeName } from '../../../constants'; |
| 15 | +import { LaunchRequestArguments } from '../../../types'; |
| 16 | +import { DebugConfigurationState, DebugConfigurationType, IDebugConfigurationProvider } from '../../types'; |
| 17 | + |
| 18 | +@injectable() |
| 19 | +export class FastAPILaunchDebugConfigurationProvider implements IDebugConfigurationProvider { |
| 20 | + constructor(@inject(IFileSystem) private fs: IFileSystem) {} |
| 21 | + public isSupported(debugConfigurationType: DebugConfigurationType): boolean { |
| 22 | + return debugConfigurationType === DebugConfigurationType.launchFastAPI; |
| 23 | + } |
| 24 | + public async buildConfiguration(input: MultiStepInput<DebugConfigurationState>, state: DebugConfigurationState) { |
| 25 | + const application = await this.getApplicationPath(state.folder); |
| 26 | + let manuallyEnteredAValue: boolean | undefined; |
| 27 | + const config: Partial<LaunchRequestArguments> = { |
| 28 | + name: DebugConfigStrings.fastapi.snippet.name(), |
| 29 | + type: DebuggerTypeName, |
| 30 | + request: 'launch', |
| 31 | + module: 'uvicorn', |
| 32 | + args: ['main:app'], |
| 33 | + jinja: true |
| 34 | + }; |
| 35 | + |
| 36 | + if (!application) { |
| 37 | + const selectedPath = await input.showInputBox({ |
| 38 | + title: DebugConfigStrings.fastapi.enterAppPathOrNamePath.title(), |
| 39 | + value: 'main.py', |
| 40 | + prompt: DebugConfigStrings.fastapi.enterAppPathOrNamePath.prompt(), |
| 41 | + validate: (value) => |
| 42 | + Promise.resolve( |
| 43 | + value && value.trim().length > 0 |
| 44 | + ? undefined |
| 45 | + : DebugConfigStrings.fastapi.enterAppPathOrNamePath.invalid() |
| 46 | + ) |
| 47 | + }); |
| 48 | + if (selectedPath) { |
| 49 | + manuallyEnteredAValue = true; |
| 50 | + config.args = [`${path.basename(selectedPath, '.py').replace('/', '.')}:app`]; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, { |
| 55 | + configurationType: DebugConfigurationType.launchFastAPI, |
| 56 | + autoDetectedFastAPIMainPyPath: !!application, |
| 57 | + manuallyEnteredAValue |
| 58 | + }); |
| 59 | + Object.assign(state.config, config); |
| 60 | + } |
| 61 | + protected async getApplicationPath(folder: WorkspaceFolder | undefined): Promise<string | undefined> { |
| 62 | + if (!folder) { |
| 63 | + return; |
| 64 | + } |
| 65 | + const defaultLocationOfManagePy = path.join(folder.uri.fsPath, 'main.py'); |
| 66 | + if (await this.fs.fileExists(defaultLocationOfManagePy)) { |
| 67 | + return 'main.py'; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments