|
| 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 { DiagnosticSeverity } from 'vscode'; |
| 8 | +import '../../../common/extensions'; |
| 9 | +import { error } from '../../../common/logger'; |
| 10 | +import { useCommandPromptAsDefaultShell } from '../../../common/terminal/commandPrompt'; |
| 11 | +import { IConfigurationService, ICurrentProcess } from '../../../common/types'; |
| 12 | +import { IServiceContainer } from '../../../ioc/types'; |
| 13 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 14 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 15 | +import { DiagnosticCodes } from '../constants'; |
| 16 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 17 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 18 | + |
| 19 | +const PowershellActivationNotSupportedWithBatchFilesMessage = 'Activation of the selected Python environment is not supported in PowerShell. Consider changing your shell to Command Prompt.'; |
| 20 | + |
| 21 | +export class PowershellActivationNotAvailableDiagnostic extends BaseDiagnostic { |
| 22 | + constructor() { |
| 23 | + super(DiagnosticCodes.EnvironmentActivationInPowerShellWithBatchFilesNotSupportedDiagnostic, |
| 24 | + PowershellActivationNotSupportedWithBatchFilesMessage, |
| 25 | + DiagnosticSeverity.Warning, DiagnosticScope.Global); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export const PowerShellActivationHackDiagnosticsServiceId = 'EnvironmentActivationInPowerShellWithBatchFilesNotSupportedDiagnostic'; |
| 30 | + |
| 31 | +@injectable() |
| 32 | +export class PowerShellActivationHackDiagnosticsService extends BaseDiagnosticsService { |
| 33 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>; |
| 34 | + constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { |
| 35 | + super([DiagnosticCodes.EnvironmentActivationInPowerShellWithBatchFilesNotSupportedDiagnostic], serviceContainer); |
| 36 | + this.messageService = serviceContainer.get<IDiagnosticHandlerService<MessageCommandPrompt>>(IDiagnosticHandlerService, DiagnosticCommandPromptHandlerServiceId); |
| 37 | + } |
| 38 | + public async diagnose(): Promise<IDiagnostic[]> { |
| 39 | + return []; |
| 40 | + } |
| 41 | + public async handle(diagnostics: IDiagnostic[]): Promise<void> { |
| 42 | + // This class can only handle one type of diagnostic, hence just use first item in list. |
| 43 | + if (diagnostics.length === 0 || !this.canHandle(diagnostics[0])) { |
| 44 | + return; |
| 45 | + } |
| 46 | + const diagnostic = diagnostics[0]; |
| 47 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 51 | + const currentProcess = this.serviceContainer.get<ICurrentProcess>(ICurrentProcess); |
| 52 | + const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService); |
| 53 | + const options = [ |
| 54 | + { |
| 55 | + prompt: 'Use Command Prompt', |
| 56 | + // tslint:disable-next-line:no-object-literal-type-assertion |
| 57 | + command: { |
| 58 | + diagnostic, invoke: async (): Promise<void> => { |
| 59 | + useCommandPromptAsDefaultShell(currentProcess, configurationService) |
| 60 | + .catch(ex => error('Use Command Prompt as default shell', ex)); |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + { |
| 65 | + prompt: 'Ignore' |
| 66 | + }, |
| 67 | + { |
| 68 | + prompt: 'Always Ignore', |
| 69 | + command: commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }) |
| 70 | + }, |
| 71 | + { |
| 72 | + prompt: 'More Info', |
| 73 | + command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://aka.ms/Niq35h' }) |
| 74 | + } |
| 75 | + ]; |
| 76 | + |
| 77 | + await this.messageService.handle(diagnostic, { commandPrompts: options }); |
| 78 | + } |
| 79 | +} |
0 commit comments