@@ -8,26 +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
+ import { EOL } from 'os' ;
11
12
12
13
// This function calls `dotnet --info` and returns the result as a DotnetInfo object.
13
14
export async function getDotnetInfo ( dotNetCliPaths : string [ ] ) : Promise < DotnetInfo > {
14
15
const dotnetExecutablePath = getDotNetExecutablePath ( dotNetCliPaths ) ;
15
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 > {
16
37
try {
17
38
const env = {
18
39
...process . env ,
19
40
DOTNET_CLI_UI_LANGUAGE : 'en-US' ,
20
41
} ;
21
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
+ }
22
49
50
+ async function parseDotnetInfo ( dotnetInfo : string , dotnetExecutablePath : string | undefined ) : Promise < DotnetInfo > {
51
+ try {
23
52
const cliPath = dotnetExecutablePath ;
24
- const fullInfo = data ;
53
+ const fullInfo = dotnetInfo ;
25
54
26
55
let version : string | undefined ;
27
56
let runtimeId : string | undefined ;
28
57
let architecture : string | undefined ;
29
58
30
- let lines = data . replace ( / \r / gm, '' ) . split ( '\n' ) ;
59
+ let lines = dotnetInfo . replace ( / \r / gm, '' ) . split ( '\n' ) ;
31
60
for ( const line of lines ) {
32
61
let match : RegExpMatchArray | null ;
33
62
if ( ( match = / ^ \s * V e r s i o n : \s * ( [ ^ \s ] .* ) $ / . exec ( line ) ) ) {
@@ -68,22 +97,8 @@ export async function getDotnetInfo(dotNetCliPaths: string[]): Promise<DotnetInf
68
97
}
69
98
70
99
throw new Error ( 'Failed to parse dotnet version information' ) ;
71
- } catch {
72
- // something went wrong with spawning 'dotnet --info'
73
- throw new Error ( 'A valid dotnet installation could not be found' ) ;
74
- }
75
- }
76
-
77
- export function getDotNetExecutablePath ( dotNetCliPaths : string [ ] ) : string | undefined {
78
- const dotnetExeName = `dotnet${ CoreClrDebugUtil . getPlatformExeExtension ( ) } ` ;
79
- let dotnetExecutablePath : string | undefined ;
80
-
81
- for ( const dotnetPath of dotNetCliPaths ) {
82
- const dotnetFullPath = join ( dotnetPath , dotnetExeName ) ;
83
- if ( CoreClrDebugUtil . existsSync ( dotnetFullPath ) ) {
84
- dotnetExecutablePath = dotnetFullPath ;
85
- break ;
86
- }
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 } ` ) ;
87
103
}
88
- return dotnetExecutablePath ;
89
104
}
0 commit comments