@@ -8,32 +8,55 @@ import { join } from 'path';
88import { execChildProcess } from '../../common' ;
99import { CoreClrDebugUtil } from '../../coreclrDebug/util' ;
1010import { DotnetInfo } from './dotnetInfo' ;
11-
12- let _dotnetInfo : DotnetInfo | undefined ;
11+ import { EOL } from 'os' ;
1312
1413// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
1514export async function getDotnetInfo ( dotNetCliPaths : string [ ] ) : Promise < DotnetInfo > {
16- if ( _dotnetInfo !== undefined ) {
17- return _dotnetInfo ;
18- }
19-
2015 const dotnetExecutablePath = getDotNetExecutablePath ( dotNetCliPaths ) ;
2116
17+ const data = await runDotnetInfo ( dotnetExecutablePath ) ;
18+ const dotnetInfo = await parseDotnetInfo ( data , dotnetExecutablePath ) ;
19+ return dotnetInfo ;
20+ }
21+
22+ export function getDotNetExecutablePath ( dotNetCliPaths : string [ ] ) : string | undefined {
23+ const dotnetExeName = `dotnet${ CoreClrDebugUtil . getPlatformExeExtension ( ) } ` ;
24+ let dotnetExecutablePath : string | undefined ;
25+
26+ for ( const dotnetPath of dotNetCliPaths ) {
27+ const dotnetFullPath = join ( dotnetPath , dotnetExeName ) ;
28+ if ( CoreClrDebugUtil . existsSync ( dotnetFullPath ) ) {
29+ dotnetExecutablePath = dotnetFullPath ;
30+ break ;
31+ }
32+ }
33+ return dotnetExecutablePath ;
34+ }
35+
36+ async function runDotnetInfo ( dotnetExecutablePath : string | undefined ) : Promise < string > {
2237 try {
2338 const env = {
2439 ...process . env ,
2540 DOTNET_CLI_UI_LANGUAGE : 'en-US' ,
2641 } ;
2742 const data = await execChildProcess ( `${ dotnetExecutablePath ?? 'dotnet' } --info` , process . cwd ( ) , env ) ;
43+ return data ;
44+ } catch ( error ) {
45+ const message = error instanceof Error ? error . message : `${ error } ` ;
46+ throw new Error ( `Error running dotnet --info: ${ message } ` ) ;
47+ }
48+ }
2849
50+ async function parseDotnetInfo ( dotnetInfo : string , dotnetExecutablePath : string | undefined ) : Promise < DotnetInfo > {
51+ try {
2952 const cliPath = dotnetExecutablePath ;
30- const fullInfo = data ;
53+ const fullInfo = dotnetInfo ;
3154
3255 let version : string | undefined ;
3356 let runtimeId : string | undefined ;
3457 let architecture : string | undefined ;
3558
36- let lines = data . replace ( / \r / gm, '' ) . split ( '\n' ) ;
59+ let lines = dotnetInfo . replace ( / \r / gm, '' ) . split ( '\n' ) ;
3760 for ( const line of lines ) {
3861 let match : RegExpMatchArray | null ;
3962 if ( ( match = / ^ \s * V e r s i o n : \s * ( [ ^ \s ] .* ) $ / . exec ( line ) ) ) {
@@ -62,34 +85,20 @@ export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInf
6285 }
6386
6487 if ( version !== undefined ) {
65- _dotnetInfo = {
88+ const dotnetInfo : DotnetInfo = {
6689 CliPath : cliPath ,
6790 FullInfo : fullInfo ,
6891 Version : version ,
6992 RuntimeId : runtimeId ,
7093 Architecture : architecture ,
7194 Runtimes : runtimeVersions ,
7295 } ;
73- return _dotnetInfo ;
96+ return dotnetInfo ;
7497 }
7598
7699 throw new Error ( 'Failed to parse dotnet version information' ) ;
77- } catch {
78- // something went wrong with spawning 'dotnet --info'
79- throw new Error ( 'A valid dotnet installation could not be found' ) ;
80- }
81- }
82-
83- export function getDotNetExecutablePath ( dotNetCliPaths : string [ ] ) : string | undefined {
84- const dotnetExeName = `dotnet${ CoreClrDebugUtil . getPlatformExeExtension ( ) } ` ;
85- let dotnetExecutablePath : string | undefined ;
86-
87- for ( const dotnetPath of dotNetCliPaths ) {
88- const dotnetFullPath = join ( dotnetPath , dotnetExeName ) ;
89- if ( CoreClrDebugUtil . existsSync ( dotnetFullPath ) ) {
90- dotnetExecutablePath = dotnetFullPath ;
91- break ;
92- }
100+ } catch ( error ) {
101+ const message = error instanceof Error ? error . message : `${ error } ` ;
102+ throw new Error ( `Error parsing dotnet --info: ${ message } , raw info was:${ EOL } ${ dotnetInfo } ` ) ;
93103 }
94- return dotnetExecutablePath ;
95104}
0 commit comments