|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | + |
| 5 | +import { inject, injectable, named } from 'inversify'; |
| 6 | +import { ConfigurationTarget, DiagnosticSeverity } from 'vscode'; |
| 7 | +import { ICommandManager, IWorkspaceService } from '../../../common/application/types'; |
| 8 | +import { PVSC_EXTENSION_ID } from '../../../common/constants'; |
| 9 | +import { IDisposableRegistry, Resource } from '../../../common/types'; |
| 10 | +import { SwitchToPrereleaseExtension } from '../../../common/utils/localize'; |
| 11 | +import { IServiceContainer } from '../../../ioc/types'; |
| 12 | +import { sendTelemetryEvent } from '../../../telemetry'; |
| 13 | +import { EventName } from '../../../telemetry/constants'; |
| 14 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 15 | +import { DiagnosticCodes } from '../constants'; |
| 16 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 17 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 18 | + |
| 19 | +export class SwitchToPreReleaseExtensionDiagnostic extends BaseDiagnostic { |
| 20 | + constructor(message: string, resource: Resource) { |
| 21 | + super( |
| 22 | + DiagnosticCodes.SwitchToPreReleaseExtensionDiagnostic, |
| 23 | + message, |
| 24 | + DiagnosticSeverity.Warning, |
| 25 | + DiagnosticScope.Global, |
| 26 | + resource, |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const SwitchToPreReleaseExtensionDiagnosticServiceId = 'SwitchToPreReleaseExtensionDiagnosticServiceId'; |
| 32 | + |
| 33 | +@injectable() |
| 34 | +export class SwitchToPreReleaseExtensionDiagnosticService extends BaseDiagnosticsService { |
| 35 | + constructor( |
| 36 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 37 | + @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, |
| 38 | + @inject(ICommandManager) private readonly commandManager: ICommandManager, |
| 39 | + @inject(IDiagnosticHandlerService) |
| 40 | + @named(DiagnosticCommandPromptHandlerServiceId) |
| 41 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 42 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry, |
| 43 | + ) { |
| 44 | + super( |
| 45 | + [DiagnosticCodes.SwitchToPreReleaseExtensionDiagnostic], |
| 46 | + serviceContainer, |
| 47 | + disposableRegistry, |
| 48 | + true, |
| 49 | + true, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 54 | + const config = this.workspaceService.getConfiguration('python', resource); |
| 55 | + const value = config.inspect<string>('insidersChannel'); |
| 56 | + if (value) { |
| 57 | + const insiderType = value.globalValue ?? value.globalLanguageValue; |
| 58 | + if (insiderType) { |
| 59 | + return Promise.resolve([ |
| 60 | + new SwitchToPreReleaseExtensionDiagnostic(SwitchToPrereleaseExtension.bannerMessage(), resource), |
| 61 | + ]); |
| 62 | + } |
| 63 | + } |
| 64 | + return Promise.resolve([]); |
| 65 | + } |
| 66 | + |
| 67 | + protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 68 | + if (diagnostics.length === 0 || !this.canHandle(diagnostics[0])) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const diagnostic = diagnostics[0]; |
| 73 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + await this.messageService.handle(diagnostic, { |
| 78 | + onClose: () => { |
| 79 | + sendTelemetryEvent(EventName.INSIDERS_PROMPT, undefined, { selection: 'closed' }); |
| 80 | + }, |
| 81 | + commandPrompts: [ |
| 82 | + { |
| 83 | + prompt: SwitchToPrereleaseExtension.installPreRelease(), |
| 84 | + command: { |
| 85 | + diagnostic, |
| 86 | + invoke: (): Promise<void> => this.installExtension(true, diagnostic.resource), |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + prompt: SwitchToPrereleaseExtension.installStable(), |
| 91 | + command: { |
| 92 | + diagnostic, |
| 93 | + invoke: (): Promise<void> => this.installExtension(false, diagnostic.resource), |
| 94 | + }, |
| 95 | + }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private async installExtension(preRelease: boolean, resource: Resource): Promise<void> { |
| 101 | + sendTelemetryEvent(EventName.INSIDERS_PROMPT, undefined, { selection: preRelease ? 'preRelease' : 'stable' }); |
| 102 | + const config = this.workspaceService.getConfiguration('python', resource); |
| 103 | + const setting = config.inspect<string>('insidersChannel'); |
| 104 | + if (setting) { |
| 105 | + config.update('insidersChannel', undefined, ConfigurationTarget.Global); |
| 106 | + } |
| 107 | + await this.commandManager.executeCommand(`workbench.extensions.installExtension`, PVSC_EXTENSION_ID, { |
| 108 | + installPreReleaseVersion: preRelease, |
| 109 | + }); |
| 110 | + } |
| 111 | +} |
0 commit comments