|
4 | 4 | 'use strict'; |
5 | 5 |
|
6 | 6 | import { inject, injectable } from 'inversify'; |
| 7 | +import * as semver from 'semver'; |
| 8 | +import { IApplicationEnvironment, IWorkspaceService } from '../../common/application/types'; |
| 9 | +import { NugetPackage } from '../../common/nuget/types'; |
| 10 | +import { IConfigurationService, Resource } from '../../common/types'; |
7 | 11 | import { IServiceContainer } from '../../ioc/types'; |
| 12 | +import { traceWarning } from '../../logging'; |
8 | 13 | import { LanguageServerFolderService } from '../common/languageServerFolderService'; |
9 | | -import { NodeLanguageServerFolder } from '../types'; |
| 14 | +import { |
| 15 | + BundledLanguageServerFolder, |
| 16 | + FolderVersionPair, |
| 17 | + ILanguageServerFolderService, |
| 18 | + NodeLanguageServerFolder |
| 19 | +} from '../types'; |
10 | 20 |
|
11 | | -@injectable() |
12 | | -export class NodeLanguageServerFolderService extends LanguageServerFolderService { |
13 | | - constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer) { |
| 21 | +// Must match languageServerVersion* keys in package.json |
| 22 | +export const NodeLanguageServerVersionKey = 'languageServerVersionV2'; |
| 23 | + |
| 24 | +class FallbackNodeLanguageServerFolderService extends LanguageServerFolderService { |
| 25 | + constructor(serviceContainer: IServiceContainer) { |
14 | 26 | super(serviceContainer, NodeLanguageServerFolder); |
15 | 27 | } |
16 | 28 |
|
17 | 29 | protected getMinimalLanguageServerVersion(): string { |
18 | 30 | return '0.0.0'; |
19 | 31 | } |
20 | 32 | } |
| 33 | + |
| 34 | +@injectable() |
| 35 | +export class NodeLanguageServerFolderService implements ILanguageServerFolderService { |
| 36 | + private readonly _bundledVersion: semver.SemVer | undefined; |
| 37 | + private readonly fallback: FallbackNodeLanguageServerFolderService; |
| 38 | + |
| 39 | + constructor( |
| 40 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 41 | + @inject(IConfigurationService) configService: IConfigurationService, |
| 42 | + @inject(IWorkspaceService) workspaceService: IWorkspaceService, |
| 43 | + @inject(IApplicationEnvironment) appEnv: IApplicationEnvironment |
| 44 | + ) { |
| 45 | + this.fallback = new FallbackNodeLanguageServerFolderService(serviceContainer); |
| 46 | + |
| 47 | + // downloadLanguageServer is a bit of a misnomer; if false then this indicates that a local |
| 48 | + // development copy should be run instead of a "real" build, telemetry discarded, etc. |
| 49 | + // So, we require it to be true, even though in the bundled case no real download happens. |
| 50 | + if ( |
| 51 | + configService.getSettings().downloadLanguageServer && |
| 52 | + !workspaceService.getConfiguration('python').get<string>('packageName') |
| 53 | + ) { |
| 54 | + const ver = appEnv.packageJson[NodeLanguageServerVersionKey] as string; |
| 55 | + this._bundledVersion = semver.parse(ver) || undefined; |
| 56 | + if (this._bundledVersion === undefined) { |
| 57 | + traceWarning( |
| 58 | + `invalid language server version ${ver} in package.json (${NodeLanguageServerVersionKey})` |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public get bundledVersion(): semver.SemVer | undefined { |
| 65 | + return this._bundledVersion; |
| 66 | + } |
| 67 | + |
| 68 | + public isBundled(): boolean { |
| 69 | + return this._bundledVersion !== undefined; |
| 70 | + } |
| 71 | + |
| 72 | + public async getLanguageServerFolderName(resource: Resource): Promise<string> { |
| 73 | + if (this._bundledVersion) { |
| 74 | + return BundledLanguageServerFolder; |
| 75 | + } |
| 76 | + return this.fallback.getLanguageServerFolderName(resource); |
| 77 | + } |
| 78 | + |
| 79 | + public async getLatestLanguageServerVersion(resource: Resource): Promise<NugetPackage | undefined> { |
| 80 | + if (this._bundledVersion) { |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + return this.fallback.getLatestLanguageServerVersion(resource); |
| 84 | + } |
| 85 | + |
| 86 | + public async getCurrentLanguageServerDirectory(): Promise<FolderVersionPair | undefined> { |
| 87 | + if (this._bundledVersion) { |
| 88 | + return { path: BundledLanguageServerFolder, version: this._bundledVersion }; |
| 89 | + } |
| 90 | + return this.fallback.getCurrentLanguageServerDirectory(); |
| 91 | + } |
| 92 | +} |
0 commit comments