|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { exec } from "child_process"; |
| 7 | +import * as path from 'path'; |
| 8 | +import { promisify } from "util"; |
| 9 | +import { HostExecutableInformation } from "../constants/HostExecutableInformation"; |
| 10 | +import { IHostExecutableResolver } from "../constants/IHostExecutableResolver"; |
| 11 | +import { PlatformInformation } from "../platform"; |
| 12 | +import { Options } from "./options"; |
| 13 | + |
| 14 | +const minimumDotnetMajor = 6; |
| 15 | +const minimumDotnetMinor = 0; |
| 16 | +const minimumDotnetPatch = 100; |
| 17 | + |
| 18 | +export class OmniSharpDotnetResolver implements IHostExecutableResolver { |
| 19 | + |
| 20 | + constructor(private platformInfo: PlatformInformation) { } |
| 21 | + |
| 22 | + public async getHostExecutableInfo(options: Options): Promise<HostExecutableInformation> { |
| 23 | + const dotnet = this.platformInfo.isWindows() ? 'dotnet.exe' : 'dotnet'; |
| 24 | + const env = { ...process.env }; |
| 25 | + |
| 26 | + if (options.dotnetPath) { |
| 27 | + env['PATH'] = options.dotnetPath + path.delimiter + env['PATH']; |
| 28 | + } |
| 29 | + |
| 30 | + // Test the dotnet exe for version |
| 31 | + const result = await promisify(exec)(`${dotnet} --version`, { env }); |
| 32 | + |
| 33 | + if (result.stderr) { |
| 34 | + throw new Error(`Unable to read dotnet version information. Error ${result.stderr}`); |
| 35 | + } |
| 36 | + |
| 37 | + const segments = result.stdout.split('.').map(str => Number.parseInt(str)); |
| 38 | + if (segments.length != 3) { |
| 39 | + throw new Error(`Unknown result output from 'dotnet --version'. Received ${result.stdout}`); |
| 40 | + } |
| 41 | + |
| 42 | + if (this.testVersionPart(segments[0], minimumDotnetMajor, result.stdout) |
| 43 | + || this.testVersionPart(segments[1], minimumDotnetMinor, result.stdout)) { |
| 44 | + return { |
| 45 | + version: result.stdout, |
| 46 | + path: options.dotnetPath, |
| 47 | + env |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + this.testVersionPart(segments[2], minimumDotnetPatch, result.stdout); |
| 52 | + |
| 53 | + return { |
| 54 | + version: result.stdout, |
| 55 | + path: options.dotnetPath, |
| 56 | + env |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + private testVersionPart(actualVersion: number, minimumRequired: number, foundVersion: string): boolean { |
| 61 | + if (actualVersion < minimumRequired) { |
| 62 | + throw new Error(`Found dotnet version ${foundVersion}. Minimum required version is ${minimumDotnetMajor}.${minimumDotnetMinor}.${minimumDotnetPatch}.`); |
| 63 | + } |
| 64 | + |
| 65 | + return actualVersion === minimumRequired; |
| 66 | + } |
| 67 | +} |
0 commit comments