@@ -8,32 +8,55 @@ import { join } from 'path';
8
8
import { execChildProcess } from '../../common' ;
9
9
import { CoreClrDebugUtil } from '../../coreclrDebug/util' ;
10
10
import { DotnetInfo } from './dotnetInfo' ;
11
-
12
- let _dotnetInfo : DotnetInfo | undefined ;
11
+ import { EOL } from 'os' ;
13
12
14
13
// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
15
14
export async function getDotnetInfo ( dotNetCliPaths : string [ ] ) : Promise < DotnetInfo > {
16
- if ( _dotnetInfo !== undefined ) {
17
- return _dotnetInfo ;
18
- }
19
-
20
15
const dotnetExecutablePath = getDotNetExecutablePath ( dotNetCliPaths ) ;
21
16
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 > {
22
37
try {
23
38
const env = {
24
39
...process . env ,
25
40
DOTNET_CLI_UI_LANGUAGE : 'en-US' ,
26
41
} ;
27
42
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
+ }
28
49
50
+ async function parseDotnetInfo ( dotnetInfo : string , dotnetExecutablePath : string | undefined ) : Promise < DotnetInfo > {
51
+ try {
29
52
const cliPath = dotnetExecutablePath ;
30
- const fullInfo = data ;
53
+ const fullInfo = dotnetInfo ;
31
54
32
55
let version : string | undefined ;
33
56
let runtimeId : string | undefined ;
34
57
let architecture : string | undefined ;
35
58
36
- let lines = data . replace ( / \r / gm, '' ) . split ( '\n' ) ;
59
+ let lines = dotnetInfo . replace ( / \r / gm, '' ) . split ( '\n' ) ;
37
60
for ( const line of lines ) {
38
61
let match : RegExpMatchArray | null ;
39
62
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
62
85
}
63
86
64
87
if ( version !== undefined ) {
65
- _dotnetInfo = {
88
+ const dotnetInfo : DotnetInfo = {
66
89
CliPath : cliPath ,
67
90
FullInfo : fullInfo ,
68
91
Version : version ,
69
92
RuntimeId : runtimeId ,
70
93
Architecture : architecture ,
71
94
Runtimes : runtimeVersions ,
72
95
} ;
73
- return _dotnetInfo ;
96
+ return dotnetInfo ;
74
97
}
75
98
76
99
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 } ` ) ;
93
103
}
94
- return dotnetExecutablePath ;
95
104
}
0 commit comments