|
| 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 { IPlatformService } from '../../../common/platform/types'; |
| 10 | +import { IConfigurationService } from '../../../common/types'; |
| 11 | +import { IInterpreterHelper, IInterpreterService, InterpreterType } from '../../../interpreter/contracts'; |
| 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, IDiagnosticCommand, IDiagnosticHandlerService } from '../types'; |
| 18 | + |
| 19 | +const messages = { |
| 20 | + [DiagnosticCodes.NoPythonInterpretersDiagnostic]: 'Python is not installed. Please download and install Python before using the extension.', |
| 21 | + [DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic]: 'You have selected the macOS system install of Python, which is not not recommended for use with the Python extension. Some functionality will be limited, please select a different interpreter.', |
| 22 | + [DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic]: 'The macOS system install of Python is not recommended, some functionality in the extension will be limited. Install another version of Python for the best experience.' |
| 23 | +}; |
| 24 | + |
| 25 | +export class InvalidPythonInterpreterDiagnostic extends BaseDiagnostic { |
| 26 | + constructor(code: DiagnosticCodes) { |
| 27 | + super(code, messages[code], DiagnosticSeverity.Error, DiagnosticScope.WorkspaceFolder); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const InvalidPythonInterpreterServiceId = 'InvalidPythonInterpreterServiceId'; |
| 32 | + |
| 33 | +@injectable() |
| 34 | +export class InvalidPythonInterpreterService extends BaseDiagnosticsService { |
| 35 | + constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { |
| 36 | + super([DiagnosticCodes.NoPythonInterpretersDiagnostic, |
| 37 | + DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic, |
| 38 | + DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic], serviceContainer); |
| 39 | + } |
| 40 | + public async diagnose(): Promise<IDiagnostic[]> { |
| 41 | + const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService); |
| 42 | + const settings = configurationService.getSettings(); |
| 43 | + if (settings.disableInstallationChecks === true) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + const interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService); |
| 48 | + const interpreters = await interpreterService.getInterpreters(); |
| 49 | + |
| 50 | + if (interpreters.length === 0) { |
| 51 | + return [new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.NoPythonInterpretersDiagnostic)]; |
| 52 | + } |
| 53 | + |
| 54 | + const platform = this.serviceContainer.get<IPlatformService>(IPlatformService); |
| 55 | + if (!platform.isMac) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + |
| 59 | + const helper = this.serviceContainer.get<IInterpreterHelper>(IInterpreterHelper); |
| 60 | + if (!helper.isMacDefaultPythonPath(settings.pythonPath)) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + const interpreter = await interpreterService.getActiveInterpreter(); |
| 64 | + if (!interpreter || interpreter.type !== InterpreterType.Unknown) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + if (interpreters.filter(i => !helper.isMacDefaultPythonPath(i.path)).length === 0) { |
| 68 | + return [new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic)]; |
| 69 | + } |
| 70 | + |
| 71 | + return [new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic)]; |
| 72 | + } |
| 73 | + public async handle(diagnostics: IDiagnostic[]): Promise<void> { |
| 74 | + if (diagnostics.length === 0) { |
| 75 | + return; |
| 76 | + } |
| 77 | + const messageService = this.serviceContainer.get<IDiagnosticHandlerService<MessageCommandPrompt>>(IDiagnosticHandlerService, DiagnosticCommandPromptHandlerServiceId); |
| 78 | + await Promise.all(diagnostics.map(async diagnostic => { |
| 79 | + if (!this.canHandle(diagnostic)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + const commandPrompts = this.getCommandPrompts(diagnostic); |
| 83 | + return messageService.handle(diagnostic, { commandPrompts, message: diagnostic.message }); |
| 84 | + })); |
| 85 | + } |
| 86 | + private getCommandPrompts(diagnostic: IDiagnostic): { prompt: string; command?: IDiagnosticCommand }[] { |
| 87 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 88 | + switch (diagnostic.code) { |
| 89 | + case DiagnosticCodes.NoPythonInterpretersDiagnostic: { |
| 90 | + return [{ |
| 91 | + prompt: 'Download', |
| 92 | + command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://www.python.org/downloads' }) |
| 93 | + }]; |
| 94 | + } |
| 95 | + case DiagnosticCodes.MacInterpreterSelectedAndHaveOtherInterpretersDiagnostic: { |
| 96 | + return [{ |
| 97 | + prompt: 'Select Python Interpreter', |
| 98 | + command: commandFactory.createCommand(diagnostic, { type: 'executeVSCCommand', options: 'python.setInterpreter' }) |
| 99 | + }]; |
| 100 | + } |
| 101 | + case DiagnosticCodes.MacInterpreterSelectedAndNoOtherInterpretersDiagnostic: { |
| 102 | + return [{ |
| 103 | + prompt: 'Learn more', |
| 104 | + command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://code.visualstudio.com/docs/python/python-tutorial#_prerequisites' }) |
| 105 | + }, |
| 106 | + { |
| 107 | + prompt: 'Download', |
| 108 | + command: commandFactory.createCommand(diagnostic, { type: 'launch', options: 'https://www.python.org/downloads' }) |
| 109 | + }]; |
| 110 | + } |
| 111 | + default: { |
| 112 | + throw new Error('Invalid diagnostic for \'InvalidPythonInterpreterService\''); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments