Skip to content

Commit

Permalink
Merge pull request #6180 from dibarbet/do_not_cache_dotnet_info
Browse files Browse the repository at this point in the history
Do not cache calls to dotnet info
  • Loading branch information
dibarbet authored Aug 21, 2023
2 parents 618124b + 5f88d8c commit 11e3a9d
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions src/shared/utils/getDotnetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,55 @@ import { join } from 'path';
import { execChildProcess } from '../../common';
import { CoreClrDebugUtil } from '../../coreclrDebug/util';
import { DotnetInfo } from './dotnetInfo';

let _dotnetInfo: DotnetInfo | undefined;
import { EOL } from 'os';

// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInfo> {
if (_dotnetInfo !== undefined) {
return _dotnetInfo;
}

const dotnetExecutablePath = getDotNetExecutablePath(dotNetCliPaths);

const data = await runDotnetInfo(dotnetExecutablePath);
const dotnetInfo = await parseDotnetInfo(data, dotnetExecutablePath);
return dotnetInfo;
}

export function getDotNetExecutablePath(dotNetCliPaths: string[]): string | undefined {
const dotnetExeName = `dotnet${CoreClrDebugUtil.getPlatformExeExtension()}`;
let dotnetExecutablePath: string | undefined;

for (const dotnetPath of dotNetCliPaths) {
const dotnetFullPath = join(dotnetPath, dotnetExeName);
if (CoreClrDebugUtil.existsSync(dotnetFullPath)) {
dotnetExecutablePath = dotnetFullPath;
break;
}
}
return dotnetExecutablePath;
}

async function runDotnetInfo(dotnetExecutablePath: string | undefined): Promise<string> {
try {
const env = {
...process.env,
DOTNET_CLI_UI_LANGUAGE: 'en-US',
};
const data = await execChildProcess(`${dotnetExecutablePath ?? 'dotnet'} --info`, process.cwd(), env);
return data;
} catch (error) {
const message = error instanceof Error ? error.message : `${error}`;
throw new Error(`Error running dotnet --info: ${message}`);
}
}

async function parseDotnetInfo(dotnetInfo: string, dotnetExecutablePath: string | undefined): Promise<DotnetInfo> {
try {
const cliPath = dotnetExecutablePath;
const fullInfo = data;
const fullInfo = dotnetInfo;

let version: string | undefined;
let runtimeId: string | undefined;
let architecture: string | undefined;

let lines = data.replace(/\r/gm, '').split('\n');
let lines = dotnetInfo.replace(/\r/gm, '').split('\n');
for (const line of lines) {
let match: RegExpMatchArray | null;
if ((match = /^\s*Version:\s*([^\s].*)$/.exec(line))) {
Expand Down Expand Up @@ -62,34 +85,20 @@ export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInf
}

if (version !== undefined) {
_dotnetInfo = {
const dotnetInfo: DotnetInfo = {
CliPath: cliPath,
FullInfo: fullInfo,
Version: version,
RuntimeId: runtimeId,
Architecture: architecture,
Runtimes: runtimeVersions,
};
return _dotnetInfo;
return dotnetInfo;
}

throw new Error('Failed to parse dotnet version information');
} catch {
// something went wrong with spawning 'dotnet --info'
throw new Error('A valid dotnet installation could not be found');
}
}

export function getDotNetExecutablePath(dotNetCliPaths: string[]): string | undefined {
const dotnetExeName = `dotnet${CoreClrDebugUtil.getPlatformExeExtension()}`;
let dotnetExecutablePath: string | undefined;

for (const dotnetPath of dotNetCliPaths) {
const dotnetFullPath = join(dotnetPath, dotnetExeName);
if (CoreClrDebugUtil.existsSync(dotnetFullPath)) {
dotnetExecutablePath = dotnetFullPath;
break;
}
} catch (error) {
const message = error instanceof Error ? error.message : `${error}`;
throw new Error(`Error parsing dotnet --info: ${message}, raw info was:${EOL}${dotnetInfo}`);
}
return dotnetExecutablePath;
}

0 comments on commit 11e3a9d

Please sign in to comment.