diff --git a/src/commands/extract.ts b/src/commands/extract.ts index bdff034..534094a 100644 --- a/src/commands/extract.ts +++ b/src/commands/extract.ts @@ -63,7 +63,7 @@ async function extract(node: ApiTreeItem | ServiceTreeItem, apiName?: string): P }, async () => { await azUtils.checkAzInstalled(); - await dotnetUtils.checkDotnetInstalled(); + await dotnetUtils.validateDotnetInstalled(); await runExtractor(configFile, subscriptionId); } ).then( @@ -104,20 +104,16 @@ async function runExtractor(filePath: string, subscriptionId: string): Promise { diff --git a/src/utils/dotnetUtils.ts b/src/utils/dotnetUtils.ts index 0efda6b..d0da244 100644 --- a/src/utils/dotnetUtils.ts +++ b/src/utils/dotnetUtils.ts @@ -19,46 +19,48 @@ export namespace dotnetUtils { } } - export async function validateDotnetInstalled(actionContext: IActionContext): Promise { - if (!await isDotnetInstalled()) { - const message: string = localize('dotnetNotInstalled', 'You must have the .NET CLI installed to perform this operation.'); + export async function validateDotnetInstalled(actionContext?: IActionContext, minVersion: string = "2.1"): Promise { + if (!await isDotnetInstalled() || !await checkDotnetVersionInstalled(minVersion)) { + const message: string = localize('dotnetNotInstalled', 'You must have the .NET CLI {0} or older installed to perform this operation.', minVersion); - if (!actionContext.errorHandling.suppressDisplay) { + if (!actionContext || !actionContext.errorHandling.suppressDisplay) { // don't wait vscode.window.showErrorMessage(message, DialogResponses.learnMore).then(async (result) => { if (result === DialogResponses.learnMore) { await openUrl('https://aka.ms/AA4ac70'); } }); - actionContext.errorHandling.suppressDisplay = true; + if (actionContext) { + actionContext.errorHandling.suppressDisplay = true; + } } throw new Error(message); } } - export async function checkDotnetInstalled(): Promise { - if (!await isDotnetInstalled()) { - const message: string = localize('dotnetNotInstalled', 'You must have a .NET Core SDK v2.1.x CLI installed to perform this operation.'); - - // don't wait - vscode.window.showErrorMessage(message, DialogResponses.learnMore).then(async (result) => { - if (result === DialogResponses.learnMore) { - await openUrl('https://aka.ms/AA4ac70'); - } - }); - - throw new Error(message); + function compareVersion(version1: string, version2: string): number { + const v1 = version1.split('.').map(parseInt); + const v2 = version2.split('.').map(parseInt); + for (let i = 0; i < Math.min(v1.length, v2.length); i++) { + if (v1[i] > v2[i]) { return 1; } + if (v1[i] < v2[i]) { return -1; } } + return v1.length === v2.length ? 0 : (v1.length < v2.length ? -1 : 1); } - export async function checkDotnetVersionInstalled(targetVersion: string): Promise { - const response = await cpUtils.executeCommand(undefined, undefined, 'dotnet', '--list-sdks'); - const versions = response.split(/\r?\n/); - for (const version of versions) { - if (version.startsWith(targetVersion)) { - return true; + async function checkDotnetVersionInstalled(minVersion: string): Promise { + try { + const response = await cpUtils.executeCommand(undefined, undefined, 'dotnet', '--list-runtimes'); + const versions = response.split(/\r?\n/); + for (const version of versions) { + const versionNumber = version.split(' ')[1]; + if (compareVersion(versionNumber, minVersion) >= 0) { + return false; + } } + } catch (error) { + return false; } return false; }