Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extractor: require only .NET runtime and allow any version higher than 2.1 #321

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -104,20 +104,16 @@ async function runExtractor(filePath: string, subscriptionId: string): Promise<v
subscriptionId
);

if (await dotnetUtils.checkDotnetVersionInstalled("2.1")) {
await cpUtils.executeCommand(
ext.outputChannel,
workingFolderPath,
'dotnet',
'apimtemplate.dll',
'extract',
'--extractorConfig',
`"${filePath}"`
);
} else {
window.showInformationMessage(localize("dotnetNotInstalled", ".NET framework 2.1 not installed. Please go to 'https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.1.0&arch=x64&rid=win10-x64' to download"));
throw new Error();
}
await dotnetUtils.validateDotnetInstalled();
await cpUtils.executeCommand(
ext.outputChannel,
workingFolderPath,
'dotnet',
'apimtemplate.dll',
'extract',
'--extractorConfig',
`"${filePath}"`
);
}

async function askFolder(): Promise<Uri[]> {
Expand Down
48 changes: 25 additions & 23 deletions src/utils/dotnetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,48 @@ export namespace dotnetUtils {
}
}

export async function validateDotnetInstalled(actionContext: IActionContext): Promise<void> {
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<void> {
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<void> {
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<boolean> {
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<boolean> {
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;
}
Expand Down